原题 传送门
分析:采用模板线段树
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
int n,m,a[500005],ans;
struct node
{
int left,right;
int sum;
}tree[2000020];//4*500000内存大小
void build(int left,int right,int index)
{
tree[index].left=left;
tree[index].right=right;
if(left==right)return;
int mid=(left+right)/2;
build(left,mid,2*index);
build(mid+1,right,2*index+1);
}
int input(int index)//维护
{
if(tree[index].left==tree[index].right)
{
return tree[index].sum=a[tree[index].left];
}
tree[index].sum=input(2*index)+input(2*index+1);
return tree[index].sum;
}
void search(int index,int l,int r)
{
if(tree[index].left>=l&&tree[index].right<=r)
{
ans+=tree[index].sum;
return ;
}
if(tree[2*index].right>=l)search(2*index,l,r);
if(tree[2*index+1].left<=r)search(2*index+1,l,r);
}
void correct(int index,int dis,int k)
{
tree[index].sum+=k;
if(tree[index].left==tree[index].right)return ;
if(dis<=tree[2*index].right)
correct(2*index,dis,k);
if(dis>=tree[2*index+1].left)
correct(2*index+1,dis,k);
}
int main()
{
ios::sync_with_stdio(false);
cin>>n>>m;
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
build(1,n,1);
input(1);
for(int i=1;i<=m;i++)
{
int a,b,c;
cin>>a>>b>>c;
if(a==1)
{
correct(1,b,c);
}
if(a==2)
{
ans=0;
search(1,b,c);
printf("%d\n",ans);
}
}
}