求条玄关,区间历史最值
  • 板块P4314 CPU 监控
  • 楼主__Louis__
  • 当前回复2
  • 已保存回复2
  • 发布时间2024/11/22 18:36
  • 上次更新2024/11/22 20:21:42
查看原帖
求条玄关,区间历史最值
515836
__Louis__楼主2024/11/22 18:36
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define inf 0x3f3f3f3f3f3f3f
const int maxn=2e5+10;
struct node{
	int mx,hmx;
	int ad,_ad;
	int st,_st;
	node(int a=0,int b=-inf){
		ad=_ad=a;
		st=_st=b;
	}
}tree[maxn<<2];
int arr[maxn];
inline int ls(int x){
	return x<<1;
}
inline int rs(int x){
	return x<<1|1;
}
inline void push_up(int x){
	tree[x].mx=max(tree[ls(x)].mx,tree[rs(x)].mx);
	tree[x].hmx=max(tree[ls(x)].hmx,tree[rs(x)].hmx);
}
void build(int x,int l,int r){
	if(l==r){
		tree[x].hmx=tree[x].mx=arr[l];
		return;
	}
	int mid=(l+r)>>1;
	build(ls(x),l,mid);
	build(rs(x),mid+1,r);
	push_up(x); 
}
void add_ad(int x,int da,int _da){
	tree[x].hmx=max(tree[x].hmx,tree[x].mx+_da);
	tree[x].mx+=da;
	if(tree[x].st==-inf){
		tree[x]._ad=max(tree[x].ad+_da,tree[x]._ad);
		tree[x].ad+=da;
	}else{
		tree[x]._st=max(tree[x].st+_da,tree[x]._st);
		tree[x].st+=da; 
	}
}
void add_st(int x,int da,int _da){
	tree[x].hmx=max(tree[x].hmx,_da);
	tree[x].mx=da;
	tree[x]._st=max(tree[x]._st,_da);
	tree[x].st=da;
}
void push_down(int x){
	if(tree[x].ad||tree[x]._ad){
		add_ad(ls(x),tree[x].ad,tree[x]._ad);
		add_ad(rs(x),tree[x].ad,tree[x]._ad);
		tree[x].ad=tree[x]._ad=0;
	}
	if(tree[x].st!=-inf||tree[x]._st!=-inf){
		add_st(ls(x),tree[x].st,tree[x]._st);
		add_st(rs(x),tree[x].st,tree[x]._st);
		tree[x].st=tree[x]._st=-inf;
	}
} 
void insert_da(int x,int l,int r,int p,int q,int da){
	if(p<=l&&r<=q){
		add_ad(x,da,max(da,0ll));
		return;
	}
	push_down(x);
	int mid=(l+r)>>1;
	if(p<=mid) insert_da(ls(x),l,mid,p,q,da);
	if(q>mid) insert_da(rs(x),mid+1,r,p,q,da);
	push_up(x);
}
void insert_st(int x,int l,int r,int p,int q,int da){
	if(p<=l&&r<=q){
		add_st(x,da,da);
		return;
	}
	push_down(x);
	int mid=(l+r)>>1;
	if(p<=mid) insert_st(ls(x),l,mid,p,q,da);
	if(q>mid) insert_st(rs(x),mid+1,r,p,q,da);
	push_up(x);
}
int find(int x,int l,int r,int p,int q){
	if(p<=l&&r<=q){
		return tree[x].mx;
	} 
	push_down(x);
	int mid=(l+r)>>1;
	if(q<=mid) return find(ls(x),l,mid,p,q);
	else if(p>mid) return find(rs(x),mid+1,r,p,q);
	else return max(find(ls(x),l,mid,p,q),find(rs(x),mid+1,r,p,q));
}
int find_h(int x,int l,int r,int p,int q){
	if(p<=l&&r<=q){
		return tree[x].hmx;
	}
	push_down(x);
	int mid=(l+r)>>1;
	if(q<=mid) return find_h(ls(x),l,mid,p,q);
	else if(p>mid) return find_h(rs(x),mid+1,r,p,q);
	else return max(find_h(ls(x),l,mid,p,q),find(rs(x),mid+1,r,p,q)); 
}
signed main(){
	int n,m;
	scanf("%lld",&n);
	for(int i=1;i<=n;i++){
		scanf("%lld",&arr[i]);
	}
	build(1,1,n);
	scanf("%lld",&m);
	char s[2];
	int a,b,c;
	for(int i=1;i<=m;i++){
		scanf("%s%lld%lld",s,&a,&b);
		if(s[0]=='P'){
			scanf("%lld",&c);
			insert_da(1,1,n,a,b,c);
		}else if(s[0]=='C'){
			scanf("%lld",&c);
			insert_st(1,1,n,a,b,c);
		}else if(s[0]=='A'){
			printf("%lld\n",find_h(1,1,n,a,b)); 
		}else{
			printf("%lld\n",find(1,1,n,a,b));
		}
	} 
}
2024/11/22 18:36
加载中...