zoukankan      html  css  js  c++  java
  • treap 普通平衡树

         普通treap中最普通的板子(数组版)

          

    #include<stdio.h>
    #include<iostream>
    #include<cstdlib>
    #include<cstring>
    #include<cmath>
    #define inf 10000000
    using namespace std;
    struct tree
    {
    	int l,r,size,tot,h,rd;
    } t[100005];
    int n,root,fro,bac,hh;
    void l_swap(int &root)
    {
    	int k=t[root].r;
    	t[root].r=t[k].l;
    	t[k].l=root;
    	t[k].size=t[root].size;
    	t[root].size=t[t[root].l].size+t[t[root].r].size+t[root].tot;
    	root=k;
    }
    void r_swap(int &root)
    {
    	int k=t[root].l;
    	t[root].l=t[k].r;
    	t[k].r=root;
    	t[k].size=t[root].size;
    	t[root].size=t[t[root].l].size+t[t[root].r].size+t[root].tot;
    	root=k;
    }
    void build(int &root,int k)
    {
    	if(!root)
    	{
    		root=++hh;
    		t[root].h=k;
    		t[root].size=t[root].tot=1;
    		t[root].rd=rand();
    		return;
    	}
    	t[root].size++;
    	if(t[root].h==k)t[root].tot++;
    	else
    	{
    		if(t[root].h>k)
    		{
    			build(t[root].l,k);
    			if(t[t[root].l].rd<t[root].rd);
    			   r_swap(root);
    		}
    		else
    		{
    			build(t[root].r,k);
    			if(t[t[root].r].rd<t[root].rd)
    			    l_swap(root);
    		}
    	}
    }
    void del(int &root,int k)
    {
    	if(!root)return;
    	if(t[root].h==k)
    	{
    		if(t[root].tot>1)
    		{
    			t[root].tot--;
    			t[root].size--;
    			return;
    		}
    		if(t[root].l*t[root].r==0)
    		    root=t[root].l+t[root].r;
    		else
    		{
    			if(t[t[root].l].rd<t[t[root].r].rd)
    			    r_swap(root);
    			else
    			    l_swap(root);
    			del(root,k);
    		}
    	}
    	else
    	{
    		 t[root].size--;
    		 if(k>t[root].h)del(t[root].r,k);
    		 else del(t[root].l,k);
    	}
    }
    void FRO(int &root,int k)
    {
    	if(!root)return;
    	if(t[root].h<k)
    	{
    		fro=t[root].h;
    		FRO(t[root].r,k);
    	}
    	else FRO(t[root].l,k);
    }
    void BAC(int &root,int k)
    {
    	if(!root)return;
    	if(t[root].h>k)
    	{
    		bac=t[root].h;
    		BAC(t[root].l,k);
    	}
    	else BAC(t[root].r,k);
    }
    int num(int &root,int k)
    {
    	if(!root)return 0;
    	if(t[root].h==k)
    	    return t[t[root].l].size+1;
    	else
    	{
    		if(t[root].h>k)return num(t[root].l,k);
    		else  return t[t[root].l].size+t[root].tot+num(t[root].r,k);
    	}
    }
    int Q(int &root,int k)
    {
    	if(!root)return 0;
    	if(t[t[root].l].size+1<=k&&t[t[root].l].size+t[root].tot>=k)
    	     return t[root].h;
    	if(t[t[root].l].size>=k)
    	     return Q(t[root].l,k);
    	if(t[t[root].l].size<k)
    	     return Q(t[root].r,k-t[root].tot-t[t[root].l].size);
    }
    int yjn()
    {
    //	 freopen("phs.in","r",stdin);
      //  freopen("phs.out","w",stdout);
    	scanf("%d",&n);
    	int x,y;
    	for(int i=1;i<=n;i++)
    	{
    		scanf("%d%d",&x,&y);
    		if(x==1)build(root,y);
    		if(x==2)del(root,y);
    		if(x==3){printf("%d
    ",num(root,y));}
    		if(x==4){printf("%d
    ",Q(root,y));}
    		if(x==5){fro=-inf;FRO(root,y);printf("%d
    ",fro);}
    		if(x==6){bac=-inf;BAC(root,y);printf("%d
    ",bac);}
    	}
    }
    int qty=yjn();
    int main(){;}

  • 相关阅读:
    【LeetCode-位运算】位1的个数
    【LeetCode-数组】调整数组顺序使奇数位于偶数前面
    mySQL数据库中.frm和.myi和.myd和.ibd文件是什么文件?
    安装docker-compose的两种方式
    将第三方jar包 安装到 maven仓库
    limit分页查询
    pom.xml配置指定仓库
    linux常用命令
    正则笔记
    使用conda创建虚拟环境
  • 原文地址:https://www.cnblogs.com/QTY2001/p/7632749.html
Copyright © 2011-2022 走看看