Top Cluster

시간 제한4초메모리 제한2048 MB

요약
가중치 트리에서 정점 값이 모두 다를 때, 각 질의는 정점 x에서 거리 k 이내 값들의 mex를 구하는 문제로, 각 값의 가장 가까운 외부 발생 위치를 찾는 문제로 바뀐다.
난이도

보통10점 중 7점

유형
트리, DFS, 이분 탐색, 정렬
정답자
아직 제출이 없습니다

문제

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 nn vertices, labeled by 1,2,…,n1, 2, \ldots, n. The value of the ii-th vertex is a non-negative integer w_iw\_i. All the values are pairwise distinct.

You will then be given qq queries. In the ii-th query, you will be given two integers x_ix\_i and k_ik\_i (1≤x_i≤n1 \leq x\_i \leq n, 0≤k_i≤10150 \leq k\_i \leq 10^{15}), 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, dist(u,v)\mathrm{dist}(u, v) denotes the length of the shortest path from vertex uu to vertex vv. 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 dist(x_i,u)>k_i\mathrm{dist}(x\_i, u) > k\_i, 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 nn and qq (1≤n,q≤5⋅1051 \leq n, q \leq 5 \cdot 10^5) denoting the number of vertices and the number of queries.

The second line contains nn integers w_1,w_2,…,w_nw\_1, w\_2, \ldots, w\_n (0≤w_i≤1090 \leq w\_i \leq 10^9) denoting the values of the vertices. It is guaranteed that all the values are pairwise distinct.

Each of the next n−1n - 1 lines contains three integers u_iu\_i, v_iv\_i and ℓ_i\ell\_i (1≤u_i,v_i≤n1 \leq u\_i, v\_i \leq n, u_i≠v_iu\_i \neq v\_i, 1≤ℓ_i≤1091 \leq \ell\_i \leq 10^9) denoting a two-way edge between vertices u_iu\_i and v_iv\_i with length ℓ_i\ell\_i. It is guaranteed that the input forms a tree.

Each of the next qq lines contains two integers x_ix\_i and k_ik\_i (1≤x_i≤n1 \leq x\_i \leq n, 0≤k_i≤10150 \leq k\_i \leq 10^{15}) denoting the ii-th query.

출력

For each query, print a single line containing an integer: the mex\mathrm{mex} value that you found.

예제1

  1. 예제 1

    입력
    5 4
    3 9 0 1 2
    1 2 10
    3 1 4
    3 4 3
    3 5 2
    3 0
    1 0
    4 6
    4 7
    
    예상 출력
    1
    0
    3
    4