absi2011's Blog & Daily Life.

全新的开始       我要省选翻盘       I wanna AK in 高考\化学       自此,生无可恋
[破碎的状态] [-5] BZOJ 1455
[破碎的状态] [-4] POJ 2536

[破碎的状态] [-4] Tyvj 1728 普通平衡树

absi2011 posted @ Jul 18, 2016 03:00:01 PM in 刷题记录 with tags Treap 小高考 , 557 阅读

这平衡树是挺普通的..

只是我自己是sb..竟然连续写错了一叠次..

总算1A了一次...

#include<set>
#include<map>
#include<list>
#include<queue>
#include<stack>
#include<string>
#include<time.h>
#include<math.h>
#include<memory>
#include<vector>
#include<bitset>
#include<fstream>
#include<stdio.h>
#include<utility>
#include<sstream>
#include<string.h>
#include<iostream>
#include<stdlib.h>
#include<algorithm>
using namespace std;
const int inf=99999999;
struct node
{
    int val;
    int size;
    int weight;
    node * ch[2];
};
node * null;
node * new_node(int x=-inf)
{
    static node a[100005];
    static int top=0;
    node * t=&a[top++];
    t->val=x;
    t->size=1;
    t->weight=rand();
    t->ch[0]=null;
    t->ch[1]=null;
    return t;
}
void rotate(node * &x,int c)
{
    node * y=x->ch[c];
    x->ch[c]=y->ch[!c];
    y->ch[!c]=x;
    y->size=x->size;
    x->size=x->ch[0]->size+1+x->ch[1]->size;
    x=y;
}
void insert_node(node * &now,node * x)
{
    if (now==null)
    {
        now=x;
        return;
    }
    now->size++;
    int c;
    if (x->val<now->val)
    {
        c=0;
    }
    else
    {
        c=1;
    }
    insert_node(now->ch[c],x);
    if (now->ch[c]->weight>now->weight) rotate(now,c);
}
void delete_node(node * &now,int x)
{
    now->size--;
    if (now->val==x)
    {
        int c;
        if (now->ch[0]->weight>now->ch[1]->weight)
        {
            c=0;
        }
        else
        {
            c=1;
        }
        if (now->ch[c]==null)
        {
            now=null;
            return;
        }
        rotate(now,c);
        delete_node(now->ch[!c],x);
    }
    else
    {
        if (x<now->val)
        {
            delete_node(now->ch[0],x);
        }
        else
        {
            delete_node(now->ch[1],x);
        }
    }
}
int get_rank(node * x,int k)
{
    if (x==null) return 1;
    if (x->val<k)
    {
        return x->ch[0]->size+1+get_rank(x->ch[1],k);
    }
    return get_rank(x->ch[0],k);
}
int find_kth(node * x,int k)
{
    if (k==x->ch[0]->size+1)
    {
        return x->val;
    }
    else if (k<x->ch[0]->size+1)
    {
        return find_kth(x->ch[0],k);
    }
    else
    {
        return find_kth(x->ch[1],k-x->ch[0]->size-1);
    }
}
int find_small(node * x,int k)
{
    if (x==null) return x->val;
    if (x->val>=k)
    {
        return find_small(x->ch[0],k);
    }
    int t=find_small(x->ch[1],k);
    if (t==-inf) return x->val;
    return t;
}
int find_big(node * x,int k)
{
    if (x==null) return x->val;
    if (x->val<=k)
    {
        return find_big(x->ch[1],k);
    }
    int t=find_big(x->ch[0],k);
    if (t==-inf) return x->val;
    return t;
}
int main()
{
    #ifdef absi2011
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    #endif
    srand(2011719);
    null=new_node();
    null->ch[0]=null;
    null->ch[1]=null;
    null->size=0;
    null->weight=-1;
    node * root=null;
    int n;
    scanf("%d",&n);
    int i;
    for (i=0;i<n;i++)
    {
        int oper;
        scanf("%d",&oper);
        int x;
        scanf("%d",&x);
        if (oper==1)
        {
            insert_node(root,new_node(x));
        }
        else if (oper==2)
        {
            delete_node(root,x);
        }
        else if (oper==3)
        {
            printf("%d\n",get_rank(root,x));
        }
        else if (oper==4)
        {
            printf("%d\n",find_kth(root,x));
        }
        else if (oper==5)
        {
            printf("%d\n",find_small(root,x));
        }
        else if (oper==6)
        {
            printf("%d\n",find_big(root,x));
        }
    }
    return 0;
}

登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter