hdu 1890 Robotic Sort (Splay树)

参考:

http://blog.csdn.net/crazy_ac/article/details/8034206

http://www.cnblogs.com/kuangbin/archive/2013/04/21/3034081.html

区间翻转

关键注意:有flip操作时,down() 的调用

#pragma comment(linker, "/STACK:102400000000,102400000000")
#include <cstdio>
#include <ctime>
#include <cstdlib>
#include <cstring>
#include <queue>
#include <string>
#include <set>
#include <stack>
#include <map>
#include <cmath>
#include <vector>
#include <iostream>
#include <algorithm>
#include <bitset>
using namespace std;


//LOOP
#define FF(i, a, b) for(int i = (a); i < (b); ++i)
#define FE(i, a, b) for(int i = (a); i <= (b); ++i)
#define FED(i, b, a) for(int i = (b); i>= (a); --i)
#define REP(i, N) for(int i = 0; i < (N); ++i)
#define CLR(A,value) memset(A,value,sizeof(A))
//INPUT
#define RI(n) scanf("%d", &n)
#define RII(n, m) scanf("%d%d", &n, &m)
#define RIII(n, m, k) scanf("%d%d%d", &n, &m, &k)
typedef long long LL;
typedef vector <int> VI;
const int INF = 0x3f3f3f3f;
const double eps = 1e-10;
const int maxn = 111111;


#define ll ch[x][0]
#define rr ch[x][1]
#define KT (ch[ch[rt][1]][0])


struct Point{
    int a, b;
    bool operator<(const Point &rhs) const
    {
        return a < rhs.a || (a == rhs.a && b < rhs.b);
    }
}P[maxn];
int id[maxn], bb[maxn];


struct SplayTree{
    int ch[maxn][2];
    int pre[maxn], sz[maxn];
    int val[maxn], flip[maxn];
    int rt, tot;


    void Rotate(int x, int f)
    {
        int y = pre[x];
        down(y); down(x);///
        ch[y][!f] = ch[x][f];
        pre[ch[x][f]] = y;
        pre[x] = pre[y];
        if (pre[x]) ch[pre[y]][ch[pre[y]][1] == y] = x;
        ch[x][f] = y;
        pre[y] = x;
        up(y);///
    }
    void Splay(int x, int goal)
    {
        down(x);///
        while (pre[x] != goal)
        {
            down(pre[pre[x]]); down(pre[x]); down(x); /// 翻转操作down!!!
            if (pre[pre[x]] == goal) Rotate(x, ch[pre[x]][0] == x);
            else
            {
                int y = pre[x], z = pre[y];
                int f = ( ch[z][0] == y );
                if (ch[y][f] == x) Rotate(x, !f), Rotate(x, f);
                else Rotate(y, f), Rotate(x, f);
            }
        }
        up(x);///
        if (goal == 0) rt = x;
    }
    void RTO(int k, int goal)
    {
        int x = rt;
        while (sz[ll] + 1 != k)
        {
            if (k < sz[ll] + 1) x = ll;
            else
            {
                k -= (sz[ll] + 1);
                x = rr;
            }
        }
        Splay(x, goal);
    }
    void up(int x)
    {
        sz[x] = 1 + sz[ll] + sz[rr];
    }
    void Reverse(int x)///
    {
//        if (!x) return ;//???
        swap(ll, rr);///!!!
        flip[x] ^= 1;
    }
    void down(int x)
    {
        if (flip[x])
        {
            Reverse(ll); Reverse(rr);
            flip[x] = 0;
        }
    }
    void newnode(int &x, int c, int f)///&
    {
        x = ++tot;
        ll = rr = 0; pre[x] = f;
        sz[x] = 1;  flip[x] = 0;


        val[x] = c; id[bb[c]] = x;
    }
    void build(int &x, int l, int r, int f)///&
    {
        if (l > r) return ;
        int m = (l + r) >> 1;
        newnode(x, m, f);
        build(ll, l, m - 1, x);
        build(rr, m + 1, r, x);


        up(x);
    }
    void Init(int n)
    {
        ch[0][0] = ch[0][1] = pre[0] = sz[0] = 0;
        val[0] = 0; flip[0] = 0;
        rt = tot = 0;


        newnode(rt, 0, 0);
        newnode(ch[rt][1], 0, rt);


        build(KT, 1, n, ch[rt][1]);///
        up(ch[rt][1]); up(rt);///
    }
    ///
    int Getnext(int x)
    {
        down(x);///flip!!!
        while (ll)
        {
            x = ll;
            down(x);
        }
        return x;
    }
    void solve(int i)
    {
        Splay(id[i], 0);
        printf("%d", sz[ch[rt][0]]);
        RTO(i, 0);
        down(id[i]);
        Splay(Getnext(ch[id[i]][1]), rt);
        Revers(KT);
    }
}sp;


int main()
{
    int n, m;
    while (scanf("%d", &n) != EOF && n)
    {
        FE(i, 1, n)
        {
            scanf("%d", &P[i].a);
            P[i].b = i;
        }
        sort(P + 1, P + 1 + n);
        FE(i, 1, n) bb[P[i].b] = i;
        sp.Init(n);
        for (int i = 1; i <= n; i++)
        {
            if (i != 1) printf(" ");
            sp.solve(i);
        }
        printf("\n");
    }
    return 0;
}


关键注意:有flip操作时,down() 的调用

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值