60分线段树求调/hack
查看原帖
60分线段树求调/hack
514727
bcdmwSjy楼主2023/10/18 09:49
#include<bits/stdc++.h>
using namespace std;
#define ls (i<<1)
#define rs (i<<1|1)
typedef long long ll;
const ll inf=0x3f3f3f3f3f3f3f3fll;
struct Tree {
	ll l,r,len,maxn,add,cover;
};
Tree tr[4000001];
ll a[1000001];
ll n,m;
void build(ll i,ll l,ll r) {
	tr[i].l=l;
	tr[i].r=r;
	tr[i].len=r-l+1;
	tr[i].cover=-inf;
	tr[i].add=0;
	if (l==r) {
		tr[i].maxn=a[l];
		return;
	}
	ll mid=(l+r)>>1;
	build(ls,l,mid);
	build(rs,mid+1,r);
	tr[i].maxn=max(tr[ls].maxn,tr[rs].maxn);
}
void c_down(ll i){
	if (tr[i].cover!=-inf){
		tr[ls].add=tr[rs].add=0;
		tr[ls].maxn=tr[ls].maxn=tr[i].maxn;
		tr[ls].cover=tr[rs].cover=tr[i].cover;
		tr[i].cover=-inf;
	}
}
void a_down(ll i){
	if (tr[i].add){
		c_down(i);
		tr[ls].maxn+=tr[i].add;
		tr[rs].maxn+=tr[i].add;
		tr[ls].add+=tr[i].add;
		tr[rs].add+=tr[i].add;
		tr[i].add=0;
	}
}
void pushdown(ll i) {
	c_down(i);
	a_down(i);
}
void cover(ll i,ll l,ll r,ll k) {
	if (tr[i].r<l or tr[i].l>r) return;
	if (tr[i].l>=l and tr[i].r<=r) {
		tr[i].maxn=k;
		tr[i].cover=k;
		tr[i].add=0;
		return;
	}
	pushdown(i);
	if (tr[ls].r>=l) cover(ls,l,r,k);
	if (tr[rs].l<=r) cover(rs,l,r,k);
	tr[i].maxn=max(tr[ls].maxn,tr[rs].maxn);
}
void add(ll i,ll l,ll r,ll k) {
	if (tr[i].r<l or tr[i].l>r) return;
	if (tr[i].l>=l and tr[i].r<=r) {
		tr[i].maxn+=k;
		tr[i].add+=k;
		return;
	}
	pushdown(i);
	if (tr[ls].r>=l) add(ls,l,r,k);
	if (tr[rs].l<=r) add(rs,l,r,k);
	tr[i].maxn=max(tr[ls].maxn,tr[rs].maxn);
}
ll query(ll i,ll l,ll r) {
	if (tr[i].r<l or tr[i].l>r) return -inf;
	if (tr[i].l>=l and tr[i].r<=r) return tr[i].maxn;
	pushdown(i);
	ll s=-inf;
	if (tr[ls].r>=l) s=max(s,query(ls,l,r));
	if (tr[rs].l<=r) s=max(s,query(rs,l,r));
	return s;
}
int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin>>n>>m;
	for (ll i=1; i<=n; i++) cin>>a[i];
	build(1,1,n);
	for (ll i=0; i<m; i++) {
		ll op,x,y,k;
		cin>>op>>x>>y;
		if (op==1) {
			cin>>k;
			cover(1,x,y,k);
		} else if (op==2) {
			cin>>k;
			add(1,x,y,k);
		} else if (op==3) {
			cout<<query(1,x,y)<<"\n";
		}
	}
	return 0;
}
2023/10/18 09:49
加载中...