最短树状数组!!!

不废话

 1 #include<cstdio>
 2 #define lowbit(x) (x&-x)
 3 #define add(x,k)  while(x<=n) sumv[x]+=k,x+=lowbit(x)
 4 #define query(x,a) while(x) a+=sumv[x],x-=lowbit(x)
 5 
 6 const int maxn = 500010;
 7 int n, q, x, l, r, ansl, ansr;
 8 int sumv[maxn];
 9 
10 int main() {
11     scanf("%d %d", &n, &q);
12     for(int i = 1,y;i <= n;i++) {
13         scanf("%d", &y);
14         int k = i;  add(k,y);
15     }
16 
17   while(q--) {
18       scanf("%d %d %d",&x,&l,&r);
19       if(x == 1) add(l,r);
20       else {
21           l--; ansl = 0; ansr = 0;
22           query(l,ansl); query(r,ansr);
23           printf("%d\n", ansr-ansl);
24         }
25     }
26     return 0;
27 } 

 

posted @ 2018-08-24 10:48  Frank的成长之路  阅读(94)  评论(0编辑  收藏  举报