[線分ツリー][CODEVS 1080]題解
1362 ワード
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<iostream>
#define inf 0x3f3f3f3f
#define LL unsigned long long
#define lson u<<1,l,mid
#define rson u<<1|1,mid+1,r
#define maxn 200005
using namespace std;
struct node{
int l,r;
int s;
}e[4*maxn+1];
int n,m;
void build(int u,int l,int r)
{
e[u].l=l;e[u].r=r;
if(l==r)
{
scanf("%d",&e[u].s);
return ;
}
int mid=(l+r)>>1;
build(lson);
build(rson);
e[u].s=e[u<<1].s+e[u<<1|1].s;
}
void update(int u,int l,int r,int x,int add)
{
if(l==r)
{
e[u].s+=add;
return ;
}
int mid = (l+r)>>1;
if(x<=mid)update(lson,x,add);
else update(rson,x,add);
e[u].s=e[u<<1].s+e[u<<1|1].s;
}
int query(int u,int l,int r,int x,int y)
{
if(l==x&&r==y)return e[u].s;
int mid=(l+r)>>1;
if(y<=mid)return query(lson,x,y);
else if(x>mid)return query(rson,x,y);
else return query(lson,x,mid)+query(rson,mid+1,y);
}
/******Program Begin*********/
int main()
{
scanf("%d",&n);
build(1,1,n);
scanf("%d",&m);
int k,x,y;
for(int i=1;i<=m;i++)
{
scanf("%d%d%d",&k,&x,&y);
if(k==1)update(1,1,n,x,y);
else if(k==2)printf("%d
",query(1,1,n,x,y));
}
return 0;
}
/*******Program End**********/