树状数组 求调
查看原帖
树状数组 求调
1294496
wu_18楼主2024/12/17 19:32
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,m,a[100005],t[100005];
void in(){
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		cin>>a[i];
	}
}
int lowbit(int x){
	return x&-x;
}
void add(int x,int k){
	while(x<=n){
		t[x]=t[x]+k;
		x=x+lowbit(x);
	}
}
void init(){
	for(int i=1;i<=n;++i){
		t[i]+=a[i];
		int j=i+lowbit(i);
		if(j<=n) t[j]+=t[i];
	}
}
int getsum(int x){
	int ans=0;
	while(x>0){
		ans=ans+t[x];
		x=x-lowbit(x);
	}
	return ans;
}
signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);
	in();
	init();
	for(int i=1;i<=m;i++){
		char c;
		int l,r,d;
		cin>>c;
		if(c=='1'){
			cin>>l>>r>>d;
			for(int i=l;i<=r;i++) add(i,d);
		}
		if(c=='2'){
			int sum=0;
			cin>>l>>r;
			for(int i=l;i<=r;i++){
				sum+=getsum(i);
			}
			cout<<getsum(r)-getsum(l-1)<<'\n';
		}
	}
	return 0;
}
2024/12/17 19:32
加载中...