题目链接:https://www.luogu.org/problem/P3374
模板题:
/*
洛谷 P3374 【模板】树状数组 1
n个数,m个查询,a1....an。查询有两个操作
操作1: 格式:1 x k 含义:将第x个数加上k
操作2: 格式:2 x y 含义:输出区间[x,y]内每个数的和
5 5
1 5 4 2 3
1 1 3
2 2 5
1 3 -1
1 4 2
2 1 4
ans:
14
16
*/
#include<string.h>
#include<algorithm>
#include<stdio.h>
using namespace std;
int n,m;
int tem[500010];
int lowbit(int x)
{
return x&(-x);
}
int add(int pos,int x)
{
while(pos<=n)
{
tem[pos]+=x;
pos+=lowbit(pos);
}
}
int sum(int pos)
{
int ans=0;
while(pos>0)
{
ans+=tem[pos];
pos-=lowbit(pos);
}
return ans;
}
int main()
{
int x,opt,l,r;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{
scanf("%d",&x);
add(i,x);
}
while(m--)
{
scanf("%d%d%d",&opt,&l,&r);
if(opt==1) add(l,r);
else printf("%d\n",sum(r)-sum(l-1));
}
}