单点更新裸题
#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<cmath>
#include<algorithm>
#include<string>
#include<string.h>
#include<set>
#include<queue>
#include<stack>
#include<vector>
#include<functional>
#include<map>
using namespace std;
const int mxn = 50000 + 10;
struct node {
int l, r, maxn, sum;//存储最大值与和
}tree[mxn<<2];
int t, n;
char str[10];
int x, y;
int a[mxn];
void push_up(int rt)
{
tree[rt].sum=tree[rt<<1].sum+tree[rt<<1|1].sum;
}
void build(int rt,int l,int r)
{
tree[rt].l=l;
tree[rt].r=r;
if(l==r)
{
tree[rt].sum=a[l];
return ;
}
int mid=(l+r)>>1;
build(rt<<1,l,mid);
build(rt<<1|1,mid+1,r);
push_up(rt);
}
void update(int rt,int L,int R,int a,int val)
{
if(L==a&&L==R)
{
tree[rt].sum+=val;
return ;
}
int mid=(L+R)>>1;
if(a<=mid) update(rt<<1,L,mid,a,val);
else update(rt<<1|1,mid+1,R,a,val);
push_up(rt);
}
int query(int rt,int L,int R,int l,int r)
{
if(l<=L&&r>=R) return tree[rt].sum;
int mid=(L+R)>>1;
if(r<=mid) return query(rt<<1,L,mid,l,r);
else if(l>mid) return query(rt<<1|1,mid+1,R,l,r);
else return query(rt<<1,L,mid,l,mid)+query(rt<<1|1,mid+1,R,mid+1,r);
}
int main()
{
int k = 0;
scanf("%d", &t);
while (t--)
{
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
printf("Case %d:\n", ++k);
build(1, 1, n);
while (scanf("%s", str))
{
if (str[0] == 'E') break;
if (str[0] == 'A') {
scanf("%d %d", &x, &y);
update(1,1,n,x, y);
}
if (str[0] == 'S') {
scanf("%d %d", &x, &y);
update(1, 1,n,x, -y);
}
if (str[0] == 'Q') {
scanf("%d %d", &x, &y);
printf("%d\n", query(1, 1,n,x, y));
}
}
}
//system("pause");
return 0;
}