【HDU3436】 Queue-jumpers (Splay tree)

HDU 3436 Queue-jumpers (Splay tree)http://acm.hdu.edu.cn/showproblem.php?pid=3436


Queue-jumpers

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2521    Accepted Submission(s): 662


Problem Description
Ponyo and Garfield are waiting outside the box-office for their favorite movie. Because queuing is so boring, that they want to play a game to kill the time. The game is called “Queue-jumpers”. Suppose that there are N people numbered from 1 to N stand in a line initially. Each time you should simulate one of the following operations:
1.  Top x :Take person x to the front of the queue
2.  Query x: calculate the current position of person x
3.  Rank x: calculate the current person at position x
Where x is in [1, N].
Ponyo is so clever that she plays the game very well while Garfield has no idea. Garfield is now turning to you for help.
 

Input
In the first line there is an integer T, indicates the number of test cases.(T<=50)
In each case, the first line contains two integers N(1<=N<=10^8), Q(1<=Q<=10^5). Then there are Q lines, each line contain an operation as said above. 
 

Output
For each test case, output “Case d:“ at first line where d is the case number counted from one, then for each “Query x” operation ,output the current position of person x at a line, for each “Rank x” operation, output the current person at position x at a line.
 

Sample Input
  
  
3 9 5 Top 1 Rank 3 Top 7 Rank 6 Rank 8 6 2 Top 4 Top 5 7 4 Top 5 Top 2 Query 1 Rank 6
 

Sample Output
  
  
Case 1: 3 5 8 Case 2: Case 3: 3 6
 

Author
wzc1989
 

Source


#include <cstdio>
#include <cstring>
#include <algorithm>

const int MAXD = 100010;
using namespace std;

int node, tx[MAXD], begin[MAXD], end[MAXD], nodep[MAXD];
int num[2 * MAXD], size[2 * MAXD], left[2 * MAXD], right[2 * MAXD], pre[2 * MAXD], key[2 * MAXD];
char b[MAXD][10];
int ob[MAXD], T, N, Q;

inline void update(int cur)
{
    size[cur] = size[left[cur]] + size[right[cur]] + num[cur];
}

inline void newnode(int &cur, int k)
{
    cur = ++ node;
    size[cur] = num[cur] = end[k] - begin[k] + 1;
    key[cur] = k;
    nodep[k] = cur;
    left[cur] = right[cur] = 0;
}

void build(int &cur, int x, int y, int p)
{
    int mid = (x + y) / 2;
    newnode(cur, mid);
    pre[cur] = p;
    if (x == y) return ;
    if (x < mid) build(left[cur], x, mid - 1, cur);
    if (mid < y) build(right[cur], mid + 1, y, cur);
    update(cur);
}

inline void init(void)
{
    int k;
    scanf("%d%d", &N, &Q);
    k = 0;
    tx[k ++] = 0;
    for (int i = 0; i < Q; i ++)
    {
        scanf("%s%d", b[i], &ob[i]);
        if (b[i][0] == 'T' || b[i][0] == 'Q') tx[k ++] = ob[i];
    }
    tx[k ++] = N;
    sort(tx, tx + k);
    N = 0;
    for (int i = 1; i < k; i ++)
        if (tx[i] != tx[i - 1])
        {
            if (tx[i] - tx[i - 1] > 1)
            {
                begin[N] = tx[i - 1] + 1, end[N] = tx[i] - 1;
                ++ N;
            }
            begin[N] = end[N] = tx[i];
            ++ N;
        }
    T = node = left[0] = right[0] = size[0] = num[0] = 0;
    build(T, 0, N - 1, 0);
}

inline void leftrotate(int x)
{
    int y = right[x], p = pre[x];
    right[x] = left[y];
    if (right[x]) pre[right[x]] = x;
    left[y] = x;
    pre[x] = y;
    pre[y] = p;
    if (p == 0) T = y;
    else right[p] == x ? right[p] = y : left[p] = y;
    update(x);
}

inline void rightrotate(int x)
{
    int y = left[x], p = pre[x];
    left[x] = right[y];
    if (left[x]) pre[left[x]] = x;
    right[y] = x;
    pre[x] = y;
    pre[y] = p;
    if (p == 0) T = y;
    else right[p] == x ? right[p] = y : left[p] = y;
    update(x);
}

inline void splay(int x, int goal)
{
    int y, z;
    for ( ; ; )
    {
        if ((y = pre[x]) == goal) break;
        if ((z = pre[y]) == goal) right[y] == x ? leftrotate(y) : rightrotate(y);
        else
        {
            if (right[z] == y)
            {
                if (right[y] == x) leftrotate(z), leftrotate(y);
                else rightrotate(y), leftrotate(z);
            }
            else
            {
                if (left[y] == x) rightrotate(z), rightrotate(y);
                else leftrotate(y), rightrotate(z);
            }
        }
    }
    update(x);
}

inline int BS(int x)
{
    int max, mid, min;
    min = 0, max = N;
    for (;;)
    {
        mid = (max + min) / 2;
        if (mid == min) break;
        if (begin[mid] <= x) min = mid;
        else max = mid;
    }
    return mid;
}

int Delete(int &cur, int p)
{
    int k;
    if (cur == T || right[cur] == 0)
    {
        if (cur == T)
        {
            k = Delete(left[cur], cur);
            left[k] = left[T], right[k] = right[T];
            if (left[k]) pre[left[k]] = k;
            if (right[k]) pre[right[k]] = k;
            T = k;
            pre[T] = 0;
            update(T);
        }
        else
        {
            k = cur;
            if (left[k]) pre[left[k]] = p;
            cur = left[k];
        }
        return k;
    }
    else
    {
        k = Delete(right[cur], cur);
        update(cur);
        return k;
    }
}

void Insert(int &cur, int k, int p)
{
    if (cur == 0)
    {
        newnode(cur, k);
        pre[cur] = p;
        return ;
    }
    Insert(left[cur], k, cur);
    update(cur);
}

inline void Top(int x)
{
    int k, cur;
    k = BS(x);
    cur = nodep[k];
    splay(cur, 0);
    if (left[T] == 0 || right[T] == 0)
    {
        T = left[T] + right[T];
        pre[T] = 0;
    }
    else Delete(T, 0);
    Insert(T, k, 0);
    splay(node, 0);
}

inline void Query(int x)
{
    int k, cur;
    k = BS(x);
    cur = nodep[k];
    splay(cur, 0);
    printf("%d\n", size[left[cur]] + 1);
}

inline int Search(int cur, int x)
{
    int ls = left[cur], rs = right[cur], k = key[cur];
    if (x <= size[ls]) return Search(left[cur], x);
    else if (x <= size[ls] + num[cur]) return begin[k] + (x - size[ls]) - 1;
    else return Search(right[cur], x - size[ls] - num[cur]);
}

inline void Rank(int x)
{
    printf("%d\n", Search(T, x));
}

inline void solve(void)
{
    for (int i = 0; i < Q; i ++)
    {
        if (b[i][0] == 'T') Top(ob[i]);
        else if (b[i][0] == 'Q') Query(ob[i]);
        else Rank(ob[i]);
    }
}

int main(void)
{
    int t;
    scanf("%d", &t);
    for (int i = 0; i < t; i ++)
    {
        init();
        printf("Case %d:\n", i + 1);
        solve();
    }
    return 0;
}

3种操作:

RANK:求第K位

TOP:将某个人移至队首,对中间区间没有影响

QUERY:求某个人的位置

N≤10^8,要离散化。

离散化之后就是Splay的基本操作:

TOP:将目标点旋转至根部,删除后插入到队首

RANK:通过size查找

QUERY:把该点旋转至根部,左子树的大小+1


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值