跳到正文

剪贴板

剪贴板 vn3b8yjh

原文
剪贴板信息

剪贴板信息

收录
更新

树状数组查询/修改

C++
#include <iostream>
#include <cmath>
#include <cstring>
#include<cstdio>
#include<cstring>
#define N 500005
using namespace std;
int a[N],c[N];
int n,m,x,y;

inline int read()
{
    int x=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9'){
		if(ch=='-') f=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9'){
		x=x*10+ch-'0';
		ch=getchar();
	}
    return x*f;
}
inline void update(int x,int y){
	for(;x<=n;x+=x&(-x))
		c[x]+=y;
	return;
}
inline int s(int x){
	int ans=0;
	while(x>0){
		ans+=c[x];
		x-=x&(-x);
	}
	return ans;
}
int main()
{
	n=read();
	m=read();
	for(int i=1;i<=n;i++){
		scanf("%d",&y);
		update(i,y);
	}
	int f=0;
	for(int i=1;i<=m;i++){
		f=read();
		x=read();
		y=read();
		if(f==1) update(x,y);
		if(f==2) cout<<s(y)-s(x-1)<<endl;
	}
	return 0;
}