平衡树

支持

  1. 插入 \(x\) 数
  2. 删除 \(x\) 数(若有多个相同的数,应只删除一个)
  3. 查询 \(x\) 数的排名(若有多个相同的数,应输出最小的排名)
  4. 查询排名为 \(x\) 的数
  5. 求 \(x\) 的前趋(前趋定义为小于 ,且最大的数)
  6. 求 \(x\) 的后继(后继定义为大于 ,且最小的数)

线段树法?

#include<bits/stdc++.h>
#define lson tree[rt].ls
#define rson tree[rt].rs
using namespace std;
constexpr int maxn=1e5+10;
long long read() {long long x=0,f=1;char ch=getchar();while (ch<'0' || ch>'9'){if (ch=='-') f=-1;ch=getchar();}while (ch>='0' && ch<='9'){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}return x*f;}
struct _ {int ls,rs,sum,cnt;}tree[maxn*30];
int n,root,segtot;
void update(int &rt,int l,int r,int pos,int val)
{
	if (!rt) rt=++segtot;
	if (l == r)
	{
		tree[rt].sum=pos;
		tree[rt].cnt+=val;
		return;
	}
	int mid=(l+r-1) >> 1;
	if (pos<=mid) update(lson,l,mid,pos,val);
	else update(rson,mid+1,r,pos,val);
	tree[rt].cnt=tree[lson].cnt+tree[rson].cnt;
}
int query(int rt,int l,int r,int L,int R)
{
	if (!rt) return 0;
	if (L<=l && r<=R) return tree[rt].cnt;
	int ans=0;
	int mid=(l+r-1) >> 1;
	if (L<=mid) ans+=query(lson,l,mid,L,R);
	if (R>mid) ans+=query(rson,mid+1,r,L,R);
	return ans;
}
int queryk(int rt,int l,int r,int k)
{
	if (!rt) return -0x3f3f3f3f;
	if (l == r) return tree[rt].sum;
	int mid=(l+r-1) >> 1;
	if (k<=tree[lson].cnt) return queryk(lson,l,mid,k);
	else return queryk(rson,mid+1,r,k-tree[lson].cnt);
}
int main()
{
	int op,x,y,z;
	cin >> n;
	for (int i=1;i<=n;i++)
	{
		cin >> op >> x;
		if (op == 1) update(root,-1e7,1e7,x,1);
		else if (op == 2) update(root,-1e7,1e7,x,-1);
		else if (op == 3)
		{
			y=query(root,-1e7,1e7,-1e7,x-1)+1;
			cout << y << endl;
		}
		else if (op == 4)
		{
			y=queryk(root,-1e7,1e7,x);
			cout << y << endl;
		}
		else if (op == 5)
		{
			y=query(root,-1e7,1e7,-1e7,x-1);
			y=queryk(root,-1e7,1e7,y);
			cout << y << endl;
		}
		else
		{
			y=query(root,-1e7,1e7,-1e7,x);
			z=queryk(root,-1e7,1e7,y+1);
			cout << z << endl;
		}
	}
	return 0;
}

带旋Treap

#include<bits/stdc++.h>
using namespace std;
constexpr int maxn=1e6+10;
int n;
struct _
{
	int l,r;
	int val,dat;
	int cnt,siz;
}a[maxn];
int tot,root;
int New(int val)
{
	a[++tot].val=val;
	a[tot].dat=rand();
	a[tot].cnt=a[tot].siz=1;
	return tot;
}
void update(int p) {a[p].siz=a[a[p].l].siz+a[a[p].r].siz+a[p].cnt;}
void build()
{
	New(-0x7fffffff),New(0x7fffffff);
	root=1;
	a[1].r=2;
	update(root);
}
int getrankbyval(int p,int val)
{
	if (p == 0) return 1;
	if (val == a[p].val) return a[a[p].l].siz+1;
	if (val<a[p].val) return getrankbyval(a[p].l,val);
	return getrankbyval(a[p].r,val)+a[a[p].l].siz+a[p].cnt;
}
int getvalbyrank(int p,int rank)
{
	if (p == 0) return 0x7fffffff;
	if (a[a[p].l].siz>=rank) return getvalbyrank(a[p].l,rank);
	if (a[a[p].l].siz+a[p].cnt>=rank) return a[p].val;
	return getvalbyrank(a[p].r,rank-a[a[p].l].siz-a[p].cnt);
}
void zig(int &p)
{
	int q=a[p].l;
	a[p].l=a[q].r,a[q].r=p,p=q;
	update(a[p].r),update(p);
}
void zag(int &p)
{
	int q=a[p].r;
	a[p].r=a[q].l,a[q].l=p,p=q;
	update(a[p].l),update(p);
}
void insert(int &p,int val)
{
	if (p == 0)
	{
		p=New(val);
		return;
	}
	if (val == a[p].val)
	{
		a[p].cnt++;
		update(p);
		return;
	}
	if (val<a[p].val)
	{
		insert(a[p].l,val);
		if (a[p].dat<a[a[p].l].dat) zig(p);
	}
	else
	{
		insert(a[p].r,val);
		if (a[p].dat<a[a[p].r].dat) zag(p);
	}
	update(p); 
}
int getpre(int val)
{
	int ans=1;
	int p=root;
	while (p)
	{
		if (val == a[p].val)
		{
			if (a[p].l>0)
			{
				p=a[p].l;
				while (a[p].r>0) p=a[p].r;
				ans=p;
			}
			break;
		}
		if (a[p].val<val && a[p].val>a[ans].val) ans=p;
		p=val<a[p].val ? a[p].l : a[p].r;
	}
	return a[ans].val;
}
int getnext(int val)
{
	int ans=2;
	int p=root;
	while (p)
	{
		if (val == a[p].val)
		{
			if (a[p].r>0)
			{
				p=a[p].r;
				while (a[p].l>0) p=a[p].l;
				ans=p;
			}
			break;
		}
		if (a[p].val>val && a[p].val<a[ans].val) ans=p;
		p=val<a[p].val ? a[p].l : a[p].r;
	}
	return a[ans].val;
}
void remove(int &p,int val)
{
	if (p == 0) return;
	if (val == a[p].val)
	{
		if (a[p].cnt>1)
		{
			a[p].cnt--;
			update(p);
			return;
		}
		if (a[p].l || a[p].r)
		{
			if (a[p].r == 0 || a[a[p].l].dat>a[a[p].r].dat)
			{
				zig(p);
				remove(a[p].r,val);
			}
			else
			{
				zag(p);
				remove(a[p].l,val); 
			}
			update(p);
		}
		else p=0;
		return;
	}
	val<a[p].val ? remove(a[p].l,val) : remove(a[p].r,val);
	update(p);
}
int main()
{
	build();
	cin >> n;
	while (n--)
	{
		int op,x;
		cin >> op >> x;
		if (op == 1) insert(root,x);
		if (op == 2) remove(root,x);
		if (op == 3) cout << getrankbyval(root,x)-1 << endl;
		if (op == 4) cout << getvalbyrank(root,x+1) << endl;
		if (op == 5) cout << getpre(x) << endl;
		if (op == 6) cout << getnext(x) << endl;
	}
	return 0;
}

FHQ Treap

int n,root;
namespace fhq
{
	mt19937 rd(time(0));
	int val[maxn],siz[maxn],rnd[maxn],tot,ls[maxn],rs[maxn];
	void Pushup(int x) {siz[x]=siz[ls[x]]+siz[rs[x]]+1;}
	void Split(int now,int v,int &x,int &y)
	{
		if (!now) return x=y=0,void();
		if (val[now]<=v) x=now,Split(rs[now],v,rs[now],y);
		else y=now,Split(ls[now],v,x,ls[now]);
		Pushup(now);
	}
	int Merge(int x,int y)
	{
		if (!x || !y) return x|y;
		if (rnd[x]<rnd[y])
		{
			rs[x]=Merge(rs[x],y);
			Pushup(x);
			return x;
		}
		else
		{
			ls[y]=Merge(x,ls[y]);
			Pushup(y);
			return y;
		}
	}
	int New(int v)
	{
		tot++,val[tot]=v,rnd[tot]=rd(),siz[tot]=1;
		return tot;
	}
	void Insert(int a)
	{
		int x,y;
		Split(root,a,x,y);
		root=Merge(Merge(x,New(a)),y);
	}
	int fid_kth(int x,int k)
	{
		while (1)
		{
			if (k<=siz[ls[x]]) x=ls[x];
			else if (k == siz[ls[x]]+1) return x;
			else k-=(siz[ls[x]]+1),x=rs[x];
		}
	}
	int fid_siz(int v)
	{
		int x,y;
		Split(root,v-1,x,y);
		int k=siz[x]+1;
		root=Merge(x,y);
		return k;
	}
	int fid_prev(int v)
	{
		int x,y;
		Split(root,v-1,x,y);
		int tmp=val[fid_kth(x,siz[x])];
		root=Merge(x,y);
		return tmp;
	}
	int fid_nxt(int v)
	{
		int x,y;
		Split(root,v,x,y);
		int tmp=val[fid_kth(y,1)];
		root=Merge(x,y);
		return tmp;
	}
	void Delete(int v)
	{
		int x,y,z;
		Split(root,v,x,z);
		Split(x,v-1,x,y);
		y=Merge(ls[y],rs[y]);
		root=Merge(Merge(x,y),z);
	}
};
点击查看代码
#include<bits/stdc++.h>
using namespace std;
constexpr int p=100003,maxn=2e5+10;
long long read() {long long x=0,f=1;char ch=getchar();while (ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while (ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch-'0');ch=getchar();}return x*f;}
int n,root;
namespace fhq
{
	mt19937 rd(time(0));
	int val[maxn],siz[maxn],rnd[maxn],tot,ls[maxn],rs[maxn];
	void Pushup(int x) {siz[x]=siz[ls[x]]+siz[rs[x]]+1;}
	void Split(int now,int v,int &x,int &y)
	{
		if (!now) return x=y=0,void();
		if (val[now]<=v) x=now,Split(rs[now],v,rs[now],y);
		else y=now,Split(ls[now],v,x,ls[now]);
		Pushup(now);
	}
	int Merge(int x,int y)
	{
		if (!x || !y) return x|y;
		if (rnd[x]<rnd[y])
		{
			rs[x]=Merge(rs[x],y);
			Pushup(x);
			return x;
		}
		else
		{
			ls[y]=Merge(x,ls[y]);
			Pushup(y);
			return y;
		}
	}
	int New(int v)
	{
		tot++,val[tot]=v,rnd[tot]=rd(),siz[tot]=1;
		return tot;
	}
	void Insert(int a)
	{
		int x,y;
		Split(root,a,x,y);
		root=Merge(Merge(x,New(a)),y);
	}
	int fid_kth(int x,int k)
	{
		while (1)
		{
			if (k<=siz[ls[x]]) x=ls[x];
			else if (k == siz[ls[x]]+1) return x;
			else k-=(siz[ls[x]]+1),x=rs[x];
		}
	}
	int fid_siz(int v)
	{
		int x,y;
		Split(root,v-1,x,y);
		int k=siz[x]+1;
		root=Merge(x,y);
		return k;
	}
	int fid_prev(int v)
	{
		int x,y;
		Split(root,v-1,x,y);
		int tmp=val[fid_kth(x,siz[x])];
		root=Merge(x,y);
		return tmp;
	}
	int fid_nxt(int v)
	{
		int x,y;
		Split(root,v,x,y);
		int tmp=val[fid_kth(y,1)];
		root=Merge(x,y);
		return tmp;
	}
	void Delete(int v)
	{
		int x,y,z;
		Split(root,v,x,z);
		Split(x,v-1,x,y);
		y=Merge(ls[y],rs[y]);
		root=Merge(Merge(x,y),z);
	}
};
int main()
{
	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	cin >> n;
	while (n--)
	{
		int op,x;
		cin >> op >> x;
		if (op == 1) fhq::Insert(x);
		if (op == 2) fhq::Delete(x);
		if (op == 3) cout << fhq::fid_siz(x) << endl;
		if (op == 4) cout << fhq::val[fhq::fid_kth(root,x)] << endl;
		if (op == 5) cout << fhq::fid_prev(x) << endl;
		if (op == 6) cout << fhq::fid_nxt(x) << endl;
	}
	return 0;
}

splay

int n;
namespace splay
{
	struct _ {int fa,cnt,ch[2],val,siz;}st[maxn];
	int tot,root;
	void Pushup(int x) {st[x].siz=st[st[x].ch[0]].siz+st[st[x].ch[1]].siz+st[x].cnt;}
	void Rotate(int x)
	{
		int y=st[x].fa,z=st[y].fa,k=st[y].ch[1] == x;
		st[z].ch[st[z].ch[1] == y]=x;
		st[x].fa=z;
		st[y].ch[k]=st[x].ch[k^1];
		st[st[x].ch[k^1]].fa=y;
		st[x].ch[k^1]=y;
		st[y].fa=x;
		Pushup(y),Pushup(x);
	}
	void Splay(int x,int k)
	{
		while (st[x].fa!=k)
		{
			int y=st[x].fa,z=st[y].fa;
			if (z!=k) (st[z].ch[0] == y)^(st[y].ch[0] == x) ? Rotate(x) : Rotate(y);
			Rotate(x);
		}
		if (k == 0) root=x;
	}
	void fid(int x)
	{
		int u=root;
		if (!u) return;
		while (st[u].ch[x>st[u].val] && x!=st[u].val) u=st[u].ch[x>st[u].val];
		Splay(u,0);
	}
	void Insert(int x)
	{
		int u=root,fa=0;
		while (u && st[u].val!=x) fa=u,u=st[u].ch[x>st[u].val];
		if (u) st[u].cnt++;
		else
		{
			u=++tot;
			if (fa) st[fa].ch[x>st[fa].val]=u;
			st[u].ch[0]=st[u].ch[1]=0;
			st[tot].fa=fa;st[tot].val=x;
			st[tot].cnt=1;st[tot].siz=1;
		}
		Splay(u,0);
	}
	int nxt(int x,int f)
	{
		fid(x);
		int u=root;
		if ((st[u].val>x && f) || (st[u].val<x &&!f)) return u;
		u=st[u].ch[f];
		while (st[u].ch[f^1]) u=st[u].ch[f^1];
		return u;
	}
	void Delete(int x)
	{
		int lt=nxt(x,0),net=nxt(x,1);
		Splay(lt,0),Splay(net,lt);
		int del=st[net].ch[0];
		if (st[del].cnt>1) st[del].cnt--,Splay(del,0);
		else st[net].ch[0]=0;
	}
	int kth(int x)
	{
		int u=root;
		if (st[u].siz<x) return 0;
		while (1)
		{
			int y=st[u].ch[0];
			if (x>st[y].siz+st[u].cnt) x-=st[y].siz+st[u].cnt,u=st[u].ch[1];
			else
			{
				if (st[y].siz>=x) u=y;
				else return st[u].val;
			}
		}
	}
	int rak(int x)
	{
		Insert(x);
		fid(x);
		int k=st[st[root].ch[0]].siz;
		Delete(x);
		return k;
	}
}
点击查看代码
#include<bits/stdc++.h>
using namespace std;
constexpr int p=100003,maxn=2e5+10;
long long read() {long long x=0,f=1;char ch=getchar();while (ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while (ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch-'0');ch=getchar();}return x*f;}
int n;
namespace splay
{
	struct _ {int fa,cnt,ch[2],val,siz;}st[maxn];
	int tot,root;
	void Pushup(int x) {st[x].siz=st[st[x].ch[0]].siz+st[st[x].ch[1]].siz+st[x].cnt;}
	void Rotate(int x)
	{
		int y=st[x].fa,z=st[y].fa,k=st[y].ch[1] == x;
		st[z].ch[st[z].ch[1] == y]=x;
		st[x].fa=z;
		st[y].ch[k]=st[x].ch[k^1];
		st[st[x].ch[k^1]].fa=y;
		st[x].ch[k^1]=y;
		st[y].fa=x;
		Pushup(y),Pushup(x);
	}
	void Splay(int x,int k)
	{
		while (st[x].fa!=k)
		{
			int y=st[x].fa,z=st[y].fa;
			if (z!=k) (st[z].ch[0] == y)^(st[y].ch[0] == x) ? Rotate(x) : Rotate(y);
			Rotate(x);
		}
		if (k == 0) root=x;
	}
	void fid(int x)
	{
		int u=root;
		if (!u) return;
		while (st[u].ch[x>st[u].val] && x!=st[u].val) u=st[u].ch[x>st[u].val];
		Splay(u,0);
	}
	void Insert(int x)
	{
		int u=root,fa=0;
		while (u && st[u].val!=x) fa=u,u=st[u].ch[x>st[u].val];
		if (u) st[u].cnt++;
		else
		{
			u=++tot;
			if (fa) st[fa].ch[x>st[fa].val]=u;
			st[u].ch[0]=st[u].ch[1]=0;
			st[tot].fa=fa;st[tot].val=x;
			st[tot].cnt=1;st[tot].siz=1;
		}
		Splay(u,0);
	}
	int nxt(int x,int f)
	{
		fid(x);
		int u=root;
		if ((st[u].val>x && f) || (st[u].val<x &&!f)) return u;
		u=st[u].ch[f];
		while (st[u].ch[f^1]) u=st[u].ch[f^1];
		return u;
	}
	void Delete(int x)
	{
		int lt=nxt(x,0),net=nxt(x,1);
		Splay(lt,0),Splay(net,lt);
		int del=st[net].ch[0];
		if (st[del].cnt>1) st[del].cnt--,Splay(del,0);
		else st[net].ch[0]=0;
	}
	int kth(int x)
	{
		int u=root;
		if (st[u].siz<x) return 0;
		while (1)
		{
			int y=st[u].ch[0];
			if (x>st[y].siz+st[u].cnt) x-=st[y].siz+st[u].cnt,u=st[u].ch[1];
			else
			{
				if (st[y].siz>=x) u=y;
				else return st[u].val;
			}
		}
	}
	int rak(int x)
	{
		Insert(x);
		fid(x);
		int k=st[st[root].ch[0]].siz;
		Delete(x);
		return k;
	}
}
int main()
{
	n=read();
	splay::Insert(1e9),splay::Insert(-1e9);
	while (n--)
	{
		int op=read(),x=read();
		if (op == 1) splay::Insert(x);
		if (op == 2) splay::Delete(x);
		if (op == 3) printf("%d\n",splay::rak(x));
		if (op == 4) printf("%d\n",splay::kth(x+1));
		if (op == 5) printf("%d\n",splay::st[splay::nxt(x,0)].val);
		if (op == 6) printf("%d\n",splay::st[splay::nxt(x,1)].val);
	}
	return 0;
}
posted @ 2026-03-24 11:41  msjing  阅读(28)  评论(0)    收藏  举报