• FHQ Treap 板子


    #include<cstdio>
    #include<algorithm>
    #include<ctime>
    using namespace std;
    int n,tot = 0;
    
    struct tree{
    	int siz,ls,rs,v,rnd;
    }tr[100005];
    int add(int val)
    {
    	tr[++tot] = (tree){1,0,0,val,rand()};
    	return tot;
    }
    void update(int x){tr[x].siz = tr[tr[x].ls].siz + tr[tr[x].rs].siz + 1;}
    void split(int rt,int &a,int &b,int val)
    {
    	if (rt == 0) return void(a = b = 0);
    	if (tr[rt].v <= val) a = rt,split(tr[rt].rs,tr[a].rs,b,val);
    	else b = rt,split(tr[rt].ls,a,tr[b].ls,val);
    	update(rt);
    }
    void merge(int &rt,int a,int b)
    {
    	if (a == 0 || b == 0) return void(rt = a + b);
    	if (tr[a].rnd < tr[b].rnd) rt = a,merge(tr[rt].rs,tr[a].rs,b);
    	else rt = b,merge(tr[rt].ls,a,tr[b].ls);
    	update(rt);
    }
    void Insert(int &rt,int val)
    {
    	int x = 0,y = 0,k = add(val);
    	split(rt,x,y,val);
    	merge(x,x,k),merge(rt,x,y);
    }
    void Delete(int &rt,int val)
    {
    	int x = 0,y = 0,z = 0;
    	split(rt,x,y,val),split(x,x,z,val - 1);
    	merge(z,tr[z].ls,tr[z].rs),merge(x,x,z),merge(rt,x,y);
    }
    int getkth(int rt,int k)
    {
    	while (tr[tr[rt].ls].siz + 1 != k) 
    		if (tr[tr[rt].ls].siz >= k) rt = tr[rt].ls;
    		else k -= tr[tr[rt].ls].siz + 1,rt = tr[rt].rs;
    	return tr[rt].v;
    }
    int getrnk(int &rt,int val)
    {
    	int x = 0,y = 0,tmp;
    	split(rt,x,y,val - 1);
    	tmp = tr[x].siz + 1;
    	merge(rt,x,y);
    	return tmp;
    }
    int getpre(int &rt,int val)
    {
    	int x = 0,y = 0,tmp;
    	split(rt,x,y,val - 1);
    	tmp = getkth(x,tr[x].siz);
    	merge(rt,x,y);
    	return tmp;
    }
    int getscc(int &rt,int val)
    {
    	int x = 0,y = 0,tmp;
    	split(rt,x,y,val);
    	tmp = getkth(y,1);
    	merge(rt,x,y);
    	return tmp;
    }
    int main()
    {
    	srand(time(NULL));
    	scanf("%d",&n); 
    	int root = 0;
    	for (int i = 1,q,p; i <= n; i++)
    	{
    		scanf("%d%d",&q,&p);
    		if (q == 1) Insert(root,p);
    		if (q == 2) Delete(root,p);
    		if (q == 3) printf("%d
    ",getrnk(root,p));
    		if (q == 4) printf("%d
    ",getkth(root,p));
    		if (q == 5) printf("%d
    ",getpre(root,p));
    		if (q == 6) printf("%d
    ",getscc(root,p));
    	}
    }
    
  • 相关阅读:
    字,字节,字长,位的概念与区分
    Prim算法、Kruskal算法、Dijkstra算法
    关联容器
    各种排序算法的性能特点
    随机种子
    实参&形参
    C++中的I/O输入输出问题
    NLPIR智能KGB知识图谱引擎可视化数据挖掘
    NLPIR-KGB知识图谱引擎突破传统数据挖掘束缚
    NLPIR大数据语义系统KGB技术引领新方向
  • 原文地址:https://www.cnblogs.com/nibabadeboke/p/15285844.html
Copyright © 2020-2023  润新知