又爆0了
查看原帖
又爆0了
498531
jianglige楼主2021/12/25 22:51

rt,写了一晚上之后写炸了,好伤心

#include<iostream>
#include<algorithm>
using namespace std;
inline int read(){
	char c;int f=1,res=0;
	while(c=getchar(),!isdigit(c)) if(c=='-') f*=-1;
	while(isdigit(c)) res=res*10+c-'0',c=getchar();
	return res*f;
}
const int maxn=1e6+5;
int n,m,a[maxn];

int tree[maxn],tag[maxn];
void build(int x,int l,int r){
	if(l==r){
		a[l]=tree[x];
		return;
	}
	int mid=l+(r-l)/2;
	build(x<<1,l,mid);
	build(x<<1|1,mid+1,r);
	tree[x]=tree[x*2]+tree[x*2+1];
}
void modify(int p,int l,int r,int val,int s,int t){//l,r是指添加的对象,s,t是指当前的区间,x是指当前树的编号 
     if(l<=s&&t<=r){                               //表示当前区间是被查询区间所包含 
     	tree[p]+=val*(s-t+1),tag[p]+=val;
     	return;
	 }
     int mid=s+(t-s)/2;
     if(tag[p]&&s!=t){
     	tag[p*2]+=tag[p];
     	tag[p*2+1]=tag[p];
     	tree[p*2]+=(mid-s+1)*tag[p];
     	tree[p*2+1]+=(t-mid)*tag[p];
     	tag[p]=0;
	 }
	 if(l<=m) modify(p*2,l,r,val,s,mid);
	 if(r>m) modify(p*2+1,l,r,val,mid+1,t);
	 tree[p]=tree[p*2]+tree[p*2+1];
}
int getsum(int l,int r,int s,int t,int p){
	int sum=0;
	if(l<=s&&r>=t) return tree[p];
	int mid=s+(t-s)/2;
	if(l<=mid) sum+=getsum(l,r,s,mid,p*2);
	if(r>mid) sum+=getsum(l,r,mid+1,t,p*2+1);
	if(tag[p]){
	    tag[p*2]+=tag[p];
     	tag[p*2+1]=tag[p];
     	tree[p*2]+=(mid-s+1)*tag[p];
     	tree[p*2+1]+=(t-mid)*tag[p];
     	tag[p]=0;
	}
	return sum;
}

int main(){
	n=read();
	m=read();
	for(int i=1;i<=n;i++){
		a[i]=read();
		build(1,1,n);
	}
	for(int i=1;i<=m;i++){
		int d;
		d=read();
		if(d==1){
		  int x,y,k;
		  x=read();
		  y=read();
		  k=read();
		  modify(1,x,y,k,1,n);	
		}
		if(d==2){
			int x,y;
			x=read();
			y=read();
			cout<<getsum(x,y,1,n,1)<<endl;
		}
	}
	return 0;
}
2021/12/25 22:51
加载中...