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

#LCA0000010. Tổ tiên cao nhất đạt ngưỡng (Greatest Parent)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Tổ tiên cao nhất đạt ngưỡng (Greatest Parent)

Greatest Parent

Source: LightOJ

Version: Phuoc Hung OJ Extended

Problem Statement

You are given a tree rooted at vertex 00 with vertices 0,1,…,n−10,1,\ldots,n-1. Each vertex uu has a value sus_u. The root's value is s0=1s_0=1, and every child has a strictly greater value than its parent.

Each query provides a vertex kk and a threshold v≤skv\le s_k. Among the vertices on the path from root 00 to kk, inclusive, find the vertex closest to the root whose value is at least vv.

The strict increase guarantees a unique answer.

Input

  • The first line contains n,qn,q.
  • The next n−1n-1 lines describe vertex i=1,…,n−1i=1,\ldots,n-1 using its parent pip_i and value sis_i.
  • The final qq lines contain k,vk,v.

Output

For each query print the index of the highest ancestor satisfying the threshold.

Subtasks

  • Subtask 1 (20 points): n≤30n\le30; q≤60q\le60; 2≤n≤1052\le n\le10^5; 1≤q≤5⋅1041\le q\le5\cdot10^4; 0≤pi<i0\le p_i<i; 1=s0<si<2311=s_0<s_i<2^{31}; si>spis_i>s_{p_i}; 1≤v≤sk1\le v\le s_k.
  • Subtask 2 (30 points): n≤1000n\le1000; q≤3000q\le3000; 2≤n≤1052\le n\le10^5; 1≤q≤5⋅1041\le q\le5\cdot10^4; 0≤pi<i0\le p_i<i; 1=s0<si<2311=s_0<s_i<2^{31}; si>spis_i>s_{p_i}; 1≤v≤sk1\le v\le s_k.
  • Subtask 3 (50 points): 2≤n≤1052\le n\le10^5; 1≤q≤5⋅1041\le q\le5\cdot10^4; 0≤pi<i0\le p_i<i; 1=s0<si<2311=s_0<s_i<2^{31}; si>spis_i>s_{p_i}; 1≤v≤sk1\le v\le s_k.

Examples

Input

7 4
0 3
0 4
0 2
1 4
2 7
2 10
5 1
4 2
5 4
6 10

Output

0
1
2
6

Explanation

For query (4,2)(4,2), root 00 has value 11 below the threshold, while vertex 11 has value 33, so the answer is 11. Query (6,10)(6,10) returns vertex 66.