点修改区间查询 HDU1166
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include <cstdio> 3 4 using namespace std; 5 6 int an[50010]; 7 struct Node 8 { 9 int l,r; 10 int v; 11 }bn[200000]; 12 13 void build(int k,int l,int r) 14 { 15 bn[k].l=l; 16 bn[k].r=r; 17 if(l==r) 18 { 19 bn[k].v=an[l]; 20 return ; 21 } 22 int lk=k*2; 23 int rk=lk+1; 24 int mid=(l+r)/2; 25 build(lk,l,mid); 26 build(rk,mid+1,r); 27 bn[k].v=bn[lk].v+bn[rk].v; 28 } 29 30 void change(int k,int i,int a) 31 { 32 if(bn[k].l==i&&bn[k].r==i) 33 { 34 bn[k].v+=a; 35 return ; 36 } 37 int lk=k*2; 38 int rk=lk+1; 39 if(i<=bn[lk].r) 40 change(lk,i,a); 41 else if(i>bn[k].l) 42 change(rk,i,a); 43 bn[k].v=bn[lk].v+bn[rk].v; 44 } 45 46 int search(int k,int l,int r) 47 { 48 if(bn[k].l==l&&bn[k].r==r) 49 return bn[k].v; 50 int lk=k*2; 51 int rk=lk+1; 52 if(bn[lk].r>=r) 53 search(lk,l,r); 54 else if(bn[rk].l<=l) 55 search(rk,l,r); 56 else 57 return search(lk,l,bn[lk].r)+search(rk,bn[rk].l,r); 58 } 59 60 int main() 61 { 62 int t; 63 cin>>t; 64 for(int l=1;l<=t;l++) 65 { 66 cout<<"Case "<<l<<":"<<endl; 67 int n; 68 scanf("%d",&n); 69 for(int i=1;i<=n;i++) 70 scanf("%d",&an[i]); 71 build(1,1,n); 72 char str[10]; 73 int a,b; 74 while(scanf("%s",str)!=EOF) 75 { 76 if(str[0]=='E') 77 break; 78 scanf("%d%d",&a,&b); 79 if(str[0]=='Q') 80 { 81 cout<<search(1,a,b)<<endl; 82 } 83 else if(str[0]=='A') 84 { 85 change(1,a,b); 86 } 87 else if(str[0]=='S') 88 { 89 change(1,a,-b); 90 } 91 } 92 } 93 return 0; 94 }