求区间和
题目链接:YbtOJ
解题思路
线段树模板题。
code
#include<iostream>
#include<cstdio>
#define int long long
using namespace std;
int n,m;
struct abc{
int s;
int x,y;
}tree[400010];
void build(int now,int l,int r)
{
tree[now].x=l;
tree[now].y=r;
if(l==r)
return;
int mid=(l+r)/2;
build(now*2,l,mid);
build(now*2+1,mid+1,r);
}
void add(int now,int a,int b)
{
tree[now].s+=b;
if(tree[now].x==tree[now].y)
return;
int mid=(tree[now].x+tree[now].y)/2;
if(a<=mid)
add(now*2,a,b);
else
add(now*2+1,a,b);
}
int fd(int now,int a,int b)
{
if(tree[now].x==a&&tree[now].y==b)
return tree[now].s;
int mid=(tree[now].x+tree[now].y)/2;
if(b<=mid)
return fd(now*2,a,b);
else if(a>mid)
return fd(now*2+1,a,b);
else
return fd(now*2,a,mid)+fd(now*2+1,mid+1,b);
}
signed main()
{
cin>>n>>m;
build(1,1,n);
while(m--)
{
int k,a,b;
scanf("%lld%lld%lld",&k,&a,&b);
if(!k)
add(1,a,b);
else
cout<<fd(1,a,b)<<endl;
}
}