#pragma warning(disable:4786)
#include<iostream>
#include<algorithm>
#include<cmath>
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
#include<queue>
#include<set>
#include<vector>
#include<string>
#include<ctime>
#include<string.h>
using namespace std;
#define pi acos(-1.0)
//#define LL __int64
typedef long long LL;
#define INF 0x7fffffffffffffff
#define bug puts("hear!")
#define inf 0x7fffffff
#define eps 1e-10
#define E exp(1.0)
#define mod 1000000007
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
const int maxn=55555;
int sum[maxn<<2];
void pushup(int rt){
sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
void build(int l,int r,int rt){
if(l==r){
scanf("%d",&sum[rt]);
return ;
}
int mid=(l+r)>>1;
build(lson);
build(rson);
pushup(rt);
}
void update(int p,int k,int l,int r,int rt){
if(l==r){
sum[rt]+=k;
return ;
}
int mid=(l+r)>>1;
if(p <= mid)update(p,k,lson);
else update(p,k,rson);
pushup(rt);
}
LL query(int L,int R,int l,int r,int rt){
if(L <= l&&r <= R){
return sum[rt];
}
LL ret=0;
int mid=(l+r)>>1;
if(L <= mid) ret+=query(L,R,lson);
if(R > mid) ret+=query(L,R,rson);
return ret;
}
int main(){
int t;
int ll=1;
scanf("%d",&t);
while(t--){
printf("Case %d:\n",ll++);
int n;
scanf("%d",&n);
build(1,n,1);
char op[10];
while(scanf("%s",op)!=EOF&&op[0]!='E'){
int b,c;
scanf("%d %d",&b,&c);
if(op[0]=='A'){
update(b,c,1,n,1);
}
else
if(op[0]=='S'){
update(b,-c,1,n,1);
}
else
{
LL ans=query(b,c,1,n,1);
printf("%I64d\n",ans);
}
}
}
return 0;
}
【题目】:http://acm.hdu.edu.cn/showproblem.php?pid=1166
【题意】:给出初始序列,ADD表示在第i个地方加上多少个人,sub表示在第i个地方减去多少个人,query表示询问第i到j个营地一共多少人。END表示例子结束
【题解】:标准的线段树单点更新,然后需要更新父节点值, query直接进行区间求和。
【代码】: