hdu 1166

#include <iostream>
#include <string.h>
#include <stdio.h>
#define MAX_N 50001
using 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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值