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的后面。

(LCT应该更贴合这个题

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const int maxn=;
int ch[maxn][],fa[maxn],Laxt[maxn],Next[maxn],To[maxn],cnt,dd,N;
int get(int x){ return ch[fa[x]][]==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^]]=old;
ch[old][opt]=ch[x][opt^];ch[x][opt^]=old;
if(fold) ch[fold][ch[fold][]==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][]=u; dd=u;
for(int i=Laxt[u];i;i=Next[i]) dfs(To[i]);
fa[u+N]=dd; ch[dd][]=u+N; dd=u+N;
}
void build()
{
for(int i=Laxt[];i;i=Next[i]){
dd=; dfs(To[i]);
}
}
int query(int x)
{
splay(x,);
int now=x;
while(ch[now][]) now=ch[now][];
return now;
}
void move(int a,int b)
{
if(a==b) return ; //不合法1
splay(a,);
splay(a+N,a);
for(int t=b;t;t=fa[t]) if(ch[a+N][]==t) return ; //不合法2
int x=ch[a][],y=ch[a+N][]; fa[x]=fa[y]=ch[a][]=ch[a+N][]=;
int t=y; while(ch[t][]) t=ch[t][];
splay(t,); fa[x]=t; ch[t][]=x;//[a,a+N]区间分离出来,合并剩余部分
//右边的最小值放根,再合并,保证平衡。
if(b==) return ;
splay(b,);
t=ch[b][]; while(ch[t][]) t=ch[t][];
splay(t,b);// t此时没有左儿子,把[a,a+N]挤进去。
fa[a]=t; ch[t][]=a;
}
int main()
{
int M,x,y,T=; char opt[];
while(~scanf("%d",&N)){
if(T) printf("\n");
else T++;
rep(i,,N) Laxt[i]=; cnt=;
rep(i,,N+N) ch[i][]=ch[i][]=;
rep(i,,N){
scanf("%d",&x);
add(x,i);
}
build();
scanf("%d",&M);
rep(i,,M){
scanf("%s",opt);
if(opt[]=='Q'){
scanf("%d",&x);
printf("%d\n",query(x));
}
else {
scanf("%d%d",&x,&y);
move(x,y);
}
}
}
return ;
}

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

  1. Box HDU - 2475 (Splay 维护森林)

    Box \[ Time Limit: 5000 ms \quad Memory Limit: 32768 kB \] 题意 给出 \(n\) 个箱子的包含关系,每次两种操作. 操作 \(1\):把 \ ...

  2. hdu 2475 BOX (splay)

    版权声明:本文为博主原创文章,未经博主允许不得转载. hdu 2475 Splay树是一种神奇的东西... 题意: 有一些箱子,要么放在地上,要么放在某个箱子里面 . 现在有两种操作: (1) MOV ...

  3. HDU 2475 BOX 动态树 Link-Cut Tree

    Box Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) [Problem De ...

  4. HDU 2475 Box

    Box Time Limit: 5000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 247564 ...

  5. HDU 2475 Box 树型转线型 + 伸展树

    树型转线型.第一次听说这个概念. . . , 可是曾经已经接触过了,如LCA的预处理部分和树链剖分等.可是没想到还能这么用,三者虽说有不同可是大体思想还是非常相近的,学习了. 推荐博客http://b ...

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

    Box Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submi ...

  7. 【BZOJ 3729】3729: Gty的游戏 (Splay维护dfs序+博弈)

    未经博主同意不得转载 3729: Gty的游戏 Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 448  Solved: 150 Description ...

  8. HNOI2004宠物收养所(splay维护二叉搜索树模板题)

    描述 最近,阿Q开了一间宠物收养所.收养所提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物.每个领养者都希望领养到自己满意的宠物,阿Q根据领养者的要求通过他自己发明的一个特殊的公式,得出该领 ...

  9. BZOJ 3729 splay维护DFS序+博弈论

    思路: 这像是 阶梯Nim之类的东西 我们 直接把sg函数 设成mod(L+1)的 一棵子树 向下的奇数层上的石子xor起来 就是答案 有加点和改值的操作 就splay维护一下 //By Sirius ...

随机推荐

  1. Codeforces D - High Load

    D - High Load 因为要出口节点距离最小,所以除了根节点(根节点连接k个儿子)其他节点的儿子只能有一个,其他情况下的距离都比这个长,因为如果不是这样,那么根节点连接的子树数量就小与k,那么每 ...

  2. 认识网页:html + css + JavaScript

    参考资料:爬虫课程 认识网页 使用chrome,右键检查,查看网页源码,左侧的html,右侧的css,底下的JavaScript. 网页 = HTML(内容) + CSS(样式) + JavaScri ...

  3. 20170624xlVBA正则分割分类汇总

    Sub RegExpSubtotal() '声明变量 Dim Regex As Object '正则对象 Dim Dic As Object '字典对象 Dim Key As String '关键字 ...

  4. codeforces 993c//Careful Maneuvering// Codeforces Round #488 by NEAR (Div. 1)

    题意:x轴-100和+100的有敌人飞船,纵坐标由输入数据给出,我方有2飞船在x轴0,y坐标待定.0时刻时敌人同时向我方2飞船发出光线,光线会穿透飞船打到敌人自己,问2飞船放在哪敌人损失最大? 假如- ...

  5. codeforces 536a//Tavas and Karafs// Codeforces Round #299(Div. 1)

    题意:一个等差数列,首项为a,公差为b,无限长.操作cz是区间里选择最多m个不同的非0元素减1,最多操作t次,现给出区间左端ll,在t次操作能使区间全为0的情况下,问右端最大为多少. 这么一个简单题吞 ...

  6. Android 列表使用(ListView GridView Gallery图片计时滚动)

    ListView 作用: 1.将数据填充到布局. 2.处理用户的选择点击等操作. 根据列表的适配器类型,列表分为三种,ArrayAdapter,SimpleAdapter和SimpleCursorAd ...

  7. week 1

    day1 订正 学习AC自动机 day2 mobius反演 对偶图 codeforces day3 ZR模拟赛 订正 day4 复习AC自动机 题库 https://www.cnblogs.com/c ...

  8. Python基础--数据类型

    一.数据类型是什么鬼? 计算机顾名思义就是可以做数学计算的机器,因此,计算机程序理所当然地可以处理各种数值.但是,计算机能处理的远不止数值,还可以处理文本.图形.音频.视频.网页等各种各样的数据,不同 ...

  9. python-day67--MTV之Template

    一.什么是模板? html+模板语法 二.模版包括在使用时会被值替换掉的 变量,和控制模版逻辑的 标签. 三.嵌入变量的三种方式: def current_time(req): # ========= ...

  10. POJ-3894 迷宫问题 (BFS+路径还原)

    定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, ...