跳到正文

题解

题解:P14311 【MX-S8-T4】平衡三元组

DerrickLo约 7 分钟
原文
文章信息与版本

文章信息

发布
2025年10月25日
收录
2025年12月12日
更新
2026年11月8日
篇幅
约 7 分钟
题目
P14311

版本

  1. v1当前

先进行一些观察:假设 Bx,By,BzB_x,B_y,B_z 中没有任何一个全局最大值,那么设 BpB_p 为全局最大值,若 p<yp<y 则将 xx 调整为 pp,若 p>yp>y 则将 zz 调整为 pp 一定不劣。所以 xx 与 zz 必然有一个为全局最大值。不妨设 xx 为全局最大值。那么我们现在只考虑 [x,m][x,m] 这一段数。我们先取出前三大值,不妨设分别为 Bx,By,BzB_x,B_y,B_z。如果 ByB_y 为三个中的最小值那么一定满足条件,如果 BzB_z 为最小值,那么不满足条件当且仅当 Bx−By<By−BzB_x-B_y<B_y-B_z。此时我们取出第四小值。如果它所在的位置 ∈[x,z]\in[x,z] 那么我们显然能在这四个位置中选出三个位置满足条件,这是十分优秀的。所以再假设这个位置 >z>z,不妨设为 ww,那么它们四个无法选出三个位置当且仅当 Bx−Bz<Bz−BwB_x-B_z<B_z-B_w。后面的我们进行类似的操作,那么这样的过程显然不会超过 O(log⁡V)\mathcal O(\log V) 次,也就是说 BzB_z 必然是 [x,m][x,m] 中前 O(log⁡V)\mathcal O(\log V) 大的数。

那么对于原问题,我们使用线段树维护 AA 序列,然后直接枚举 zz 选哪个数即可。中间的 yy 需要满足 2Ay≤Ax+Az2A_y\le A_x+A_z,也可以使用线段树求出。时间复杂度 O(n+qlog⁡V(log⁡n+log⁡V))\mathcal O(n+q\log V(\log n+\log V))。

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,q,a[1000005],op,l,r,x,B=32;
class Segment_Tree{public:
	struct tree{
		int l,r,mx,p1,p2,flag;
		friend tree operator+(const tree&a,const tree&b){
			if(a.l==-1)return b;tree c;c.l=a.l,c.r=b.r,c.flag=0;
			c.mx=max(a.mx,b.mx);
			if(c.mx==a.mx)c.p1=a.p1;else c.p1=b.p2;if(c.mx==b.mx)c.p2=b.p2;else c.p2=a.p2;return c;
		}
	}tr[4000005];
	void pushtag(int u,int k){tr[u].mx+=k,tr[u].flag+=k;}
	void pushdown(int u){
		if(!tr[u].flag)return;
		pushtag(u<<1,tr[u].flag),pushtag(u<<1|1,tr[u].flag);
		tr[u].flag=0;
	}
	void build(int u,int l,int r){
		tr[u]={l,r,0,0,0,0};
		if(l==r){tr[u].mx=a[l],tr[u].p1=tr[u].p2=l;return;}
		int mid=l+r>>1;build(u<<1,l,mid),build(u<<1|1,mid+1,r);
		tr[u]=tr[u<<1]+tr[u<<1|1];
	}
	void change(int u,int l,int r,int k){
		if(tr[u].l>=l&&tr[u].r<=r){pushtag(u,k);return;}
		int mid=tr[u].l+tr[u].r>>1;pushdown(u);
		if(l<=mid)change(u<<1,l,r,k);
		if(r>mid)change(u<<1|1,l,r,k);tr[u]=tr[u<<1]+tr[u<<1|1];
	}
	tree query(int u,int l,int r){
		if(tr[u].l>=l&&tr[u].r<=r)return tr[u];
		int mid=tr[u].l+tr[u].r>>1;tree res;res.l=res.r=-1;
		pushdown(u);
		if(l<=mid)res=res+query(u<<1,l,r);
		if(r>mid)res=res+query(u<<1|1,l,r);
		return res;
	}
}t;
int query1(int l,int r){
	auto p=t.query(1,l,r);int o=p.p1;
	if(o+2>r)return -1e18;
	vector<pair<int,int>>v;int ans=-1e18;
	for(int i=1;i<=B;i++){
		auto xx=t.query(1,o+1,r);
		if(xx.mx<-1e10)break;
		v.emplace_back(xx.p2,xx.mx),t.change(1,xx.p2,xx.p2,-1e18);
	}
	for(auto x:v)for(auto y:v)if(x.first<y.first)if(x.second*2<=p.mx+y.second)ans=max(ans,x.second+y.second+p.mx);
	for(auto x:v)if(x.first>o+1){
		auto xx=t.query(1,o+1,x.first-1);
		ans=max(ans,x.second+p.mx+xx.mx);
	}
	for(auto x:v)t.change(1,x.first,x.first,1e18);
	return ans;
}
int query2(int l,int r){
	auto p=t.query(1,l,r);int o=p.p2;
	if(o-2<l)return -1e18;
	vector<pair<int,int>>v;int ans=-1e18;
	for(int i=1;i<=B;i++){
		auto xx=t.query(1,l,o-1);
		if(xx.mx<-1e10)break;
		v.emplace_back(xx.p2,xx.mx),t.change(1,xx.p2,xx.p2,-1e18);
	}
	for(auto x:v)for(auto y:v)if(x.first>y.first)if(x.second*2<=p.mx+y.second)ans=max(ans,x.second+y.second+p.mx);
	for(auto x:v)if(x.first<o-1){
		auto xx=t.query(1,x.first+1,o-1);
		ans=max(ans,x.second+p.mx+xx.mx);
	}
	for(auto x:v)t.change(1,x.first,x.first,1e18);
	return ans;
}
signed main(){
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	cin>>n>>q;
	for(int i=1;i<=n;i++)cin>>a[i];
	t.build(1,1,n);
	while(q--){
		cin>>op;
		if(op==2)cin>>l>>r>>x,t.change(1,l,r,x);
		else{
			cin>>l>>r;int ans=max(query1(l,r),query2(l,r));
			if(ans<=-1e10)cout<<"No\n";else cout<<ans<<"\n";
		}
	}
}
原文
存档评论2 条抓取于