HDU - 2475:Box(splay维护森林)

There are N boxes on the ground, which are labeled by numbers from 1 to N. The boxes are magical, the size of each one can be enlarged or reduced arbitrarily.
Jack can perform the “MOVE x y” operation to the boxes: take out
box x; if y = 0, put it on the ground; Otherwise, put it inside box y.
All the boxes inside box x remain the same. It is possible that an
operation is illegal, that is, if box y is contained (directly or
indirectly) by box x, or if y is equal to x.

In the following picture, box 2 and 4 are directly inside box 6,
box 3 is directly inside box 4, box 5 is directly inside box 1, box 1
and 6 are on the ground.

The picture below shows the state after Jack performs “MOVE 4 1”:

Then he performs “MOVE 3 0”, the state becomes:

During a sequence of MOVE operations, Jack wants to know the root
box of a specified box. The root box of box x is defined as the most
outside box which contains box x. In the last picture, the root box of
box 5 is box 1, and box 3’s root box is itself.

InputInput contains several test cases.

For each test case, the first line has an integer N (1 <= N <= 50000), representing the number of boxes.

Next line has N integers: a1, a2, a3, ... , aN (0 <= ai <= N),
describing the initial state of the boxes. If ai is 0, box i is on the
ground, it is not contained by any box; Otherwise, box i is directly
inside box ai. It is guaranteed that the input state is always correct
(No loop exists).

Next line has an integer M (1 <= M <= 100000), representing the number of MOVE operations and queries.

On the next M lines, each line contains a MOVE operation or a query:

1.  MOVE x y, 1 <= x <= N, 0 <= y <= N, which is described above. If an operation is illegal, just ignore it.

2.  QUERY x, 1 <= x <= N, output the root box of box x.

OutputFor each query, output the result on a single line. Use a blank line to separate each test case.Sample Input

2
0 1
5
QUERY 1
QUERY 2
MOVE 2 0
MOVE 1 2
QUERY 1
6
0 6 4 6 1 0
4
MOVE 4 1
QUERY 3
MOVE 1 4
QUERY 1

Sample Output

1
1
2

1
1

题意:给定一些盒子,以及盒子的嵌套关系,现在有一些操作,可以把盒子移到另外的盒子里。一些询问,问包含x盒子的最外面的盒子是哪个。

思路:题意转化一下,就是森林,Cut(x,y)操作是可以把x为根的子树砍下来,接在y节点下面。 询问Query(x)就是查询x的根。

(模型很裸,还是写一下装进模板里。

像这样维护森林,求根,并查集肯定不够。我们用括号序列来做。

  • 那么点x的子树就的区间就是[x,x+N];
  • 点y在x的子树里,当且仅当pos[x]<=pos[y]<=pos[y+N]<=pos[x+N];(pos是点在splay里面的排名。)
  • 把x加到y的子树里,即[y,y+N]里面加序列[x,x+N];可以把后者加到y的后面。
#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const int maxn=100010;
int ch[maxn][2],fa[maxn],Laxt[maxn],Next[maxn],To[maxn],cnt,dd,N;
int get(int x){ return ch[fa[x]][1]==x; }
void rotate(int x)
{
    int old=fa[x],fold=fa[old],opt=get(x);
    fa[x]=fold; fa[old]=x; fa[ch[x][opt^1]]=old;
    ch[old][opt]=ch[x][opt^1];ch[x][opt^1]=old;
    if(fold) ch[fold][ch[fold][1]==old]=x;
}
void splay(int x,int y)
{
    for(int f;(f=fa[x])!=y;rotate(x)){
        if(fa[f]!=y) rotate(get(x)==get(f)?f:x);
    }
}
void add(int u,int v)
{
    Next[++cnt]=Laxt[u]; Laxt[u]=cnt; To[cnt]=v;
}
void dfs(int u) //按照括号序建spaly,起初全是单链。
{
    fa[u]=dd; ch[dd][1]=u; dd=u;
    for(int i=Laxt[u];i;i=Next[i]) dfs(To[i]);
    fa[u+N]=dd; ch[dd][1]=u+N; dd=u+N;
}
void build()
{
    for(int i=Laxt[0];i;i=Next[i]){
        dd=0; dfs(To[i]);
    }
}
int query(int x)
{
    splay(x,0);
    int now=x;
    while(ch[now][0]) now=ch[now][0];
    return now;
}
void move(int a,int b)
{
    if(a==b) return ;  //不合法1
    splay(a,0);
    splay(a+N,a);
    for(int t=b;t;t=fa[t]) if(ch[a+N][0]==t) return ; //不合法2
    int x=ch[a][0],y=ch[a+N][1]; fa[x]=fa[y]=ch[a][0]=ch[a+N][1]=0;
    int t=y; while(ch[t][0]) t=ch[t][0];
    splay(t,0); fa[x]=t; ch[t][0]=x;//[a,a+N]区间分离出来,合并剩余部分
    //右边的最小值放根,再合并,保证平衡。
    if(b==0) return ;
    splay(b,0);
    t=ch[b][1]; while(ch[t][0]) t=ch[t][0];
    splay(t,b);// t此时没有左儿子,把[a,a+N]挤进去。
    fa[a]=t; ch[t][0]=a;
}
int main()
{
    int M,x,y,T=0; char opt[6];
    while(~scanf("%d",&N)){
        if(T) printf("\n");
        else T++;
        rep(i,0,N) Laxt[i]=0; cnt=0;
        rep(i,0,N+N) ch[i][0]=ch[i][1]=0;
        rep(i,1,N){
            scanf("%d",&x);
            add(x,i);
        }
        build();
        scanf("%d",&M);
        rep(i,1,M){
            scanf("%s",opt);
            if(opt[0]==‘Q‘){
                scanf("%d",&x);
                printf("%d\n",query(x));
            }
            else {
                scanf("%d%d",&x,&y);
                move(x,y);
            }
        }
    }
    return 0;
}

原文地址:https://www.cnblogs.com/hua-dong/p/10116963.html

时间: 2024-12-24 14:35:00

HDU - 2475:Box(splay维护森林)的相关文章

HDU 2475 BOX 动态树 Link-Cut Tree 动态树模板

Box Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) [Problem Description] There are N boxes on the ground, which are labeled by numbers from 1 to N. The boxes are magical, the size of each one can be enlarged or r

HDOJ 题目2475 Box(link cut tree去点找祖先)

Box Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2374    Accepted Submission(s): 718 Problem Description There are N boxes on the ground, which are labeled by numbers from 1 to N. The boxes

HDU 3213 Box Relations(拓扑排序构造)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3231 题意:有n个长方体,四种限制条件.(1)I x y x和y有相交:(2)X/Y/Z  x y x的最大X/Y/Z坐标小于y的最大X/Y/Z.构造出这样的n个长方体. 思路:首先,XYZ三个方向是可以分开考 虑的.那么我们可以一个个分别求解.将每个长方体拆成左上角右下角两个点,我们假设现在考虑X方向,也即是一个长方体对应两个X方向的点,共2*n个点, 边<i,j>表示i小于j,那么首先有边&l

BZOJ 3786 星系探索 Splay维护树的入栈出栈序

题目大意:给出一棵树,要求有以下这些操作:1.求出一个节点到根的点权和.2.将一个节点的父亲改变.3.将一个子树中的每一个节点都加上一个权值. 思路:LCT就不用想了,因为有子树操作.然后就是一个很神奇的东西了,就是Splay维护树的入栈出栈序.这个玩应是做了这个题之后才知道的.但是感觉真的很dio. 首先,我们先按照题意,将树建出来.然后从根开始深搜,这样一个点进入DFS函数和出DFS函数的时候就会有两个时间点,就是入栈的时间和出栈的时间.然后利用Splay维护一个序列,就是入栈出栈的顺序.在

bzoj1503 Splay 维护名次数,支持删除

题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1503 题解: 维护一颗Splay和一个外部变量,树中每个节点表示一个人,节点权值a + 外部变量delta = 该员工工资. 细节看代码. 注意:一进来工资就低于最低工资的人不能算是“离开公司”的人. 1 #include <cstdio> 2 #define fprintf(...) 3 #define maxn 100100 4 5 struct Splay { 6 int key[m

BZOJ 2329 HNOI 2011 括号修复 Splay维护最大连续子段和

题目大意:给出一个括号序列,问一段区间最少需要修改多少括号使得这一段括号变成一段完整的括号序列. 思路:题解见http://ydcydcy1.blog.163.com/blog/static/2160890402013116111134791/ OTZ ydc 维护起来稍微有些麻烦啊.. CODE: #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #d

bzoj3173 Splay 维护前缀中的最大值

大致题意: 有一个空序列,依次插入1~N到该序列中,每次制定插入的位置,每次插入完成返回当前序列的LIS的长度. 题解: 设dp[i]表示 前缀1~i的最长上升子序列的长度. 因为是按照递增顺序插入的,所以dp[i] = max{ dp[j] | j<i },答案 ans=max{ dp[i] | i in [1,len] } 因为要支持动态插入,所以要用BST来做,每个节点代表一个位置(即树的中序遍历就是该序列),每个节点维护dp[i]和 dpmax[i] = max{ dp[i] | i i

HDU 1326 Box of Bricks(简单数学题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1326 Problem Description Little Bob likes playing with his box of bricks. He puts the bricks one upon another and builds stacks of different height. ``Look, I've built a wall!'', he tells his older siste

HDU 3436 Queue-jumpers Splay

这题用Splay写得我蛋疼菊紧,4000b的代码还有debug半天,看来我的splay姿势还不够好a = = 首先N是很大的,所以离散化是必然的,把要Top操作的和要Query操作的编号单独划分为一个区间,然后再中间会产生其他的区间,把这些区间缩点,然后离散化就好了. 三个操作其实不难实现,Top操作只要先把这个节点删除,然后把节点的其他值不变,key值变成一个极小值再重新插入就好,可以把极小值直接设成0,因为据说splay是稳定的,不过保险起见我还是设置了一个tpos变量维护了一下当前的极小值