跳到正文

题解

P3071题解

zhangshirui约 10 分钟
原文
文章信息与版本

文章信息

发布
2025年2月11日
收录
2025年12月12日
更新
2026年11月20日
篇幅
约 10 分钟
题目
P3071

版本

  1. v1当前

前置知识:珂朵莉树

居然没有splay手写珂朵莉树的题解?

其实我们要注意一件事,就是这题其实并没有说数据随机,所以理论上来说这题应该不能用珂朵莉树的 (那些用珂朵莉树的好好反省),但是珂朵莉树做这种题是真的很方便,常数还小。所以有一种可以保证它单log时间复杂度的方法:用平衡树手写,主要是用splay树,因为在维护珂朵莉树时的提取和维护区间比FHQ treap方便,可以把要处理的节点转到根,可以少处理一些信息。

操作一可以维护子树最长空段,然后做平衡树上二分找第一个合法的点,如果整颗树上的最长段都没有需要的长,就让计数器自增,不然进行如下操作(设当第一个合法节点为 u):

  1. 把 u 转到根。
  2. 如果 u 的长度大于需要的长度,把 u 的区间按照维护的区间分裂为两个点。
  3. 更改 u 的颜色。

更麻烦的是操作二,它要复杂很多,设涉及的左右端点为 ll 和 rr。和普通的splay一样,先用平衡树上二分找到包含 l−1l-1 和 r+1r+1 的点,如果它们在同一个点上,就把它转到根,然后分裂为三段,中间的一段即为 l∼rl \sim r,在将其的标记赋值为没人。

如果不在一个点上,那么设包含 l−1l-1 和 r+1r+1 的点分别为 lsls 和 rsrs,把 lsls 转到根,在把 rsrs 转到 lsls 的儿子。但是还没完,把 lsls 的右端点设为 l−1l-1 ,把 rsrs 的左端点设为 r+1r+1 ,这里为了减少代码长度同时也更好维护,就不用再补全区间了(这也是上面为什么要特判在同一个点的情况),现在 rsrs 的左儿子就是要删的部分,直接丢掉,不用一个一个遍历删,内存影响不大,再把它改为一个 l∼rl \sim r 的没有人的点,完成推平。

推平完成之后需要经行一个合并节点的操作,如果涉及的 lsls 或 rsrs 原本是没人的,那么这个点就被分开了,维护的空段长度就不对了,所以要对新点的前驱和后继中的空点进行合并,对前驱和后继显然最多分别进行一次合并。

这里为什么不对有人的点进行合并呢?它并不对答案产生共享,在平衡树的二分和操作上最多也就有 nn 个点,时间复杂度为 log2nlog_2 n。删除时直接丢弃,也和大小无关,但是对有人的点和并还有增加长度和常数,所以不做处理。

最后代码如下(指针写的,马蜂清奇,不要在意):

C++
#include<bits/stdc++.h>
#define ll long long
using namespace std;
struct node{
	node *l=NULL,*r=NULL,*fa=NULL;
	int ls=0,rs=0,maxx=0,tg=0;
};
node* head;
int fxj(node* u){
	if(u==u->fa->l)return 0;
	return 1;
}
void updata(node* u){
	u->maxx=max({(u->l?u->l->maxx:0),(u->r?u->r->maxx:0),(u->rs-u->ls+1)*(u->tg^1)});
}
void rotate(node* u){
	node *f=u->fa,*g=(f?f->fa:NULL);
	if(f==head)head=u;
	if(fxj(u)==0){
		f->l=u->r;
		if(u->r)u->r->fa=f;
		u->r=f;
	}else{
		f->r=u->l;
		if(u->l)u->l->fa=f;
		u->l=f;
	}
	f->fa=u;
	u->fa=g;
	if(g){
		if(g->l==f)g->l=u;
		else g->r=u;
	}
	updata(f),updata(u);
}
int gt_dep(node* u){
	if(u->fa==NULL)return 0;
	return 1+gt_dep(u->fa);
}
void splay(node* u,node* to=NULL){
	int x=0;
	while(u->fa!=to){
		if(u->fa->fa!=to){
			if(fxj(u)==fxj(u->fa))rotate(u->fa);
			else rotate(u);
		}
		rotate(u);x++;
	}
}
int cnt=0,ii=0;
void upd(int len){
	node* u=head;
	while(1){
		if(u->maxx<len){
			cnt++;
			return;
		}
		if((u->l?u->l->maxx:0)>=len)u=u->l;
		else if(u->rs-u->ls+1>=len&&u->tg==0)break;
		else u=u->r; 
	}
	splay(u);
	int r2=u->ls+len-1,tr=u->rs;
	u->rs=r2;
	u->tg=1;
	updata(u);
	if(r2!=tr){
		node* new_node=new node;
		new_node->ls=r2+1;
		new_node->rs=tr;
		new_node->l=NULL;
		new_node->r=u->r;
		if(u->r)u->r->fa=new_node;
		new_node->fa=u;
		u->r=new_node;
		new_node->tg=0;
		new_node->fa=u;
		updata(new_node);
		updata(u);
	}
}
void split(int l,int r){
	node *l_pos=head;
	l--;r++;
	while(1){
		if(l_pos->ls<=l&&l<=l_pos->rs){
			if(r<=l_pos->rs){
				if(l_pos->tg==0)return;
				splay(l_pos);
				node* new_node=new node;
				new_node->tg=1;
				new_node->fa=l_pos;
				new_node->l=l_pos->l;
				if(new_node->l)new_node->l->fa=new_node;
				new_node->ls=l_pos->ls;
				new_node->rs=l;
				updata(new_node);
				l_pos->l=new_node;
				
				new_node=new node;
				new_node->tg=1;
				new_node->fa=l_pos;
				new_node->r=l_pos->r;
				if(new_node->r)new_node->r->fa=new_node;
				new_node->ls=r;
				new_node->rs=l_pos->rs;
				updata(new_node);
				l_pos->r=new_node;
				
				l_pos->tg=0;
				l_pos->ls=l+1;
				l_pos->rs=r-1;
				updata(l_pos);
				return;
			}
			break;
		}
		if(l<l_pos->ls)l_pos=l_pos->l;
		else l_pos=l_pos->r;
	}
	node* r_pos=head;
	while(1){
		if(r_pos->ls<=r&&r<=r_pos->rs){
			if(head&&head->l&&head->l->l&&head->l->l->l&&head->l==head->l->l->l->l)exit(114);
			splay(l_pos);
			if(head&&head->l&&head->l->l&&head->l->l->l&&head->l==head->l->l->l->l)exit(514);
			l_pos->rs=l;
			updata(l_pos);
			splay(r_pos,head);
			r_pos->ls=r;
			updata(r_pos);
			updata(head);
			break;
		}
		if(r<r_pos->ls)r_pos=r_pos->l;
		else r_pos=r_pos->r;
	}
	node *new_node=new node;
	head->r->l=new_node;
	new_node->fa=head->r;
	new_node->ls=l+1;
	new_node->rs=r-1;
	new_node->tg=0;
	updata(new_node);
	updata(head->r);
	updata(head);
	splay(new_node);
	node *ul=new_node->l,*ur=new_node->r;
	if(ul){
		while(ul->r)ul=ul->r;
		splay(ul,head);
	}
	if(ur){
		while(ur->l)ur=ur->l;
		splay(ur,head);
	}
	if(ul&&ul->tg==0){
		new_node->ls=ul->ls;
		new_node->l=ul->l;
		if(ul->l)ul->l->fa=new_node;
	}
	if(ur&&ur->tg==0){
		new_node->rs=ur->rs;
		new_node->r=ur->r;
		if(ur->r)ur->r->fa=new_node;
	}
	updata(new_node);
}
int main(){
	ios::sync_with_stdio(0);
	int n,m,b,c;
	char a;
	cin>>n>>m;
	head=new node;
	head->ls=1;
	head->rs=n;
	updata(head);
	
	head->l=new node;
	head->l->ls=0;
	head->l->rs=0;
	head->l->fa=head;
	head->l->tg=1;
	updata(head);
	
	head->r=new node;
	head->r->ls=n+1;
	head->r->rs=n+1;
	head->r->fa=head;
	head->r->tg=1;
	updata(head);
	
	while(m--){
		cin>>a>>b;
		if(a=='A')upd(b);
		else{
			cin>>c;
			split(b,c);
		}
	}
	cout<<cnt;
}

最后提几个细节:

初始化要有三个点:

0∼00 \sim 0 的有人的节点、1∼n1 \sim n 的没人的节点、n+1∼n+1n+1 \sim n+1 的有人的节点。第一个和第三个的目的是维护边界,防止操作二找不到 l−1l-1 和 r+1r+1 的节点。

后记:

这个做法真的很快,我什么优化都没做就轻松拿下最优解,提交记录,作为一个珂朵莉树的拓展挺牛逼的,但是真的比较难写,可以单纯作为一个splay的练手做法。

原文