POJ 3481 Double Queue 伸展树splay + 删除节点

题目:

http://poj.org/problem?id=3481

题意:

有一组操作,有如下三种:
0. 0,结束操作
1. 1 k p,把一个客户k加入到队列中,优先级为p
2. 2, 把队列中优先级最高的客户取出来
3. 3, 把队列中优先级最低的客户取出来

思路:

此题多种数据结构都可以做,练习splay用

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;

#define key_val son[son[root][1]][0]
typedef long long ll;
const int N = 100010, INF = 0x3f3f3f3f, MOD = 1000000;
int son[N][2], pre[N], siz[N];
int key[N], val[N], lazy[N];
int root, num, cas;
int arr[N];
int n, m;
void new_node(int &x, int fa, int v, int vv = 0)
{
    x = ++num;
    pre[x] = fa, key[x] = v, val[x] = vv;
    siz[x] = 1, lazy[x] = 0;
    son[x][0] = son[x][1] = 0;
}
void push_up(int x)
{
    siz[x] = siz[son[x][0]] + siz[son[x][1]] + 1;
}
void push_down(int x)
{
}
void _rotate(int x, int dir)
{
    int y = pre[x];
    push_down(y), push_down(x);
    son[y][!dir] = son[x][dir], pre[son[x][dir]] = y;
    if(pre[y]) son[pre[y]][son[pre[y]][1]==y] = x;
    pre[x] = pre[y];
    son[x][dir] = y, pre[y] = x;
    push_up(y);
}
void splay(int x, int goal)
{
    push_down(x);
    while(pre[x] != goal)
    {
        int y = pre[x];
        if(pre[y] == goal) _rotate(x, son[y][0] == x);
        else
        {
            int dir = son[pre[y]][0] == y;
            if(son[y][dir] == x) _rotate(x, !dir), _rotate(x, dir);
            else _rotate(y, dir), _rotate(x, dir);
        }
    }
    push_up(x);
    if(goal == 0) root = x;
}
//void splay(int x, int goal)
//{
//    push_down(x);
//    while(pre[x] != goal) _rotate(x, son[pre[x]][0] == x);
//    push_up(x);
//    if(goal == 0) root = x;
//}
int get_prec(int x)
{
    int t = son[x][0];
    push_down(t);
    while(son[t][1]) t = son[t][1], push_down(t);
    return t;
}
int get_subs(int x)
{
    int t = son[x][1];
    push_down(t);
    while(son[t][0]) t = son[t][0], push_down(t);
    return t;
}
int get_min(int x)
{
    push_down(x);
    while(son[x][0]) x = son[x][0], push_down(x);
    return x;
}
int get_max(int x)
{
    push_down(x);
    while(son[x][1]) x = son[x][1], push_down(x);
    return x;
}
int _find(int v)
{
    int x = root;
    push_down(x);
    while(x && key[x] != v) x = son[x][key[x]<v], push_down(x);
    return x;
}
void _delete(int v)
{
    int x = _find(v);
    if(!x) return;
    splay(x, 0);
    if(!son[root][0] || !son[root][1])
        root = son[root][0] + son[root][1], pre[root] = 0;
    else
    {
        int t = get_subs(root);
        splay(t, root);
        son[son[root][1]][0] = son[root][0], root = son[root][1];
        pre[son[root][0]] = root, pre[root] = 0;
        push_up(root);
    }
}
int get_kth(int k)
{
    int x = root;
    push_down(x);
    while(siz[son[x][0]] + 1 != k)
    {
        if(siz[son[x][0]] + 1 > k) x = son[x][0];
        else k -= (siz[son[x][0]] + 1), x = son[x][1];
        push_down(x);
    }
    return x;
}
bool _insert(int v, int vv)
{
    if(!root) new_node(root, 0, v, vv);
    else
    {
        int x = root;
        while(son[x][key[x]<v])
        {
//            if(key[x] == v)
//            {
//                splay(x, 0);
//                return false;
//            }
            x = son[x][key[x]<v];
        }
        new_node(son[x][key[x]<v], x, v, vv);
        splay(son[x][key[x]<v], 0);
    }
    return true;
}
void build(int &x, int l, int r, int fa)
{
    if(l > r) return;
    int mid = (l + r) >> 1;
    new_node(x, fa, arr[mid]);
    build(son[x][0], l, mid-1, x);
    build(son[x][1], mid+1, r, x);
    push_up(x);
}
int query(int l, int r)
{
    splay(get_kth(l), 0);
    splay(get_kth(r+2), root);
    return val[key_val];
}
void update(int l, int r, int v)
{
    splay(get_kth(l), 0);
    splay(get_kth(r+2), root);
    lazy[key_val] += v;
    val[key_val] += v * siz[key_val];
}
void init()
{
    root = num = 0;
    son[root][0] = son[root][1] = siz[root] = pre[root] = 0;
    key[root] = val[root] = lazy[root] = 0;
    new_node(root, 0, 0);
    new_node(son[root][1], root, 0);
    build(key_val, 1, n, son[root][1]);
    push_up(son[root][1]), push_up(root);
}
int main()
{
    int a, b;
    while(scanf("%d", &n), n)
    {
        if(n == 1)
        {
            scanf("%d%d", &a, &b);
            _insert(b, a);
        }
        else if(n == 2)
        {
            int x = get_max(root);
            printf("%d\n", val[x]);
            _delete(key[x]);
        }
        else
        {
            int x = get_min(root);
            printf("%d\n", val[x]);
            _delete(key[x]);
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值