标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 69983 Accepted Submission(s): 29354
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <algorithm>
#include <set>
using namespace std;
typedef long long ll;
typedef unsigned long long Ull;
#define MM(a,b) memset(a,b,sizeof(a));
const double eps = 1e-10;
const int inf = 0x3f3f3f3f;
const double pi=acos(-1);
const int maxn=100000;
struct Tree{
int l,r;
ll maxn,sum;
}tree[4*maxn+5];
void build(int id,int l,int r)
{
tree[id].l=l;
tree[id].r=r;
if(r==l) scanf("%lld",&tree[id].sum);
else
{
int mid=(l+r)>>1;
build(2*id,l,mid);
build(2*id+1,mid+1,r);
tree[id].sum=tree[2*id].sum+tree[2*id+1].sum;
}
}
int query(int id,int l,int r)
{
if(l<=tree[id].l&&tree[id].r<=r)
return tree[id].sum;
else if(tree[id].r>=l&&tree[id].l<=r)
{
int x=query(2*id,l,r);
int y=query(2*id+1,l,r);
return x+y;
}
else return 0;
}
void update(int id,int pos,int val)
{
if(tree[id].l==tree[id].r) tree[id].sum+=val;
else{
int mid=(tree[id].l+tree[id].r)>>1;
if(pos<=mid) update(2*id,pos,val);
else update(2*id+1,pos,val);
tree[id].sum=tree[2*id].sum+tree[2*id+1].sum;
}
}
char s[14];
int main()
{
int cas;
scanf("%d",&cas);
for(int kk=1;kk<=cas;kk++)
{
int n;
scanf("%d",&n);
build(1,1,n);
printf("Case %d:\n",kk);
while(~scanf("%s",s))
{
if(s[0]==‘E‘) break;
int x,y;
scanf("%d %d",&x,&y);
if(s[0]==‘Q‘)
printf("%d\n",query(1,x,y));
else if(s[0]==‘A‘)
update(1,x,y);
else if(s[0]==‘S‘)
update(1,x,-y);
}
}
return 0;
}
标签:
原文地址:http://www.cnblogs.com/smilesundream/p/5423218.html