标签:des style java color os 数据 io 2014
敌兵布阵<span style="color:#6600cc;">/*************************************************** author : Grant Yuan time : 2014.7.27 algorithm: 线段树 explain : 用printf()312ms,用cout超时 ****************************************************/ #include <iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<cstring> #include<algorithm> #define MAX 50003 using namespace std; struct node{ int l; int r; int sum; }; node tree[3*MAX]; int b[MAX]; int t,n,ans; int ct=1; void build(int left,int right,int root) { tree[root].l=left; tree[root].r=right; if(left==right){ tree[root].sum=b[left]; return; } int mid=(left+right)>>1; build(left,mid,root*2); build(mid+1,right,root*2+1); tree[root].sum=tree[root*2].sum+tree[root*2+1].sum; } void OperAdd(int point,int root,int num) { if(tree[root].l==tree[root].r) { tree[root].sum+=num; return; } int mid=(tree[root].l+tree[root].r)>>1; if(mid>=point) OperAdd(point,root*2,num); else if(point>mid) OperAdd(point,root*2+1,num); tree[root].sum=tree[root*2].sum+tree[root*2+1].sum; } void OperSub(int point,int root,int num) { if(tree[root].l==tree[root].r) { tree[root].sum-=num; return; } int mid=(tree[root].l+tree[root].r)>>1; if(mid>=point) OperSub(point,root*2,num); else if(point>mid) OperSub(point,root*2+1,num); tree[root].sum=tree[root*2].sum+tree[root*2+1].sum; } void Query(int left,int right,int root) { if(tree[root].l>right||tree[root].r<left) return; if(left==tree[root].l&&right==tree[root].r) {ans+=tree[root].sum; return;} int mid=(tree[root].l+tree[root].r)>>1; if(mid<left) Query(left,right,root*2+1); else if(mid>=right) Query(left,right,root*2); else{ Query(left,mid,root*2); Query(mid+1,right,root*2+1); } } int main() { char s[7];int aa,bb; scanf("%d",&t); while(t--){ scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d",&b[i]); build(1,n,1); printf("Case %d:\n",ct++); while(1){ scanf("%s",s); if(strcmp(s,"Add")==0) { scanf("%d%d",&aa,&bb); OperAdd(aa,1,bb); } else if(strcmp(s,"Sub")==0) { scanf("%d%d",&aa,&bb); OperSub(aa,1,bb); } else if(strcmp(s,"Query")==0) { ans=0; scanf("%d%d",&aa,&bb); Query(aa,bb,1); printf("%d\n",ans); } else if(strcmp(s,"End")==0) { break; } } } return 0; } </span>
标签:des style java color os 数据 io 2014
原文地址:http://blog.csdn.net/yuanchang_best/article/details/38170765