Under construction

Still being built. Things may change

Top Cluster

Time limit4sMemory limit2048 MB

Summary
Given a weighted tree with distinct vertex values, answer queries by finding the smallest value not found within distance k of a given vertex x.
Level

Hard9 of 10

Topics
Divide and conquer, Tree, Binary search
Solved
No attempts yet

Problem

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 wiw_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 xix_i and kik_i (1≤xi≤n1 \leq x_i \leq n, 0≤ki≤10150 \leq k_i \leq 10^{15}), and you need to find the value of mex({wu∣dist(u,xi)≤ki∧1≤u≤n})\mathrm{mex}\left(\{w_u \mid \mathrm{dist}(u, x_i) \leq k_i \land 1 \leq u \leq n\}\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(xi,u)>ki\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?

Input

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), the number of vertices and the number of queries.

The second line contains nn integers w1,w2,…,wnw_1, w_2, \ldots, w_n (0≤wi≤1090 \leq w_i \leq 10^9), the values of the vertices. All the values are pairwise distinct.

Each of the next n−1n-1 lines contains three integers uiu_i, viv_i and ℓi\ell_i (1≤ui,vi≤n1 \leq u_i, v_i \leq n, ui≠viu_i \neq v_i, 1≤ℓi≤1091 \leq \ell_i \leq 10^9), describing a two-way edge between vertices uiu_i and viv_i with length ℓi\ell_i. The input forms a tree.

Each of the next qq lines contains two integers xix_i and kik_i (1≤xi≤n1 \leq x_i \leq n, 0≤ki≤10150 \leq k_i \leq 10^{15}), describing the ii-th query.

Output

For each query, print a single line containing the mex value you found.

Examples1

  1. Example 1

    Input
    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
    
    Expected output
    1
    0
    3
    4