60分求助+悬关
查看原帖
60分求助+悬关
529977
Soaring_light楼主2023/10/25 20:13

smodify是加修改

cmodify是赋值修改

请路过的大佬帮忙改改把

#include<bits/stdc++.h>
#define ls x<<1
#define rs x<<1|1
#define int long long
using namespace std;
typedef long long ll;
int n,q,op,lk,rk,xk,a[1000005];
const int inf = 1e18+1;
struct node{
	int tot,cl,cr,stag,ctag;
}tree[2000006];
void build(ll x,ll l,ll r){
	tree[x].cl = l,tree[x].cr = r;
	tree[x].ctag = -inf,tree[x].stag = 0;
	if(l == r){
		tree[x].tot = a[l];return;
	}
	ll mid = (tree[x].cl + tree[x].cr)>>1;
	build(ls,l,mid);
	build(rs,mid+1,r);
	tree[x].tot = max(tree[ls].tot,tree[rs].tot);
	return;
}
void Cpushdown(ll x){
	if(tree[x].ctag != -inf){
		tree[ls].stag = tree[rs].stag = tree[x].stag = 0;
		tree[ls].tot = tree[rs].tot = tree[x].ctag;
		tree[ls].ctag = tree[rs].ctag = tree[x].ctag;
	}
	tree[x].ctag = -inf;
}
void Spushdown(ll x){
	if(tree[x].stag){
		Cpushdown(x);
		tree[ls].stag += tree[x].stag;
		tree[rs].stag += tree[x].stag;
		tree[ls].tot += tree[x].stag;
		tree[rs].tot += tree[x].stag;
	}
	tree[x].stag = 0;
}
void Smodify(ll x,ll l,ll r,ll num){
	if(tree[x].cl > r || tree[x].cr < l)return;
	if(tree[x].cl >= l && tree[x].cr <= r){
		tree[x].tot += num;tree[x].stag += num;
		return;
	}
	Spushdown(x);Cpushdown(x);
	int mid = (tree[x].cl + tree[x].cr)>>1;
	if(mid >= l)Smodify(ls,l,r,num);
	if(mid < r)Smodify(rs,l,r,num);
	tree[x].tot = max(tree[ls].tot,tree[rs].tot);
	return;
}
void Cmodify(ll x,ll l,ll r,ll num){
	if(tree[x].cl > r || tree[x].cr < l)return;
	if(tree[x].cl >= l && tree[x].cr <= r){
		tree[x].tot = num;tree[x].ctag = num;
		return;
	}
	Spushdown(x);Cpushdown(x);
	ll mid = (tree[x].cl + tree[x].cr)>>1;
	if(mid >= l)Cmodify(ls,l,r,num);
	if(mid < r)Cmodify(rs,l,r,num);
	tree[x].tot = max(tree[ls].tot , tree[rs].tot);
	return;
}
int Query(ll x,ll l,ll r){
	if(tree[x].cl >= l && tree[x].cr <= r){
		return tree[x].tot;
	}
	Cpushdown(x),Spushdown(x);
	int ans = -inf,mid = (tree[x].cl + tree[x].cr)>>1;
	if(mid >= l)ans = max(ans,Query(ls,l,r));
	if(mid < r)ans = max(ans,Query(rs,l,r));
	return ans;
}
signed main(int argc, char const *argv[]){
//	freopen("P1253_7.in","r",stdin);
//	freopen("anrs.out","w",stdout);
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	cin>>n>>q;
	for(int i = 1;i <= n;i++)
		cin>>a[i];
	build(1,1,n);
	while(q--){
		cin>>op>>lk>>rk;
		if(op == 1){
			cin>>xk;
			Cmodify(1,lk,rk,xk);
		}
		if(op == 2){
			cin>>xk;
			Smodify(1,lk,rk,xk);
		}
		if(op == 3)
			cout<<Query(1,lk,rk)<<endl;
	}
	return 0;
}
2023/10/25 20:13
加载中...