http://acm.hdu.edu.cn/showproblem.php?pid=1166
单点更新,区间求和
1 10 1 2 3 4 5 6 7 8 9 10 Query 1 3 Add 3 6 Query 2 7 Sub 10 2 Add 6 3 Query 3 10 End
Case 1: 6 33 59
#include <cstdio> #include <cstring> #include <algorithm> #include <queue> #include <map> #include <set> #include <stack> #include <cmath> #include <string> #include <iostream> #include <string> #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define LL long long const int SIZE=5e4+10; using namespace std; struct sge{ int sum; }a[SIZE<<2]; void pushup(int rt){ a[rt].sum=a[rt<<1].sum+a[rt<<1|1].sum; } void build(int l,int r,int rt){ if(l==r){ scanf("%d",&a[rt].sum); return; } int m=(l+r)>>1; build(lson); build(rson); pushup(rt); } void update(int p,int c,int l,int r,int rt){ if(l==r){ a[rt].sum+=c; return; } int m=(l+r)>>1; if(p<=m)update(p,c,lson); else update(p,c,rson); pushup(rt); } int query(int L,int R,int l,int r,int rt){ if(L<=l&&r<=R){ return a[rt].sum; } int m=(l+r)>>1; int ans=0; if(L<=m)ans+=query(L,R,lson); if(R>m)ans+=query(L,R,rson); return ans; } int main() { int T,n,x,y; char s[20]; scanf("%d",&T); for(int cas=1;cas<=T;cas++){ scanf("%d",&n); build(1,n,1); printf("Case %d:\n",cas); while(scanf("%s",s)){ if(s[0]=='E')break; scanf("%d%d",&x,&y); if(s[0]=='Q'){ printf("%d\n",query(x,y,1,n,1)); } else if(s[0]=='A')update(x,y,1,n,1); else update(x,-y,1,n,1); } } return 0; }