#LCA0000074. Sắp xếp bộ ba truy vấn trên cây (Tree Query Order)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Sắp xếp bộ ba truy vấn trên cây (Tree Query Order)
Tree Query Order
Source: LQDOJ
Version: Phuoc Hung OJ Extended
Problem Statement
For a tree with vertices and a temporary root , let denote the LCA of under root . Form a list of exactly triples , one for each ordered pair . Sort the list lexicographically in ascending order, retaining multiplicity. Two query types are supported: 1 r x y asks for the 1-based rank of that triple; 2 r k asks for the pair that produces the triple at rank . Each query selects its own root; edges stay unchanged.
Input
First line contains , followed by edges . Each of the remaining lines is 1 r x y or 2 r k.
Output
For type 1 print a rank; for type 2 print the pair .
Subtasks
General constraints: ; ; ; dùng số nguyên 64 bit để lưu vị trí.
-
Subtask 1 (20 points): ; .
-
Subtask 2 (30 points): ; .
-
Subtask 3 (50 points): ; .
Examples
Input
5 2
1 2
1 3
2 4
2 5
1 2 3 4
2 2 13
Output
13
3 4
Explanation
Under root , the triple of ordered pair has rank ; conversely, rank corresponds to pair . The task orders triples, rather than merely querying LCAs.