用树状数组写的,求助+2关
查看原帖
用树状数组写的,求助+2关
755911
Ethereal_KQ楼主2024/7/26 15:57
#include<bits/stdc++.h>
using namespace std;
#define N 5444231
int t1[N],t2[N],n,m;
int lowbit(int x){return x& -x;}
void add1(int x,int d){
	while(x<=n)
		t1[x]+=d,
			x+=lowbit(x);
}
void add2(int x,int d){
	while(x<=n)
		t2[x]+=d,
			x+=lowbit(x);
}
int cx1(int x){
	int res=0;
	while(x>0)
		res+=t1[x],
			x-=lowbit(x);
	return res;
}
int cx2(int x){
	int res=0;
	while(x>0)
		res+=t2[x],
			x-=lowbit(x);
	return res;
}
int main(){
	cin>>n>>m;
	int temp=0,a;
	for(int i=1;i<=n;i++){
		cin>>a;
		add1(i,a-temp);
		add2(i,(i-1)*(a-temp));
		temp=a;
	}
	int op,l,r,d;
	while(m--){
		cin>>op;
		if(op==1){
			cin>>l>>r>>d;
			add1(l,d);
			add1(r+1,-d);
			add2(l,d*(l-1));
			add2(r+1,-d*r);
		}
		else{
			cin>>l>>r;
			cout<<r*cx1(r)-cx2(r)-(l-1)*cx1(l-1)-cx2(l-1)<<"\n";
		}
	}
	return 0;
}
2024/7/26 15:57
加载中...