#include<iostream> #include<cstdio> #include<cstring> using namespace std; const int mx=5e4+10; int node[mx<<2]; void build(int l,int r,int rt) { if(l==r) { scanf("%d",&node[rt]); return ; } int m=(l+r)/2; build(l,m,2*rt); build(m+1,r,2*rt+1); node[rt]=node[2*rt]+node[2*rt+1]; } void update(int p,int ans,int l,int r,int rt) { if(l==r) { node[rt]+=ans; return ; } int m=(l+r)/2; if(p<=m) update(p,ans,l,m,2*rt); if(p>m) update(p,ans,m+1,r,2*rt+1); node[rt]=node[2*rt]+node[2*rt+1]; } int sea(int L,int R,int l,int r,int rt) { if(L<=l&&r<=R)return node[rt]; int ans=0; int m=(l+r)/2; if( L<=m)ans+=sea(L,R,l,m,rt*2); if(m<R)ans+=sea(L,R,m+1,r,rt*2+1); return ans; } int main() { int t,n,a,b; char te[10]; scanf("%d",&t); for(int i=1;i<=t;i++) {printf("Case %d:\n",i); scanf("%d",&n); build(1,n,1); while(scanf("%s",te)&&te[0]!='E') { if(te[0]=='A') { scanf("%d %d",&a,&b); update(a,b,1,n,1); } else if(te[0]=='S') { scanf("%d %d",&a,&b); update(a,-b,1,n,1); } else { scanf("%d %d",&a,&b); printf("%d\n",sea(a, b, 1, n, 1)); } } } }
HDU1166:敌兵布阵(线段树单点更新)
猜你喜欢
转载自blog.csdn.net/qq_41568836/article/details/80290455
今日推荐
周排行