跳到正文

休闲·娱乐

超绝无旋 Treap,16.21s 通过 P6131,考场上写出直接宗教胜利

ice_shooter约 11 分钟
原文

这是 2026年11月2日 存档的第 3 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

发布
2026年10月28日
收录
2026年10月29日
更新
2026年11月14日
篇幅
约 11 分钟

版本

  1. v5当前
  2. v4
  3. v3
  4. v2
更早的 1 个版本
  1. v1

首先,Treap 的主要思想是令我们的二叉树在权值满足二叉查找树性质的前提下又使一个随机数满足最大堆的性质,以此来尽可能保持平衡。

目前有两种做法:旋转或者分裂合并。前者感觉不如直接写 splay 或者 AVL,后者我看不懂。

于此情形,我发明了既不用旋转,也不用把整棵树分裂合并的 Treap。

首先当插入的节点到达某个子树时,将它与该子树根节点的 Treap 值对比,如果新来的节点比原来的节点 Treap 值更大,直接将它们互换。

你没有听错,直接把整颗子树交换过去。

这么做虽然可以很方便地解决最大堆的问题,但是,肯定会有很多其它问题。

所以,先假设我们插入的是一个子树,根节点为 PP,当前尝试插入的子树根节点为 TT。

若 PP 的权值小于 TT 的权值,

把 PP 的右儿子单独分出来再次尝试插入子树 TT。(没有则忽视)

把 PP 插入 TT 的左子树。

否则,即 PP 的权值大于 TT 的权值

把 PP 的左儿子单独分出来再次尝试插入子树 TT。(没有则忽视)

把 PP 插入 TT 的右子树。

这样一来,我们的删除操作也非常好写,将当前节点删除后把右子树插入左子树就好了。

可以看出,这么插入确实可以保证我们的算法正确性,可是时间正确性呢?

经过测试,这样生成出来的树确实不是平衡的,相同范围数据下深度大约为 AVL 的两到三倍,运气不好甚至十倍。毕竟还是 Treap,稳定性就别奢求这么多了。

以及数据加强版 AC 代码。写的是指针,常数可能大一点。跑了 21.42s。

C++
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define inf (2e9+7)
struct tree{
	int x=0,sum=1,hp=0;
	tree *left=NULL,*right=NULL;
}*root=NULL;
tree* newtree(int x){
	tree *t=new tree;
	t->x=x;
	t->hp=rand()*1ll+(rand()*1ll<<15ll)+(rand()*1ll<<31ll)+(rand()*1ll<<47ll);
	return t;
}void insert(tree *&t,tree *p){
	if(!p)return;
	if(!t){
		t=p;
		return;
	}if(t->hp<p->hp){
		swap(t,p);
	}if(p->x<t->x || (p->x==t->x && rand()%2)){ //防止被特殊数据卡飞
		tree *q=p->right;
		p->right=NULL;
		if(q)p->sum-=q->sum;
		t->sum+=p->sum;
		insert(t->left,p);
		insert(t,q);
	}else{
		tree *q=p->left;
		p->left=NULL;
		if(q)p->sum-=q->sum;
		t->sum+=p->sum;
		insert(t->right,p);
		insert(t,q);
	}
}void erase(tree *&t,int x){
	if(!t)return;
	if(x==t->x){
		tree *l=t->left,*r=t->right,*d=t;
		t=l;
		insert(t,r);
		delete d;
		return;
	}if(x<t->x){
		erase(t->left,x);
	}else{
		erase(t->right,x);
	}t->sum--;
}int query1(tree *t,int x){
	if(!t)return 0;
	if(x<=t->x){
		return query1(t->left,x);
	}else{
		return query1(t->right,x)+t->sum-(t->right?t->right->sum:0);
	}
}int query2(tree *t,int k){
	if(!t)return 0;
	if(k==(t->left?t->left->sum:0)+1)return t->x;
	if(k<(t->left?t->left->sum:0)+1){
		return query2(t->left,k);
	}else{
		return query2(t->right,k-(t->left?t->left->sum:0)-1);
	}
}int query3(tree *t,int x){
	if(!t)return -inf;
	if(x<=t->x){
		return query3(t->left,x);
	}else{
		return max(t->x,query3(t->right,x));
	}
}int query4(tree *t,int x){
	if(!t)return inf;
	if(x<t->x){
		return min(t->x,query4(t->left,x));
	}else{
		return query4(t->right,x);
	}
}
signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);
	srand(time(0));
	int n,m,last=0,sum=0;
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		int x;
		cin>>x;
		insert(root,newtree(x));
	}
	while(m--){
		int op,x;
		cin>>op>>x;
		x^=last;
		if(op==1){
			insert(root,newtree(x));
		}else if(op==2){
			erase(root,x);
		}else if(op==3){
			last=query1(root,x)+1;
			sum^=last;
		}else if(op==4){
			last=query2(root,x);
			sum^=last;
		}else if(op==5){
			last=query3(root,x);
			sum^=last;
		}else if(op==6){
			last=query4(root,x);
			sum^=last;
		}
	}cout<<sum;
	return 0;
}

同学说本质 FHQ,是这样吗。

注意到频繁把子树分出来会极大地增加我们的深度和不必要的耗时,所以可以优化一下,跳到需要分出来的子树再分裂。

以及优化后的 AC 代码,跑了 16.21s。

C++
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define inf (2e9+7)
struct tree{
	int x=0,sum=1,hp=0;
	tree *left=NULL,*right=NULL;
}*root=NULL;
tree* newtree(int x){
	tree *t=new tree;
	t->x=x;
	t->hp=rand()*1ll+(rand()*1ll<<15ll)+(rand()*1ll<<31ll)+(rand()*1ll<<47ll);
	return t;
}void insert(tree *&t,tree *p){
	if(!p)return;
	if(!t){
		t=p;
		return;
	}if(t->hp<p->hp){
		swap(t,p);
	}if(p->x<t->x || (p->x==t->x && rand()%2)){
		tree *q=p;
		while(q->right && q->right->x<t->x)q=q->right;
		tree *c=q->right,*d=p;
		q->right=NULL;
		if(c)while(d){
			d->sum-=c->sum;
			d=d->right;
		}t->sum+=p->sum;
		insert(t->left,p);
		insert(t,c);
	}else{
		tree *q=p;
		while(q->left && q->left->x>t->x)q=q->left;
		tree *c=q->left,*d=p;
		q->left=NULL;
		if(c)while(d){
			d->sum-=c->sum;
			d=d->left;
		}t->sum+=p->sum;
		insert(t->right,p);
		insert(t,c);
	}
}void erase(tree *&t,int x){
	if(!t)return;
	if(x==t->x){
		tree *l=t->left,*r=t->right,*d=t;
		t=l;
		insert(t,r);
		delete d;
		return;
	}if(x<t->x){
		erase(t->left,x);
	}else{
		erase(t->right,x);
	}t->sum--;
}int query1(tree *t,int x){
	if(!t)return 0;
	if(x<=t->x){
		return query1(t->left,x);
	}else{
		return query1(t->right,x)+t->sum-(t->right?t->right->sum:0);
	}
}int query2(tree *t,int k){
	if(!t)return 0;
	if(k==(t->left?t->left->sum:0)+1)return t->x;
	if(k<(t->left?t->left->sum:0)+1){
		return query2(t->left,k);
	}else{
		return query2(t->right,k-(t->left?t->left->sum:0)-1);
	}
}int query3(tree *t,int x){
	if(!t)return -inf;
	if(x<=t->x){
		return query3(t->left,x);
	}else{
		return max(t->x,query3(t->right,x));
	}
}int query4(tree *t,int x){
	if(!t)return inf;
	if(x<t->x){
		return min(t->x,query4(t->left,x));
	}else{
		return query4(t->right,x);
	}
}
signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);
	srand(time(0));
	int n,m,last=0,sum=0;
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		int x;
		cin>>x;
		insert(root,newtree(x));
	}
	while(m--){
		int op,x;
		cin>>op>>x;
		x^=last;
		if(op==1){
			insert(root,newtree(x));
		}else if(op==2){
			erase(root,x);
		}else if(op==3){
			last=query1(root,x)+1;
			sum^=last;
		}else if(op==4){
			last=query2(root,x);
			sum^=last;
		}else if(op==5){
			last=query3(root,x);
			sum^=last;
		}else if(op==6){
			last=query4(root,x);
			sum^=last;
		}
	}cout<<sum;
	return 0;
}