PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN
#SRD0000101. Cây động của Yuezheng Ling (Yuezheng Ling and Dynamic Tree)
HỌC THEO LỘ TRÌNH
1 lộ trìnhBài tập thuộc lộ trình
Mục lục đề ⌄
Đang đọc
Cây động của Yuezheng Ling (Yuezheng Ling and Dynamic Tree)
Yuezheng Ling and Dynamic Tree
Source: Codeforces
Version: Phuoc Hung OJ Extended
Problem Statement
A tree has vertices rooted at . For each vertex with , its parent is and . The tree changes over time. Process the following operations in order:
1 l r x: For every index with , change the parent of vertex to . This changes parent links, not vertex weights.2 u v: Print the lowest common ancestor (LCA) of and in the current tree, after all preceding updates.
The parent index of every non-root vertex always remains smaller than its index; therefore the structure remains a rooted tree after each update.
Input
- The first line contains .
- The second line contains .
- Each of the next lines has the format
1 l r xor2 u v. At least one type-2 query is guaranteed.
Output
For each type-2 operation, print the vertex index of the LCA on its own line.
Subtasks
- Subtask 1 (10 points): , ; ; updates have , ; queries have ; at least one type-2 query.
- Subtask 2 (20 points): ; ; only type-2 queries with .
- Subtask 3 (30 points): ; ; updates have , ; queries have ; at least one type-2 query.
- Subtask 4 (40 points): ; ; updates have , ; queries have ; at least one type-2 query.
Examples
Input
6 4
1 2 3 3 4
2 3 4
1 2 3 1
2 5 6
2 2 3
Output
3
3
1
Explanation
Initially the parents are . Vertex 3 is an ancestor of vertex 4, hence . After 1 2 3 1, the parents of 2 and 3 are both 1. The paths from vertices 5 and 6 to the root are and , so their LCA is 3. Finally, .