标签:des style blog java color os strong 数据
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<stdio.h> #include<string.h> #include<iostream> using namespace std; struct Node{ int left,right; int s; }a[200010]; void built(int cur,int x,int y){ a[cur].left=x; a[cur].right=y; if(x==y){ scanf("%d",&a[cur].s); return ; } if(x<y){ int mid=(x+y)>>1; built(cur<<1,x,mid); built(cur<<1 |1,mid+1,y); a[cur].s=a[cur<<1].s+a[cur<<1|1].s; } } void insert(int cur,int v,int val){ if(a[cur].left==v && a[cur].right==v){ a[cur].s+=val; return ; } int mid=(a[cur].left+a[cur].right)>>1; if(v<=mid) insert(cur<<1,v,val); else insert(cur<<1 | 1,v,val); a[cur].s=a[cur<<1].s+a[cur<<1 | 1].s; } int sum(int cur,int x,int y){ if(a[cur].left==x && a[cur].right==y) return a[cur].s; int mid=(a[cur].left+a[cur].right)>>1; if(y<=mid) return sum(cur<<1,x,y); else if(x>mid) return sum(cur<<1|1,x,y); else{ return sum(cur<<1,x,mid)+sum(cur<<1|1,mid+1,y); } } //void output(int cur){ // if(a[cur].left==a[cur].right){ // printf("%d ",a[cur].s); // return ; // } // output(cur<<1); // output(cur<<1 | 1); //} int main(){ int T; scanf("%d",&T); for(int cas=1;cas<=T;cas++){ printf("Case %d:\n",cas); int n; char ch[10]; scanf("%d",&n); built(1,1,n); while(1){ scanf("%s",ch); if(!strcmp("Query",ch)){ int x,y; scanf("%d%d",&x,&y); printf("%d\n",sum(1,x,y)); } else if(!strcmp("Add",ch)){ int x,y; scanf("%d%d",&x,&y); insert(1,x,y); } else if(!strcmp("Sub",ch)){ int x,y; scanf("%d%d",&x,&y); insert(1,x,-y); } else break; } } return 0; }
标签:des style blog java color os strong 数据
原文地址:http://blog.csdn.net/my_acm/article/details/38112583