模板——Fhq_treap

$Fhq$ $treap$

#include <bits/stdc++.h>
using namespace std;
const int MAXN=100100;
int n,root,t;
struct node
{
    int val,key,si,son[2],g;
}sh[MAXN];
deque <int> q;
int newnode(int v)
{
    t++;
    sh[t].si=sh[t].g=1;
    sh[t].val=v;
    sh[t].key=rand();
    return t;
}
void pushup(int x)
{
    sh[x].si=sh[x].g+sh[sh[x].son[0]].si+sh[sh[x].son[1]].si;
}
void split(int now,int k,int &x,int &y)
{
    if (now==0)
    {
        x=y=0;
        return;
    }
    if (sh[now].val<=k)
    {
        x=now;
        split(sh[now].son[1],k,sh[now].son[1],y);
    }
    else
    {
        y=now;
        split(sh[now].son[0],k,x,sh[now].son[0]);
    }
    pushup(now);
}
int merge(int x,int y)
{
    if (x==0)
      return y;
    if (y==0)
      return x;
    if (sh[x].key<sh[y].key)
    {
        sh[x].son[1]=merge(sh[x].son[1],y);
        pushup(x);
        return x;
    }
    else
    {
        sh[y].son[0]=merge(x,sh[y].son[0]);
        pushup(y);
        return y;
    }
}
int kth(int now,int k)
{
    if (sh[sh[now].son[0]].si<k && k<=sh[sh[now].son[0]].si+sh[now].g)
      return now;
    else
    if (k<sh[sh[now].son[0]].si+sh[now].g)
      return kth(sh[now].son[0],k);
    else
      return kth(sh[now].son[1],k-sh[sh[now].son[0]].si-sh[now].g);
}
int find(int x)
{
    q.clear();
    int cur;
    cur=root;
    while (sh[cur].son[x>sh[cur].val]!=0 && sh[cur].val!=x)
      q.push_back(cur),cur=sh[cur].son[x>sh[cur].val];
    q.push_back(cur);
    return cur;
}
void updata()
{
    while (!q.empty())
    {
        pushup(q.back());
        q.pop_back();
    }
}
int main()
{
    srand(time(0));
    root=0;
    scanf("%d",&n);
    for (int i=1;i<=n;i++)
    {
        int x,op;
        scanf("%d%d",&op,&x);
        if (op==1)
        {
            int cur=find(x);
            if (sh[cur].val==x)
            {
                sh[cur].g++;
                updata();
            }
            else
            {
                int a,b;
                split(root,x,a,b);
                root=merge(merge(a,newnode(x)),b);
            }
        }
        if (op==2)
        {
            int cur=find(x);
            sh[cur].g--;
            updata();
            if (sh[cur].g==0)
            {
                int a,b,c;
                split(root,x,a,b);
                split(a,x-1,a,c);
                c=merge(sh[c].son[0],sh[c].son[1]);
                root=merge(merge(a,b),c);
            }
        }
        if (op==3)
        {
            int a,b;
            split(root,x-1,a,b);
            printf("%d\n",sh[a].si+1);
            root=merge(a,b);
        }
        if (op==4)
        {
            printf("%d\n",sh[kth(root,x)].val);
        }
        if (op==5)
        {
            int a,b;
            split(root,x-1,a,b);
            printf("%d\n",sh[kth(a,sh[a].si)].val);
            root=merge(a,b);
        }
        if (op==6)
        {
            int a,b;
            split(root,x,a,b);
            printf("%d\n",sh[kth(b,1)].val);
            root=merge(a,b);
        }
    }
}

 

posted @ 2019-07-20 10:00  SevenDawns  阅读(220)  评论(0编辑  收藏  举报
浏览器标题切换
浏览器标题切换end