50pts求助
查看原帖
50pts求助
996206
zcs_楼主2024/12/8 16:59
#include <bits/stdc++.h>
using namespace std;
int n,m,a[1001000];
struct node{
	long long l,r,add,maxx=-1e18,modify=-1e18;
}tree[4001000];
void pushdown(int p){
	tree[p].maxx=max(tree[p*2].maxx,tree[p*2+1].maxx);
}
void build(int p,int l,int r){
	tree[p].l=l,tree[p].r=r; 
	if (l==r){
		tree[p].maxx=a[l];
		return;
	}
	long long mid=(tree[p].l+tree[p].r)/2;
	build(p*2,l,mid);
	build(p*2+1,mid+1,r);
	pushdown(p);
}
void down(int p){
	long long d=tree[p].add,g=tree[p].modify;
	if (d>-1e18){
		tree[p*2].add+=d;
		tree[p*2+1].add+=d;
	}
	else{
		tree[p*2].maxx=g;
		tree[p*2+1].maxx=g;
		tree[p*2].add=d;
		tree[p*2+1].add=d;
		tree[p*2].modify=g;
		tree[p*2+1].modify=g;
		tree[p].modify=-1e18;
	}
	tree[p*2].maxx+=d;
	tree[p*2+1].maxx+=d;
	tree[p].add=0;
}
void update(int p,int l,int r,int k){
	if (tree[p].l>=l && tree[p].r<=r){
		tree[p].add+=k;
		tree[p].maxx+=k;
		return;
	}
	down(p);
	long long mid=(tree[p].l+tree[p].r)/2;
	if (l<=mid)update(p*2,l,r,k);
	if (r>mid)update(p*2+1,l,r,k);
	pushdown(p);
}
void update1(int p,int l,int r,int k){
	if (tree[p].l>=l && tree[p].r<=r){
		tree[p].modify=k,tree[p].add=0,tree[p].maxx=k;
		return;
	}
	down(p);
	long long mid=(tree[p].l+tree[p].r)/2;
	if (l<=mid)update1(p*2,l,r,k);
	if (r>mid)update1(p*2+1,l,r,k);
	pushdown(p);
}
long long ask(int p,int l,int r){
	if (tree[p].l>=l && tree[p].r<=r){
		return tree[p].maxx;
	}
	down(p);
	long long mid=(tree[p].l+tree[p].r)/2;
	long long maxxx=-1e18;
	if (l<=mid)maxxx=max(maxxx,ask(p*2,l,r));
	if (r>mid)maxxx=max(maxxx,ask(p*2+1,l,r));
	return maxxx;
}
signed main(){
	cin>>n>>m;
	for (int i=1;i<=n;i++)cin>>a[i];
	build(1,1,n);
	while(m--){
		int op,l,r,k;
		cin>>op>>l>>r;
		if (op==1){
			cin>>k;
			update1(1,l,r,k);
		}
		if (op==2){
			cin>>k;
			update(1,l,r,k);
		}
		if (op==3){
			cout<<ask(1,l,r)<<endl;
		}
	}
	return 0;
}
2024/12/8 16:59
加载中...