标签:hdoj 敌兵布阵
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<cstdlib> #include<cstring> #define lson l,mid,rr<<1 #define rson mid+1,r,rr<<1|1 using namespace std; const int MAX=50005; int SUM; struct Node{ int sum; }tree[MAX<<2]; void build(int l,int r,int rr){ if(l==r){ scanf("%d",&tree[rr].sum); return; } int mid=(l+r)>>1; build(lson); build(rson); tree[rr].sum=tree[rr<<1].sum+tree[rr<<1|1].sum; } void query(int left,int right,int l,int r,int rr){ if(left==l&&right==r){ SUM+=tree[rr].sum; return; } int mid=(r+l)>>1; if(right<=mid)query(left,right,lson); else if(left>mid)query(left,right,rson); else { query(left,mid,lson); query(mid+1,right,rson); } } void add(int left,int right,int q,int l,int r,int rr){ if(left==l&&right==r){ tree[rr].sum+=q; return; } int mid=(r+l)>>1; if(right<=mid){ add(left,right,q,lson); tree[rr].sum=tree[rr<<1].sum+tree[rr<<1|1].sum; } else if(left>mid){ add(left,right,q,rson); tree[rr].sum=tree[rr<<1].sum+tree[rr<<1|1].sum; } else { add(left,mid,q,lson); add(mid+1,right,q,rson); tree[rr].sum=tree[rr<<1].sum+tree[rr<<1|1].sum; } } void sub(int left,int right,int q,int l,int r,int rr){ if(right==r&&left==l){ tree[rr].sum-=q; return; } int mid=(r+l)>>1; if(right<=mid){ sub(left,right,q,lson); tree[rr].sum=tree[rr<<1].sum+tree[rr<<1|1].sum; } else if(left>mid){ sub(left,right,q,rson); tree[rr].sum=tree[rr<<1].sum+tree[rr<<1|1].sum; } else { sub(left,mid,q,lson); sub(mid+1,right,q,rson); tree[rr].sum=tree[rr<<1].sum+tree[rr<<1|1].sum; } } int main() { int k=1,t,i,j,n; char str[10]; scanf("%d",&t); while(t--){ scanf("%d",&n); build(1,n,1); printf("Case %d:\n",k++); while(1){ scanf("%s",str); if(strcmp(str,"Query")==0){ int a,b;SUM=0; scanf("%d%d",&a,&b); query(a,b,1,n,1); printf("%d\n",SUM); } else if(strcmp(str,"Add")==0){ int a,b; scanf("%d%d",&a,&b); add(a,a,b,1,n,1); } else if(strcmp(str,"Sub")==0){ int a,b; scanf("%d%d",&a,&b); sub(a,a,b,1,n,1); } else if(strcmp(str,"End")==0)break; } } return 0; }树状数组#include<cstdio> #include<cstdlib> #include<cstring> using namespace std; const int MAX=50005; int count[MAX],c[MAX],N,M; int low(int n){ return n&(-n); } int sum(int n){ int value=0; while(n!=0){ value+=c[n]; n-=low(n); } return value; } void add(int p,int q){ while(p<=N){ c[p]+=q; p+=low(p); } } void sub(int p,int q){ while(p<=N){ c[p]-=q; p+=low(p); } } int main() { int p=1,t,k,i,j,q; char str[10]; scanf("%d",&t); while(t--){ scanf("%d",&N); for(i=1;i<=N;++i){ scanf("%d",&count[i]); c[i]=0; } for(i=1;i<=N;++i){ k=low(i);q=i-k; while(i>q){ q+=1; c[i]+=count[q]; } } printf("Case %d:\n",p++); while(1){ scanf("%s",str); if(strcmp(str,"Query")==0){ int a;int b; scanf("%d%d",&a,&b); printf("%d\n",sum(b)-sum(a-1)); } else if(strcmp(str,"Add")==0){ int a,b; scanf("%d%d",&a,&b); add(a,b); } else if(strcmp(str,"Sub")==0){ int a;int b; scanf("%d%d",&a,&b); sub(a,b); } else if(strcmp(str,"End")==0)break; } } return 0; }
标签:hdoj 敌兵布阵
原文地址:http://blog.csdn.net/r1986799047/article/details/44760897