#abc239e. E - Subtree K-th Max

E - Subtree K-th Max

Score : 500500 points

问题描述

我们有一棵包含 NN 个顶点的有根树。顶点编号为 11NN,其中顶点 11 是树的根节点。

ii 条边连接顶点 AiA_iBiB_i

顶点 ii 上写有一个整数 XiX_i

你将获得 QQ 个查询请求。对于第 ii 个查询,给定一对整数 (Vi,Ki)(V_i, K_i),回答以下问题:

  • 问题:在以顶点 ViV_i 为根的子树中,找出所写整数中的第 KiK_i 大值。

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

We have a rooted tree with NN vertices. The vertices are numbered 11 through NN, and the root is Vertex 11.
The ii-th edge connects Vertices AiA_i and BiB_i.
Vertex ii has an integer XiX_i written on it.

You are given QQ queries. For the ii-th query, given a pair of integers (Vi,Ki)(V_i,K_i), answer the following question.

  • Question: among the integers written on the vertices in the subtree rooted at Vertex ViV_i, find the KiK_i-th largest value.

Constraints

  • 2N1052 \leq N \leq 10^5
  • 0Xi1090\leq X_i\leq 10^9
  • 1Ai,BiN1\leq A_i,B_i\leq N
  • 1Q1051\leq Q \leq 10^5
  • 1ViN1\leq V_i\leq N
  • 1Ki201\leq K_i\leq 20
  • The given graph is a tree.
  • The subtree rooted at Vertex ViV_i has KiK_i or more vertices.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

NN QQ

X1X_1 \ldots XNX_N

A1A_1 B1B_1

\vdots

AN1A_{N-1} BN1B_{N-1}

V1V_1 K1K_1

\vdots

VQV_Q KQK_Q

Output

Print QQ lines. The ii-th line should contain the response to the ii-th query.

Sample Input 1

5 2
1 2 3 4 5
1 4
2 1
2 5
3 2
1 2
2 1

Sample Output 1

4
5

The tree given in this input is shown below.

figure

For the 11-st query, the vertices in the subtree rooted at Vertex 11 are Vertices 1,2,3,41, 2, 3, 4, and 55, so print the 22-nd largest value of the numbers written on these vertices, 44.
For the 22-nd query, the vertices in the subtree rooted at Vertex 22 are Vertices 2,32, 3, and 55, so print the 11-st largest value of the numbers written on these vertices, 55.

Sample Input 2

6 2
10 10 10 9 8 8
1 4
2 1
2 5
3 2
6 4
1 4
2 2

Sample Output 2

9
10

Sample Input 3

4 4
1 10 100 1000
1 2
2 3
3 4
1 4
2 3
3 2
4 1

Sample Output 3

1
10
100
1000

update @ 2024/3/10 10:19:15