hdu 1166

来源:互联网 发布:php 获取项目根目录 编辑:程序博客网 时间:2024/05/22 09:42
#include <iostream>#include <string.h>#include <stdio.h>#define MAX_N 50001using namespace std;int a[MAX_N];char str[10];struct node {    int left, right, sum;} tt[MAX_N * 3];int build (int left, int right, int index) {    tt[index].left = left;    tt[index].right = right;    if (left == right) return tt[index].sum = a[left];    int mid = (left + right) / 2;    return tt[index].sum = build (left, mid, index * 2) + build (mid + 1, right, index * 2 + 1);}int count (int a, int b, int index) {    int left, right, mid;    left = tt[index].left;    right = tt[index].right;    if (left == a && right == b) return tt[index].sum;    mid = (left + right) / 2;    if (mid >= b) return count (a, b, index * 2);    else if (mid < a) return count (a, b, index * 2 + 1);    else return count (a, mid, index * 2) + count (mid + 1, b, index * 2 + 1);}void update(int index, int x, int a) {    tt[index].sum += a;    if (tt[index].left == x && tt[index].right == x) return;    int mid;    mid = (tt[index].left + tt[index].right) / 2;    if (mid >= x) update(index * 2, x, a);    else update(index * 2 + 1, x, a);}int main() {    int t, icase, n, i, x, y;    scanf("%d", &icase);    t = 0;    while (icase--) {        scanf("%d", &n);        for (i = 1; i <= n; i++) scanf("%d", &a[i]);        build(1, n, 1);        printf("Case %d:\n", ++t);        while (1) {            scanf("%s", str);            if (str[0] == 'E') break;            scanf("%d%d", &x, &y);            switch(str[0]) {            case 'Q': printf("%d\n", count(x, y, 1));            break;            case 'A': update(1, x, y);            break;            case 'S': update(1, x, -y);            break;            }        }    }    return 0;}