标签:des style blog io ar color os sp java
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 46775 Accepted Submission(s): 19828
线段树单点更新
#include<iostream> #include<cstdio> #include<cstring> using namespace std; #pragma comment(linker, "/STACK:1024000000,1024000000") #define N 50005 int sum[N<<2]; void pushup(int rt) { sum[rt]=sum[rt<<1]+sum[rt<<1|1]; } void build(int l,int r,int rt) { if(l==r) { scanf("%d",&sum[rt]); return; } int m=(l+r)>>1; build(l,m,rt<<1); build(m+1,r,rt<<1|1); pushup(rt); } void update(int l,int r,int rt,int pos,int c) { if(l==r) { sum[rt]+=c; return; } int m=(l+r)>>1; if(pos<=m) update(l,m,rt<<1,pos,c); else update(m+1,r,rt<<1|1,pos,c); pushup(rt); } int query(int l,int r,int rt,int L,int R) { if(L==l && r==R) { return sum[rt]; } int m=(l+r)>>1; if(R<=m) return query(l,m,rt<<1,L,R); else if(L>m) return query(m+1,r,rt<<1|1,L,R); else return query(l,m,rt<<1,L,m)+query(m+1,r,rt<<1|1,m+1,R); } int main() { int n,T,iCase=1; char op[2]; scanf("%d",&T); while(T--) { scanf("%d",&n); build(1,n,1); printf("Case %d:\n",iCase++); while(scanf("%s",op) && op[0]!=‘E‘) { int a,b; scanf("%d%d",&a,&b); if(op[0]==‘A‘) update(1,n,1,a,b); else if(op[0]==‘S‘) update(1,n,1,a,-b); else printf("%d\n",query(1,n,1,a,b)); } } return 0; }
标签:des style blog io ar color os sp java
原文地址:http://www.cnblogs.com/hate13/p/4119864.html