[HDU2475]Box

Problem

先告诉你每个盒子在哪个盒子的内部
接下来有M个操作:
可以把一个盒子及里面的盒子移到另外一个盒子的内部
或者询问你某个盒子最外面的盒子是哪个

Solution

首先可以建成一个图,然后先dfs一遍,用dfs序加入多棵splay
然后移动操作就是区间切割,询问操作就是这棵splay中最小的值

Notice

注意,因为有多棵splay,用0这个虚点连接起来。所以细节非常难处理

Code

#pragma GCC optimize(2)
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define sqz main
#define ll long long
#define reg register int
#define rep(i, a, b) for (reg i = a; i <= b; i++)
#define per(i, a, b) for (reg i = a; i >= b; i--)
#define travel(i, u) for (reg i = head[u]; i; i = edge[i].next)
const int INF = 1e9, N = 100000;
const double eps = 1e-6, phi = acos(-1);
ll mod(ll a, ll b) {if (a >= b || a < 0) a %= b; if (a < 0) a += b; return a;}
ll read(){ ll x = 0; int zf = 1; char ch; while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
if (ch == '-') zf = -1, ch = getchar(); while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); return x * zf;}
void write(ll y) { if (y < 0) putchar('-'), y = -y; if (y > 9) write(y / 10); putchar(y % 10 + '0');}
int Left[N + 5], Right[N + 5], T[N * 2 + 5], head[N + 5];
int point, num, root, time;
struct Node
{
    int vet, next;
}edge[N + 5];
void add(int u, int v)
{
    edge[++num].vet = v;
    edge[num].next = head[u];
    head[u] = num;
}
struct node
{
    int val[N + 5], parent[N + 5], son[2][N + 5];
    void Newnode(int &u, int from, int v)
    {
        u = ++point;
        val[u] = v, parent[u] = from;
        son[0][u] = son[1][u] = 0;
        if (v > 0) Left[v] = u;
        else Right[-v] = u;
    }
    void Build(int &u, int l, int r, int from)
    {
        int mid = (l + r) >> 1;
        Newnode(u, from, T[mid]);
        if (l < mid) Build(son[0][u], l, mid - 1, u);
        if (r > mid) Build(son[1][u], mid + 1, r, u);
    }
    
    void Rotate(int x)
    {
        int y = parent[x], z = parent[y];
        int l = (son[1][y] == x), r = l ^ 1;
        if (z != 0) son[son[1][z] == y][z] = x;
        parent[x] = z;
        parent[son[r][x]] = y, son[l][y] = son[r][x];
        parent[y] = x, son[r][x] = y;
    }
    void Splay(int x, int rt)
    {
        if (x == rt)  return;
        while (parent[x] != rt)
        {
            int y = parent[x], z = parent[y];
            if (z != rt)
                if ((son[0][y] == x) ^ (son[0][z] == y)) Rotate(x);
                else Rotate(y);
            Rotate(x);
        }
    }
    
    int Find_pre(int x)
    {
        x = son[0][x];
        while (son[1][x]) x = son[1][x];
        return x;
    }
    int Find_suf(int x)
    {
        x = son[1][x];
        while (son[0][x]) x = son[0][x];
        return x;
    }
    
    int Query(int x)
    {
        x = Left[x];
        Splay(x, 0);
        while (son[0][x]) x = son[0][x];
        return val[x];
    }
    void Move(int x, int y)
    {
        if (x == y) return;
        int xx = Left[x], yy = Right[x];
        Splay(xx, 0);
        Splay(yy, xx);
        int xxx = son[0][xx], yyy = son[1][yy], ttt = Find_pre(xx);
        son[0][xx] = 0, son[1][yy] = 0;
        parent[xxx] = parent[yyy] = 0;
        if (ttt) 
        {
            son[1][ttt] = yyy;
            parent[yyy] = ttt;
        }
        if (y == 0) return;
        if (Query(y) == x)
        {
            son[0][xx] = xxx, son[1][yy] = yyy;
            parent[xxx] = xx, parent[yyy] = yy;
            son[1][ttt] = 0; return;
        }
        Splay(Left[y], 0);
        ttt = Find_suf(Left[y]);
        Splay(ttt, Left[y]);
        son[0][son[1][Left[y]]] = xx;
        parent[xx] = son[1][Left[y]];
    }
}splay_tree;
void dfs(int u)
{
    T[time++] = u;
    travel(i, u)
    {
        int v = edge[i].vet;
        dfs(v);
    }
    T[time++] = -u;
}

int sqz()
{
    int n, flag = 0;
    while (~scanf("%d", &n))
    {
        if (flag) puts("");
        else flag = 1;
        point = num = time = 0;
        memset(head, 0, sizeof head);
        rep(i, 1, n)
        {
            int x = read();
            add(x, i);
        }
        dfs(0);
        int now = 0, last = 1;
        rep(i, 1, 2 * n)
        {
            if (T[i] > 0) now++;
            else now--;
            if (!now)
            {
                splay_tree.Build(root, last, i, 0);
                last = i + 1;
            }
        }
        int q = read(); char st[10];
        while(q--)
        {
            scanf("%s", st);
            if (st[0] == 'Q')
            {
                int x = read();
                printf("%d\n", splay_tree.Query(x));
            }
            else
            {
                int x = read(), y = read();
                splay_tree.Move(x, y);
            }
        }
    }
    return 0;
}

转载于:https://www.cnblogs.com/WizardCowboy/p/7631387.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值