PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#LCA0000074. Sắp xếp bộ ba truy vấn trên cây (Tree Query Order)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

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 nn vertices and a temporary root rr, let fr(x,y)f_r(x,y) denote the LCA of x,yx,y under root rr. Form a list of exactly n2n^2 triples (x,fr(x,y),y)(x,f_r(x,y),y), one for each ordered pair (x,y)(x,y). 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 (x,y)(x,y) that produces the triple at rank kk. Each query selects its own root; edges stay unchanged.

Input

First line contains n qn\ q, followed by n−1n-1 edges u vu\ v. Each of the qq remaining lines is 1 r x y or 2 r k.

Output

For type 1 print a rank; for type 2 print the pair x,yx,y.

Subtasks

General constraints: 1≤n,q≤1051\le n,q\le10^5; 1≤r,x,y≤n1\le r,x,y\le n; 1≤k≤n21\le k\le n^2; dùng số nguyên 64 bit để lưu vị trí.

  • Subtask 1 (20 points): 1≤n≤401\le n\le 40; 1≤q≤501\le q\le 50.

  • Subtask 2 (30 points): 1≤n≤10001\le n\le 1000; 1≤q≤10001\le q\le 1000.

  • Subtask 3 (50 points): 1≤n≤1000001\le n\le 100000; 1≤q≤1000001\le q\le 100000.

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 22, the triple of ordered pair (3,4)(3,4) has rank 1313; conversely, rank 1313 corresponds to pair (3,4)(3,4). The task orders triples, rather than merely querying LCAs.