萌新刚学线段树,照着深进打了份模板,50pts求调,玄关
查看原帖
萌新刚学线段树,照着深进打了份模板,50pts求调,玄关
654577
RainySoul楼主2023/7/20 09:50
#include<bits/stdc++.h>
#define int long long
#define inf 1000000000000000000ll
using namespace std;
const int maxn=1000000;
int a[maxn],w[maxn*4],lzy_set[maxn*4],lzy_add[maxn*4];
void pushup(const int u){
	w[u]=max(w[u*2],w[u*2+1]);
}
void build(int u,int L,int R){
	if(L==R){
		w[u]=a[L];
		return;
	}
	int M=(L+R)/2;
	build(u*2,L,M);
	build(u*2+1,M+1,R);
	pushup(u);
	lzy_set[u]=inf;
}
bool InRange(int L,int R,int l,int r){
	return (l<=L)&&(R<=r);
}
bool OutofRange(int L,int R,int l,int r){
	return (L>r)||(R<l);
}
void maketag(int u,int x,int type){
	if(type==1){
		lzy_add[u]=0;
		lzy_set[u]=x;
		w[u]=x;
	}
	else{
		if(lzy_set[u]==inf)lzy_add[u]+=x;
		else lzy_set[u]+=x;
		w[u]+=x;
	}
} 
void pushdown(int u){
	if(lzy_set[u]==inf){
		maketag(u*2,lzy_add[u],2);
		maketag(u*2+1,lzy_add[u],2);
		lzy_add[u]=0;
	}
	else{
		maketag(u*2,lzy_set[u],1);
		maketag(u*2+1,lzy_set[u],1);
		lzy_set[u]=inf;
	}
}
int query(int u,int L,int R,int l,int r){
	if(InRange(L,R,l,r)){
		return w[u];
	}
	else if(!OutofRange(L,R,l,r)){
		int M=(L+R)/2;
		pushdown(u);
		return max(query(u*2,L,M,l,r),query(u*2+1,M+1,R,l,r));
	}
	else return 0;
}
void update(int u,int L,int R,int l,int r,int x,int type){
	if(InRange(L,R,l,r)){
		maketag(u,x,type);
	}
	else if(!OutofRange(L,R,l,r)){
		int M=(L+R)/2;
		pushdown(u);
		update(u*2,L,M,l,r,x,type);
		update(u*2+1,M+1,R,l,r,x,type);
		pushup(u);
	}
}
signed main(){
    int n,q;
	cin>>n>>q;
	for(int i=1;i<=n;i++)cin>>a[i];
	build(1,1,n);
	for(int t=1;t<=q;t++){
		int op,x,y,k;
		cin>>op;
		if(op==1||op==2){
			cin>>x>>y>>k;
			update(1,1,n,x,y,k,op);
		}
		else{
			cin>>x>>y;
			cout<<query(1,1,n,x,y)<<'\n';
		}
	}
    return 0;
}
2023/7/20 09:50
加载中...