跳到正文

题解

题解:P14761 [Opoi 2025] CCD 的序列

Silence_in_Sea约 8 分钟
原文
文章信息与版本

文章信息

收录
2025年12月17日
更新
2025年12月17日
篇幅
约 8 分钟
题目
P14761

版本

  1. v2当前
  2. v1

赛时看错许多遍题目导致死亡。

首先我们考虑对于每个括号求出其最终所在位置。

我们手玩一下,这一步其实有一定的思维难度,我们会发现,后面的想要放到的位置一定比前面优先级高。 倒着处理之后,对于在位置 xx 插入我们可以直接找第 xx 个没有被确定的位置,我们可以直接使用权值线段树先处理 ll 再处理 rr,记得更新就好。

然后我们考虑如何去求值。

我们思考,对于目前的括号序列,插入后改变的只可能是左边的多的右括号和右边多的左括号。这是一个很典的 trick,在这里不再赘述。我们只要给左右括号赋值 −1-1 和 11 即可。

然后我们可以区间加维护最大值或单点加维护最大子段和。做完了。

记得左右括号都要处理。

代码:

C++
#include <bits/stdc++.h>
using namespace std;

int n;
struct node{
	int l,r;
}a[200005];
struct pp{
	int l,r,s;
}tree[2000005];
void pushup(int w){
	tree[w].s=tree[w*2].s+tree[w*2+1].s;
}
void build(int w,int l,int r){
	tree[w].l=l,tree[w].r=r;
	if (l==r) return;
	int mid=(l+r)/2;
	build(w*2,l,mid);
	build(w*2+1,mid+1,r);
}
void update(int w,int s,int x){
	if (tree[w].l==tree[w].r){
		tree[w].s+=x;
		return;
	}
	int mid=(tree[w].l+tree[w].r)/2;
	if (s<=mid) update(w*2,s,x);
	else update(w*2+1,s,x);
	pushup(w);
}
int query1(int w,int l,int r){
	if(l>r) return 0;
	if (tree[w].l>=l&&tree[w].r<=r) return tree[w].s;
	int mid=(tree[w].l+tree[w].r)/2,ans=0;
	if (l<=mid) ans+=query1(w*2,l,r);
	if (mid<r) ans+=query1(w*2+1,l,r);
	return ans;
}
int query(int w,int rk){
	if (tree[w].l==tree[w].r) return tree[w].l;
	int mid=tree[w*2].s;
	if (rk<=mid) return query(w*2,rk);
	else return query(w*2+1,rk-mid);
}
struct nmb{
	int l,r,s,lazy;
}tree1[2000005],tree2[2000005];
void pushup1(int w){
	tree1[w].s=max(tree1[w*2].s,tree1[w*2+1].s);
}
void pushdown(int w){
	if (tree1[w].lazy!=0){
		tree1[w*2].lazy+=tree1[w].lazy;
		tree1[w*2+1].lazy+=tree1[w].lazy;
		tree1[w*2].s+=tree1[w].lazy;
		tree1[w*2+1].s+=tree1[w].lazy;
		tree1[w].lazy=0;
	}
}
void build1(int w,int l,int r){
	tree1[w].l=l,tree1[w].r=r;
	if (l==r) return;
	int mid=(tree1[w].l+tree1[w].r)/2;
	build1(w*2,l,mid);
	build1(w*2+1,mid+1,r);
}
void update1(int w,int l,int r,int s){
	if (tree1[w].l>=l&&tree1[w].r<=r){
		tree1[w].s+=s;
		tree1[w].lazy+=s;
		return;
	}
	pushdown(w);
	int mid=(tree1[w].l+tree1[w].r)/2;
	if (l<=mid) update1(w*2,l,r,s);
	if (mid<r) update1(w*2+1,l,r,s);
	pushup1(w);
}
int query2(int w,int l,int r){
	if (tree1[w].l>=l&&tree1[w].r<=r) return tree1[w].s;
	pushdown(w);
	int ans=-8*n;
	int mid=(tree1[w].l+tree1[w].r)/2;
	if (l<=mid) ans=max(ans,query2(w*2,l,r));
	if (mid<r) ans=max(ans,query2(w*2+1,l,r));
	return ans;
}
void pushup2(int w){
	tree2[w].s=max(tree2[w*2].s,tree2[w*2+1].s);
}
void pushdown1(int w){
	if (tree2[w].lazy!=0){
		tree2[w*2].lazy+=tree2[w].lazy;
		tree2[w*2+1].lazy+=tree2[w].lazy;
		tree2[w*2].s+=tree2[w].lazy;
		tree2[w*2+1].s+=tree2[w].lazy;
		tree2[w].lazy=0;
	}
}
void build2(int w,int l,int r){
	tree2[w].l=l,tree2[w].r=r;
	if (l==r) return;
	int mid=(tree2[w].l+tree2[w].r)/2;
	build2(w*2,l,mid);
	build2(w*2+1,mid+1,r);
}
void update2(int w,int l,int r,int s){
	if (tree2[w].l>=l&&tree2[w].r<=r){
		tree2[w].s+=s;
		tree2[w].lazy+=s;
		return;
	}
	pushdown1(w);
	int mid=(tree2[w].l+tree2[w].r)/2;
	if (l<=mid) update2(w*2,l,r,s);
	if (mid<r) update2(w*2+1,l,r,s);
	pushup2(w);
}
int query3(int w,int l,int r){
	if (tree2[w].l>=l&&tree2[w].r<=r) return tree2[w].s;
	pushdown1(w);
	int ans=-8*n;
	int mid=(tree2[w].l+tree2[w].r)/2;
	if (l<=mid) ans=max(ans,query3(w*2,l,r));
	if (mid<r) ans=max(ans,query3(w*2+1,l,r));
	return ans;
}
int main(){
//	freopen(".in","r",stdin);
//	freopen(".out","w",stdout);
	ios::sync_with_stdio(false);
	cin>>n;
	for(int i=1;i<=n;i++) cin>>a[i].l>>a[i].r;
	build(1,1,n*2);
	for(int i=1;i<=n*2;i++) update(1,i,1);
	for(int i=n;i>=1;i--){
		int rp=query(1,a[i].l+1);
		a[i].l=rp;
		update(1,rp,-1);
		int pp=query(1,a[i].r+1);
		a[i].r=pp;
		update(1,pp,-1);
	}
//	for(int i=1;i<=n;i++) cout<<a[i].l<<" "<<a[i].r<<endl;
	build1(1,1,2*n);
	build2(1,1,2*n+1);
	for(int i=1;i<=n;i++){
		int p=query2(1,a[i].l-1,a[i].l-1);
		int s=query2(1,a[i].l,a[i].r);
		int vs=query3(1,a[i].r+1,a[i].r+1);
		int ps=query3(1,a[i].l,a[i].r);
		cout<<(s-p+ps-vs)*2<<endl;
		update1(1,a[i].l,2*n,-1);
		update1(1,a[i].r,2*n,1);
		update2(1,1,a[i].l,1);
		update2(1,1,a[i].r,-1);
	}
	return 0;
}
原文