CF1110F Nearest Leaf

Description

Let's define the Eulerian traversal of a tree (a connected undirected graph without cycles) as follows: consider a depth-first search algorithm which traverses vertices of the tree and enumerates them in the order of visiting (only the first visit of each vertex counts). This function starts from the vertex number $ 1 $ and then recursively runs from all vertices which are connected with an edge with the current vertex and are not yet visited in increasing numbers order. Formally, you can describe this function using the following pseudocode: ```

next_id = 1

id = array of length n filled with -1

visited = array of length n filled with false



function dfs(v):

visited[v] = true

id[v] = next_id

next_id += 1

for to in neighbors of v in increasing order:

if not visited[to]:

dfs(to)

``` You are given a weighted tree, the vertices of which were enumerated with integers from $ 1 $ to $ n $ using the algorithm described above. A leaf is a vertex of the tree which is connected with only one other vertex. In the tree given to you, the vertex $ 1 $ is not a leaf. The distance between two vertices in the tree is the sum of weights of the edges on the simple path between them. You have to answer $ q $ queries of the following type: given integers $ v $ , $ l $ and $ r $ , find the shortest distance from vertex $ v $ to one of the leaves with indices from $ l $ to $ r $ inclusive.

Input Format

N/A

Output Format

N/A

Explanation/Hint

In the first example, the tree looks like this: ![](https://cdn.luogu.com.cn/upload/vjudge_pic/CF1110F/0f1b498aea8daedc270520f6cae94d5c4aa241fe.png)In the first query, the nearest leaf for the vertex $ 1 $ is vertex $ 4 $ with distance $ 3 $ . In the second query, the nearest leaf for vertex $ 5 $ is vertex $ 5 $ with distance $ 0 $ . In the third query the nearest leaf for vertex $ 4 $ is vertex $ 4 $ ; however, it is not inside interval $ [1, 2] $ of the query. The only leaf in interval $ [1, 2] $ is vertex $ 2 $ with distance $ 13 $ from vertex $ 4 $ .