Top Cluster
시간 제한4초메모리 제한2048 MB
가중치 트리에서 정점 값이 모두 다를 때, 각 질의는 정점 x에서 거리 k 이내 값들의 mex를 구하는 문제로, 각 값의 가장 가까운 외부 발생 위치를 찾는 문제로 바뀐다.
문제
Top Cluster is a useful data structure for maintaining data on a tree. Using Top Cluster, we can do range queries efficiently.
Lovely EMmm likes data structure technologies very much. She is learning Top Cluster now, and she is trying to solve a data structure problem. Can you write a program to solve the problem together with EMmm?
In the problem, you will be given a tree with vertices, labeled by . The value of the -th vertex is a non-negative integer . All the values are pairwise distinct.
You will then be given queries. In the -th query, you will be given two integers and (, ), and you need to find the value of \mathrm{mex}\left(\left\\{w\_u \mid \mathrm{dist}(u, x\_i) \leq k\_i \land 1 \leq u \leq n\right\\}\right).
Here, denotes the length of the shortest path from vertex to vertex . In mathematics, the mex ("minimum excluded value") of a set is the smallest non-negative integer that does not belong to the set.
EMmm is good at solving mex problems. She found that when all the values are pairwise distinct, the problem above is equivalent to finding the smallest non-negative integer that either occurred outside the given range, which means , or never occurred in the whole tree. However, she can't go any further. Can you help her solve the problem?
입력
The first line of the input contains two integers and () denoting the number of vertices and the number of queries.
The second line contains integers () denoting the values of the vertices. It is guaranteed that all the values are pairwise distinct.
Each of the next lines contains three integers , and (, , ) denoting a two-way edge between vertices and with length . It is guaranteed that the input forms a tree.
Each of the next lines contains two integers and (, ) denoting the -th query.
출력
For each query, print a single line containing an integer: the value that you found.