树状数组-区间求和

P3374 【模板】树状数组 1

 1 /*by SilverN*/
 2 #include<algorithm>
 3 #include<iostream>
 4 #include<cstring>
 5 #include<cstdio>
 6 #include<cmath>
 7 using namespace std;
 8 int read(){
 9     int x=0,f=1;char ch=getchar();
10     while(ch<'0' || ch>'9'){if(ch=='-')f=-1;ch=getchar();}
11     while(ch>='0' && ch<='9'){x=x*10+ch-'0';ch=getchar();}
12     return x*f;
13 }
14 const int mxn=500010;
15 int n,m;
16 int t[mxn];
17 void add(int x,int v){
18     while(x<mxn){t[x]+=v;x+=x&-x;}
19 }
20 int smm(int x){
21     int res=0;
22     while(x){
23         res+=t[x];
24         x-=x&-x;
25     }
26     return res;
27 }
28 int main(){
29     int op,x,k;
30     n=read();m=read();
31     int i,j;
32     for(i=1;i<=n;i++){
33         x=read();
34         add(i,x);
35     }
36     for(i=1;i<=m;i++){
37         op=read();x=read();k=read();
38         if(op==1){
39             add(x,k);
40         }
41         else{
42             printf("%d\n",smm(k)-smm(x-1));
43         }
44     }
45     return 0;
46 }
树状数组1

相关文章:

  • 2021-04-29
  • 2022-12-23
  • 2021-09-29
  • 2021-10-05
  • 2021-11-08
  • 2021-08-20
  • 2021-11-16
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-08-09
  • 2021-08-15
  • 2022-12-23
  • 2021-12-12
  • 2021-10-27
相关资源
相似解决方案