树状数组求调
查看原帖
树状数组求调
705229
mayijian楼主2022/8/24 13:00

0分代码:

#include<bits/stdc++.h>
using namespace std;
const int maxn=5e5+5;
int a[maxn];
int n,m;
int lowbit(int x){
	return x&-x;
}

int getsum(int x){
	int ans=0;
	while(x){
		ans+=a[x];
		x-=lowbit(x);
	}
	return ans;
}

void update(int pos, int x){
	while(pos<=n){
		a[pos]+=x;
		pos+=lowbit(x);
	}
}

int main(){
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		int b;
		cin>>b;
		update(i, b);
	}
	while(m--){
		int x;
		cin>>x;
		if(x==1){
			int x,k;
			cin>>x>>k;
			update(x,k);
		}
		else{
			int x,y;
			cin>>x>>y;
			cout<<getsum(y)-getsum(x-1)<<endl;
		}
	}
}
2022/8/24 13:00
加载中...