POJ 2892 Tunnel Warfare splay或set

题目:

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

题意:

有一个数列从1到n,有三种操作:
1. D x,意为毁掉某个数字x
2. Q x,意为询问与第x直接或间接相连的数字个数(毁掉的算间隔)
3. R,倒序恢复毁掉的数字

思路:

对于询问,如果x被毁掉,自然是0,如果没有被毁掉,那么就是x左边被毁掉的最近点和右边被毁掉的最近点之间的数字个数了。于是我们把毁掉的数字插入到树中,对于每次询问,查找x的前驱和后继,就可以求出答案了。用splay和set都做了一下, set跑了900ms,splay跑了500ms

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

#define key_val son[son[root][1]][0]
const int N = 50010;
int son[N][2], key[N], pre[N];
int root, num;
int st[N], top;
bool vis[N];
int n, m;
void new_node(int &x, int fa, int v)
{
    x = ++num;
    pre[x] = fa, key[x] = v;
    son[x][0] = son[x][1] = 0;
}
void _rotate(int x, int dir)
{
    int y = pre[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;
}
void splay(int x, int goal)
{
    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);
        }
    }
    if(goal == 0) root = x;
}
int get_prec(int x)
{
    int t = son[x][0];
    while(son[t][1]) t = son[t][1];
    return t;
}
int get_subs(int x)
{
    int t = son[x][1];
    while(son[t][0]) t = son[t][0];
    return t;
}
int _find(int v)
{
    int x = root;
    while(x && key[x] != v) x = son[x][key[x]<v];
    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);
        key_val = son[root][0], root = son[root][1];
        pre[son[root][0]] = root, pre[root] = 0;
    }
}
void _insert(int v)
{
    if(! root) new_node(root, 0, v);
    else
    {
        int x = root;
        while(son[x][key[x]<v]) x = son[x][key[x]<v];
        new_node(son[x][key[x]<v], x, v);
        splay(son[x][key[x]<v], 0);
    }
}
int main()
{
    while(~ scanf("%d%d", &n, &m))
    {
        for(int i = 1; i <= n; i++) vis[i] = false;
        root = num = 0;
        _insert(0), _insert(n+1);
        char ch;
        int a;
        top = 0;
        for(int i = 1; i <= m; i++)
        {
            scanf(" %c", &ch);
            if(ch == 'D')
            {
                scanf("%d", &a);
                _insert(a);
                st[top++] = a;
                vis[a] = true;
            }
            else if(ch == 'Q')
            {
                scanf("%d", &a);
                if(vis[a]) printf("0\n");
                else
                {
                    _insert(a);
                    int t1 = get_prec(root), t2 = get_subs(root);
                    printf("%d\n", key[t2] - key[t1] - 1);
                    _delete(a);
                }
            }
            else
            {
                a = st[--top];
                _delete(a);
                vis[a] = false;
            }
        }
    }
    return 0;
}
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <set>
using namespace std;
const int N = 50010;
int st[N], top;
bool vis[N];
int main()
{
    int n, m;
    while(~ scanf("%d%d", &n, &m))
    {
        set<int> ste;
        set<int> :: iterator p;
        top = 0;
        char ch;
        int a;
        ste.insert(0), ste.insert(n+1);
        for(int i = 1; i <= n; i++) vis[i] = false;
        for(int i = 1; i <= m; i++)
        {
            scanf(" %c", &ch);
            if(ch == 'D')
            {
                scanf("%d", &a);
                ste.insert(a);
                vis[a] = true;
                st[top++] = a;
            }
            else if(ch == 'Q')
            {
                scanf("%d", &a);
                if(vis[a]) printf("0\n");
                else
                {
                    ste.insert(a);
                    p = ste.lower_bound(a);
                    int b = *(--p), c = *(++(++p));
                    printf("%d\n", c - b - 1);
                    ste.erase(a);
                }
            }
            else
            {
                int a = st[--top];
                ste.erase(a);
                vis[a] = false;
            }
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值