当前位置:网站首页>hdu2475 Box
hdu2475 Box
2022-08-08 14:56:00 【51CTO】
http://www.elijahqi.win/2018/03/04/hdu2475-box/
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 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.
Input
Input 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.
Output
For 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
因为有根树 所以lct不可以换根 那怎么办 设f[x]表示在树的形态中真实的根 因为存在不合法的情况 所以先把子树切下来之后看x是否在y的上面 如果在 则再把它接回去 注意接回去的时候需要将 splay到根 否则位置将不对 如果该操作是合法的那么选择将x接到y上去 同时注意改变真实树中的f数组
边栏推荐
猜你喜欢
logistic regression model - based on R
【服务器数据恢复】Ext4文件系统fsck后mount不上并报错的数据修复案例
从洞察到决策,一文解读标签画像体系建设方法论丨DTVision分析洞察篇
AD添加LOGO
浅谈 Redis 的底层数据结构
Shell三剑客之sed命令详解
【控制】动力学建模简介 --> 牛顿-欧拉 (Newton-Euler) 法和拉格朗日 (Lagrange) 法
Shell Three Musketeers-----sed command
2022-08-07 第五小组 顾祥全 学习笔记 day31-集合-Map集合
直播卖货APP——为何能得到商家和用户的喜欢?
随机推荐
什么是发饰hair accessories?
JS-Bom-while(计算闰年)
【小码匠自习室】[NOI Online 2020-2 入门组] 未了:可恶的精度会让你焦头烂额
[内部资源] 想拿年薪30W的软件测试人员,这份资料必须领取
EasyExcel导入校验必填项不能为空
企业开发小程序有什么优势?为什么要开发小程序?
Zhaoqi Technology Innovation and Entrepreneurship Event Event Platform, Investment and Financing Matchmaking, Online Live Roadshow
如何成为团队核心?从写出不可维护的代码开始
儿子满墙奖状却没考上重点高中,妈妈愤怒撕下痛哭:不读出去打工
面试题 17.05. 字母与数字
vsomeip环境搭建及helloworld测试例跑通
shell regular expression, Three Musketeers grep command
Elegantly detect and update web applications in real time
【小码匠自习室】ABC180-C: 马虎是小孩的天性吗?
AT2382-[AGC015D]A or...or B Problem
Install Update(Patches) on ESXi
HMS Core Analysis Service Intelligent Operation Version 6.5.1 Launched
看三年的CRUD程序员如何解决数据库死锁的
分布式服务治理
Iptables防火墙iprange模块扩展匹配规则