社区讨论

p1253 20pts 玄关

灌水区参与者 1已保存回复 0

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
0 条
当前快照
1 份
快照标识符
@m4c9wkqc
此快照首次捕获于
2024/12/06 12:56
去年
此快照最后确认于
2025/11/04 13:17
4 个月前
查看原帖
CPP
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,q,a[10010000],x,l,r;
struct node{
	int l,r,sum,mx,add;
}tree[40040000];
void build(int p,int l,int r){
	
	tree[p].l=l;
	tree[p].r=r;
	tree[p].mx=-1e18;
	if(l==r){
		tree[p].mx=a[l];
		return;
	}
	int mid=(l+r)/2;
	build(p*2,l,mid);
	build(p*2+1,mid+1,r);
	tree[p].mx=max(tree[p*2].mx,tree[p*2+1].mx);
}
void down(int p){
	if(tree[p].sum>-1e18){
		tree[p*2].mx=tree[p].sum;
		tree[p*2+1].mx=tree[p].sum;
		tree[p*2].sum=tree[p].sum;
		tree[p*2+1].sum=tree[p].sum;
		
		tree[p*2].add=0;
		tree[p].add=0;
    	tree[p*2+1].add=0;
        tree[p].sum=-1e18;
	}
	
		tree[p*2].add+=tree[p].add;
    tree[p*2+1].add+=tree[p].add;
	tree[p*2].mx+=tree[p].add;
	tree[p*2+1].mx+=tree[p].add;
	tree[p].add=0;
	tree[p].mx=max(tree[p*2].mx,tree[p*2+1].mx);

	
}
void xg(int p,int l,int r,int k){
	if(l<=tree[p].l&&tree[p].r<=r){
		tree[p].sum=k;
		tree[p].mx=k;
		tree[p].add=0;
		return;
	}
	down(p);
	int mid=(tree[p].l+tree[p].r)/2;
	if(l<=mid) xg(p*2,l,r,k);
	if(mid<r) xg(p*2+1,l,r,k);
	tree[p].mx=max(tree[p*2].mx,tree[p*2+1].mx);
}

void update(int p,int l,int r,int k){
	if(l<=tree[p].l&&tree[p].r<=r){
		tree[p].add+=k;
		tree[p].mx+=k;
		return;
	}
	down(p);
	int 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);
	tree[p].mx=max(tree[p*2].mx,tree[p*2+1].mx);
}
int ask(int p,int l,int r){
	if(tree[p].l>=l&&tree[p].r<=r){
		return tree[p].mx;
	}
	down(p);
	int mid=(tree[p].l+tree[p].r)/2;
	int ans=-1e18;
	if(l<=mid)ans=max(ask(p*2,l,r),ans);	
	if(r>mid)ans=max(ask(p*2+1,l,r),ans);
	return ans;
}
signed main(){
	scanf("%lld%lld",&n,&q);
	for(int i=1;i<=n;i++){
		scanf("%lld",&a[i]);
	}
	build(1,1,n);
	int op,x;
	while(q--){
		scanf("%lld%lld%lld",&op,&l,&r);
		if(op==1){
			scanf("%lld",&x);
			xg(1,l,r,x);
		}
		if(op==2){
			scanf("%lld",&x);
			update(1,l,r,x);
		}
		if(op==3){
			printf("%lld \n",ask(1,l,r));
		}
	}
	return 0;
}

回复

0 条回复,欢迎继续交流。

正在加载回复...