首页 > 代码库 > 树状数组Hdu1166

树状数组Hdu1166

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <string>
#include <iostream>
using namespace std;
int c[222222];
int n;
int lowbit(int x)
{
    return x&(-x);
}
 
int sum(int n)
{
    int sum=0;
    while(n>0){
        sum+=c[n];
        n-=lowbit(n);
    }
    return sum;
}
 
void change(int k,int add)
{
    while(k<=n){
        c[k]+=add;
        k+=lowbit(k);
    }
}
int main()
{
    int t;
    char str[100];int pos,add,l,r;
    scanf("%d",&t);
    for(int i=1;i<=t;i++){
        memset(c,0,sizeof(c));
        scanf("%d",&n);
        for(int i=1;i<=n;i++){
            int ret;scanf("%d",&ret);
            change(i,ret);
        }
        printf("Case %d:\n",i);
        for(;;){
            scanf("%s",str);
            if(strcmp(str,"End")==0)
                break;
            if(strcmp(str,"Add")==0){
                scanf("%d%d",&pos,&add);
                change(pos,add);
            }
            if(strcmp(str,"Query")==0){
                scanf("%d%d",&l,&r);
                printf("%d\n",sum(r)-sum(l-1));
            }
            if(strcmp(str,"Sub")==0){
                cin>>pos>>add;
                change(pos,-add);
            }
        }
    }
    return 0;
}