#include <iostream>
#include <cstdio>
#include <cmath>
#include <stack>
#define ll long long
using namespace std;
const int N=5e5+5;
int a[N],n,m,b[N];
int lowbit(int x)//x的管辖范围
{
return x&(-x);
}
void update(int i,int k)//在i下标上加k
{
while(i<=n)
{
b[i]+=k;
i+=lowbit(i);
}
}
ll query(int i)//求【1,i】的值
{
ll res=0;
while(i>0)
{
res+=b[i];
i-=lowbit(i);
}
return res;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int k,x,y;
cin >> n >> m;
for(int i=1;i<=n;i++)
{
cin >> a[i];
update(i,a[i]);
}
while(m--)
{
cin >> k >> x >> y;
if(k==1)
{
update(x,y);
}
else
{
cout << query(y)-query(x-1) << endl;
}
}
return 0;
}
树状数组模板 P3374(洛谷)
最新推荐文章于 2024-08-11 22:07:33 发布