当前位置:网站首页>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数组
边栏推荐
猜你喜欢
开源一夏 | 自己画一块ESP32-C3 的开发板(PCB到手)
你真的会软件测试bug分析定位嘛
sqoop连接MySQL跟本机不一致是为什么
JS-BOM-for,if(字符串转大小写)
手把手教你设计一个全局异常处理器
HMS Core Analysis Service Intelligent Operation Version 6.5.1 Launched
MySQL:Update高并发下变慢的案例及其涉及的特性
shell正则表达式,三剑客grep命令
Talking about the underlying data structure of Redis
[内部资源] 想拿年薪30W的软件测试人员,这份资料必须领取
随机推荐
shell正则表达式,三剑客grep命令
【系统设计】S3 对象存储
开源一夏 | 自己画一块ESP32-C3 的开发板(PCB到手)
兆骑科创创业赛事活动举办平台,投融资对接,线上直播路演
现在网上开户安全么?接着证券开户选择哪个证券?
基于微信小程序的幼儿园招生报名系统开发笔记
分布式服务治理
依赖传递和依赖调解
【LeetCode】761. Special binary sequence
1052. The Angry Bookstore Boss
软考 --- 软件工程(6)软项目管理
(1)通过FlinkSQL将数据写入mysql demo
Elegantly detect and update web applications in real time
「PHP基础知识」检测数据类型
shell三剑客-----awk命令
PHP —— 用 ThinkPHP5.0 实现微信小程序登陆
面试官:Redis 大 key 要如何处理?
JS-BOM-for, if (string to case)
JS Adder (DOM)
Common regularization methods in deep learning (Regularization) and detailed explanation of WeightDecay parameters in optimizers