poj 3481 分类: poj 2015-0...


裸的平衡树,于是就1A了,于是这就是我以后的平衡树模板
不要问我为什么用宏定义和结构体写出一个奇怪[极像数组并且可以用数组代替]的表达方式,
这是我最后的倔强。。。。。。

UPD:平衡树模板已换!


#include<cstdio>
#include<cstdlib>
#include<ctime>
#include<cmath>
#include<iostream>
#include<algorithm>

#define fa(x)  mp[x].fa
#define cnt(x) mp[x].cnt
#define c(x,y) mp[x].c[y]

const int MAXN = 1000000, L = 0 ,R = 1;

struct Treenode{int c[2],fa,cnt;}mp[MAXN] = {0}, emp = {0} ;//cnt:size
int root , val[MAXN] = {0} , tot = 0;

inline void update(const int &si)
{cnt(si) = cnt(c(si,L)) + cnt(c(si,R)) + 1;}

inline int  build(const int ll,const int rr)
{
    int mid = (ll + rr)>>1 , newd;
    if(ll == rr) {update(ll); return ll;}
    if(rr>mid)c(mid,R) = newd = build(mid+1,rr) , fa(newd) = mid;
    if(ll<mid)c(mid,L) = newd = build(ll,mid-1) , fa(newd) = mid;
    update(mid);  return mid;
}

inline void rotate(const int x,int &k)
{
    int y = fa(x) , z = fa(y);
    int i = (c(y,R) == x) ,j = i^1 , fi = (c(z,R) == y);

    if(y != k) c(z,fi) = x;else  k = x;

    fa(x) = z;fa(y) = x ;fa(c(x,j))= y;
    c(y,i) = c(x,j); c(x,j) = y ;
    update(y);update(x);
}
inline void splay(const int x,int &k)
{
    while(x != k)
    {
      int y = fa(x) , z =fa(y);   
      if(y != k)
      {
        if((fa(y)==c(z,R))^(fa(x)==c(y,R)))rotate(x, k);
        else   rotate(y, k);
      }
      rotate(x,k);
    }
}
inline int find(int x)
{
    int now = root;
    while(true)
    {
        int p = cnt(c(now,L)) + 1;
        if(p == x) break;
        else if(p > x)  now = c(now,L);
        else   { x -= p;now = c(now,R);}
    }
    return now;
}
inline void del(const int x)
{
    splay(x ,root);
    if(c(root,R))
    {
        int y = find(cnt(c(x,L)) + 1 +1);
        splay(y , c(x,R));root = y;fa(y) = 0;
        c(y,L) = c(x,L);fa(c(x,L)) = y;update(y);
        mp[x] = emp;
    }
    else
    {
        root = c(root,L);fa(root) = 0;
        mp[x] = emp;
    }
    tot --;
}
inline void insert(const int x)
{
    int now = root , tag;

    if(!tot){root = x; tot ++;return;}
    while(1)
    {
        if(val[x] < val[now])
         if(c(now,L))now = c(now,L);else{tag = L;break;}
        else
         if(c(now,R))now = c(now,R);else{tag = R;break;}
    }
    c(now,tag) = x;fa(x) = now;
    cnt(x) = 1,now = x;
    while(now != root){now = fa(now);update(now);}
    splay(x,root);
    tot ++;
}
int main()
{

#ifndef ONLINE_JUDGE    
    freopen("poj3481.in","r",stdin);
    freopen("poj3481.out","w",stdout);
#endif  
    root = 0;
    while(true)
    {
        int c , a , b;
        scanf("%d",&c);
        if(c == 0)  break;
        else if(c == 1)
        {scanf("%d%d",&a,&b); val[a] = b; insert(a);}
        else
        {   
           if(!tot) {printf("0\n");continue;}
           if(c == 2)
            {printf("%d\n",(a = find(tot))); del(a);}
            else if(c == 3)
            {printf("%d\n",(a = find(1))); del(a); }
            else return -1;
        }
    }
#ifndef ONLINE_JUDGE    
    fclose(stdin);
    fclose(stdout);
#endif  
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

转载于:https://www.cnblogs.com/dashgua/p/4723099.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值