蒟蒻刚学OI1ms线段树样例不过代码求调
查看原帖
蒟蒻刚学OI1ms线段树样例不过代码求调
917683
rnf5114楼主2023/12/17 23:40

悬关

自学写的代码,个人感觉思路没问题 (qwq和awa数组单纯是为了调试用的)

#include <iostream>
using namespace std;
int m,n,q,a[400010],t1[400010],t2[400010],yuan[400010],qwq[400010],awa[400010];
void apply(int p,int l,int r,int k1,int k2){
	if(k1==1){
		a[p]+=(r-l+1)*k2;
		t2[p]+=k2;
	}
	else if(k2==0){
		a[p]*=k1;
		t1[p]*=k1;
		t2[p]*=k1;
	}
	return ;
}
void pushdown(int p,int l,int r){
	apply(2*p,l,(l+r)/2,t1[p],t2[p]);
	apply(2*p+1,(l+r)/2+1,r,t1[p],t2[p]);
	t1[p]=1;
	t2[p]=0;
	return ;
}
void pushup(int p){
	a[p]=a[p*2]+a[p*2+1];
	return ;
}
int build(int l,int r,int p){
	qwq[p]=l;
	awa[p]=r;
	if(l==r){
		a[p]=yuan[l];
		return a[p];
	}
	build(l,(l+r)/2,p*2);
	build((l+r)/2+1,r,p*2+1);
	pushup(p);
}
void jia(int p,int l,int r,int ql,int qr,int k){
	//cout<<"左边界="<<l<<" 右边界="<<r<<endl;
	if(l>qr||r<ql)
		return;
	if(ql<=l&&qr>=r){
		//cout<<1<<endl;
		apply(p,l,r,1,k);
		return ;
	}
	pushdown(p,l,r);
	jia(2*p,l,(l+r)/2,ql,qr,k);
	jia(2*p+1,(l+r)/2+1,r,ql,qr,k);
	pushup(p);
	return ;
}
void cheng(int p,int l,int r,int ql,int qr,int k){
	if(l>qr||r<ql)
		return;
	if(ql<=l&&qr>=r){
		apply(p,l,r,k,0);
		return ;
	}
	pushdown(p,l,r);
	cheng(2*p,l,(l+r)/2,ql,qr,k);
	cheng(2*p+1,(l+r)/2+1,r,ql,qr,k);
	pushup(p);
	return ;
}
int an(int p,int l,int r,int ql,int qr){
	if(l>qr||r<ql)
		return 0;
	if(l>=qr&&r<=ql)
		return a[p];
	if(l==r)
		return a[l];
	pushdown(p,l,r);
	return an(2*p,l,(l+r)/2,ql,qr)+an(2*p+1,(l+r)/2+1,r,ql,qr);
}
int main(){
	cin>>n>>q>>m;
	for(int i=1;i<=n;i++)
		cin>>yuan[i];
	for(int i=0;i<=400005;i++)
		t1[i]=1;
	build(1,n,1);
	while(q--){
		int op,x,y,zhi;
		cin>>op;
		if(op==2){
			cin>>x>>y>>zhi;
			jia(1,1,n,x,y,zhi);
		}
		else if(op==1){
			cin>>x>>y>>zhi;
			cheng(1,1,n,x,y,zhi);
		}
		else{
			cin>>x>>y;
			cout<<an(1,1,n,x,y)<<endl;
		}
		/*cout<<"!!!!!!";
		for(int i=1;i<=20;i++)
			cout<<"左边界="<<qwq[i]<<" 右边界="<<awa[i]<<" 和为"<<a[i]<<endl;
		cout<<endl;*/
	}
    return 0;
}


/*
       ┏┓   ┏┓
     ┏┛┻━━━┛┻┓
     ┃       ┃
     ┃   ━   ┃
     ┃ ┳┛ ┗┳ ┃
     ┃       ┃
     ┃   ┻   ┃
     ┃       ┃
     ┗━┓   ┏━┛Codes are far away from bugs with the animal protecting
         ┃   ┃    神兽保佑,代码无bug
         ┃   ┃
         ┃   ┗━━━┓
         ┃      ┣┓
         ┃     ┏┛
         ┗┓┓┏━┳┓┏┛
           ┃┫┫ ┃┫┫
           ┗┻┛ ┗┻┛     ○| ̄|_

*/

2023/12/17 23:40
加载中...