题目:http://acm.hdu.edu.cn/showproblem.php?pid=1166
题意:
给出营地的总数和各个营地初始人数,通过对某个营地 add(增加人数)或sub(减少人数)操作
求第a个——第b个营地范围内的人数总和
可以采用线段树也可以是树状数组
树状数组:
#include <iostream> #include "stdio.h" #include "stdlib.h" #define maxn 50005 using namespace std; int c[maxn]={0}; int lowbit(int x){return x&(-x);} void update(int target,int change,int n) { while(target<=n) { c[target]+=change; target+=lowbit(target); } } int sum(int x) { int ans=0; while(x>0) { ans+=c[x]; x-=lowbit(x); } return ans; } int main() { int t,deap=0; scanf("%d",&t); while(t--) { int n; memset(c,0,sizeof(c)); char temp[10]; scanf("%d",&n); for(int i=1;i<=n;i++) { int tmp; scanf("%d",&tmp); update(i,tmp,n); } printf("Case %d:\n",++deap); while(scanf("%s",temp)!=EOF) { int a,b; if(temp[0]=='E') break; else if(temp[0]=='Q') { scanf("%d%d",&a,&b); printf("%d\n",sum(b)-sum(a-1)); } else{ scanf("%d%d",&a,&b); if(temp[0]=='S') b=b*(-1); update(a,b,n); } } } return 0; }
线段树:
#include <iostream> #include "stdio.h" #include "stdlib.h" #define maxn 50005 using namespace std; int sum[maxn<<2]={0}; void build(int k,int l,int r) { if(l==r) { sum[k]=0; return; } int mid=(l+r)>>1; build(k<<1,l,mid); build(k<<1|1,mid+1,r); } void update(int k,int left,int right,int pos,int val) { if(left==right) { sum[k]+=val; return; } int mid=(left+right)>>1; if(pos<=mid) update(k<<1,left,mid,pos,val); else update(k<<1|1,mid+1,right,pos,val); sum[k]=sum[k<<1|1]+sum[k<<1]; } int query(int k,int left,int right,int ll,int rr) { if(ll<=left&&rr>=right) return sum[k]; int mid=(left+right)>>1; int sum1=0,sum2=0; if(ll<=mid) sum1=query(k<<1,left,mid,ll,rr); if(rr>mid) sum2=query(k<<1|1,mid+1,right,ll,rr); return sum1+sum2; } int main() { int t,ans=0; scanf("%d",&t); while(t--) { int n; char temp[10]; scanf("%d",&n); build(1,1,n); for(int i=1;i<=n;i++) { int tmp; scanf("%d",&tmp); update(1,1,n,i,tmp); } printf("Case %d:\n",++ans); while(1) { scanf("%s",temp); if(temp[0]=='E') break; else if(temp[0]=='Q') { int a,b; scanf("%d%d",&a,&b); printf("%d\n",query(1,1,n,a,b)); } else{ int a,b; scanf("%d%d",&a,&b); if(temp[0]=='S') b=(-1)*b; update (1,1,n,a,b); } } } return 0; }