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

#LCA0000042. Loại một nhân viên để tăng độ sâu tổ tiên (Company)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Loại một nhân viên để tăng độ sâu tổ tiên (Company)

Company

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

A company has nn employees indexed 11 through nn, with employee 11 as director. Their reporting structure is a tree rooted at 11. The root depth is 00; every other depth is its parent's depth plus 11.

Each plan involves exactly the employees with indices in [l,r][l,r], where l<rl<r. You must remove exactly one employee xx from the plan. The most deeply nested manager who can manage all remaining employees is their lowest common ancestor. Choose xx to maximize the depth of this manager and print the removed employee index and the resulting optimal depth.

If several removals give the same optimum, any one is accepted. A special checker validates all optimal alternatives.

Input

  • First line: n,qn,q.
  • Next line: parents p2,…,pnp_2,\ldots,p_n.
  • Last qq lines: ranges l,rl,r with l<rl<r.

Output

For each query print x,dx,d, the removed employee index and the maximum achievable manager depth. Any optimal xx is accepted.

Subtasks

General constraints (all subtasks): 2≤n≤1052\le n\le 10^5; 1≤q≤1051\le q\le 10^5; 1≤pi≤n1\le p_i\le n; 1≤l<r≤n1\le l<r\le n.

  • Subtask 1 (20 points): 2≤n≤302\le n\le 30; 1≤q≤501\le q\le 50; l<rl<r; a parent index can exceed its child index.
  • Subtask 2 (30 points): 2≤n≤10002\le n\le 1000; 1≤q≤10001\le q\le 1000; l<rl<r; a parent index can exceed its child index.
  • Subtask 3 (50 points): 2≤n≤1000002\le n\le 100000; 1≤q≤1000001\le q\le 100000; l<rl<r; a parent index can exceed its child index.

Examples

Input

5 3
1 1 3 3
4 5
2 5
1 3

Output

4 2
2 1
1 0

Explanation

For [4,5][4,5], removing 44 leaves vertex 55 at depth 22; removing 55 also gives depth 22. The sample prints one valid optimal choice. The other queries are handled by the LCA of the remaining employee set.