hdu1166(线段树)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1166
线段树功能:update:单点增减 query:区间求和
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#pragma comment(linker,"/STACK:102400000,102400000") #include <cstdio> #include <cstring> #include <string> #include <cmath> #include <iostream> #include <algorithm> #include <queue> #include <cstdlib> #include <stack> #include <vector> #include <set> #include <map> #define LL long long #define mod 1000000007 #define inf 0x3f3f3f3f #define N 50010 #define FILL(a,b) (memset(a,b,sizeof(a))) #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 using namespace std; int sum[N<<2]; int a[N]; void Pushup(int rt) { sum[rt]=sum[rt<<1]+sum[rt<<1|1]; } void build(int l,int r,int rt) { if(l==r) { sum[rt]=a[l]; return; } int m=(l+r)>>1; build(lson); build(rson); Pushup(rt); } void update(int pos,int num,int l,int r,int rt) { if(l==r) { sum[rt]+=num; return; } int m=(l+r)>>1; if(pos<=m)update(pos,num,lson); else update(pos,num,rson); Pushup(rt); } int query(int L,int R,int l,int r,int rt) { if(L<=l&&r<=R) { return sum[rt]; } int m=(l+r)>>1; int res=0; if(L<=m)res+=query(L,R,lson); if(m<R)res+=query(L,R,rson); return res; } int main() { int t,n,cas=1; char op[10]; scanf("%d",&t); while(t--) { scanf("%d",&n); for(int i=1;i<=n;i++)scanf("%d",&a[i]); build(1,n,1);//printf("%d ",sum[20]); printf("Case %d:\n",cas++); while(scanf("%s",op)>0) { if(op[0]=='E')break; int a,b; scanf("%d%d",&a,&b); if(op[0]=='Q') printf("%d\n",query(a,b,1,n,1)); else if(op[0]=='A') update(a,b,1,n,1); else update(a,-b,1,n,1); } } }