树状数组爆零 求助
查看原帖
树状数组爆零 求助
232507
OK咯莫名其妙楼主2021/7/20 16:57
#include<bits/stdc++.h>
using namespace std;
const int maxn=2e5+10;
long long n,f;
long long a[maxn],c[maxn],sum[maxn],sum1[maxn],mian;
int lowbit(int x)
{
	return x&(-x);
}
 void add(int pos,int x)
{
	for(int i=pos;i<=n;i+=lowbit(i))
		sum[i]+=x,sum1[i]+=pos*x;
}
long long query(int pos)
{
	long long res=0;
	for(int i=pos;i;i-=lowbit(i))
		res+=(pos+1)*sum[i]-sum1[i];
	return res;
} 
int main(){
	cin>>n>>f;
	int last,x;
	for(int i=1;i<=n;i++) {
		cin>>x;
		add(i,x-last),last=x;
	}
	
	for(int i=1;i<=f;i++){
		int q,w,e,r;
		cin>>q;
		if(q==1){
			cin>>w>>e>>r;
			if(w==1||e==1){
				a[1]+=r;
			}
			add(w,r);
			add(e+1,-r);
		}
		if(q==2){
			cin>>w;
			a[1]+=w;
			mian+=w;
		}
		if(q==3){
			cin>>w;
			a[1]-=w;
			mian-=w;
		}
		if(q==4){
			cin>>w>>e;
			cout<<query(e)-query(w-1)+(w==1)*mian<<endl;
		//	cout<<a[1]<<endl; 
		}
		if(q==5){
			cout<<a[1]<<endl;
		}
	}
	//for(int i=1;i<=n;i++)
	//	cout<<a[i]<<" ";
	return 0;
}
2021/7/20 16:57
加载中...