CF893F.Subtree Minimum Query

提高+/省选-

通过率:0%

时间限制:6.00s

内存限制:512MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given a rooted tree consisting of n vertices. Each vertex has a number written on it; number a__i is written on vertex i.

Let's denote d(i, j) as the distance between vertices i and j in the tree (that is, the number of edges in the shortest path from i to j). Also let's denote the k-blocked subtree of vertex x as the set of vertices y such that both these conditions are met:

  • x is an ancestor of y (every vertex is an ancestor of itself);
  • d(x, y) ≤ k.

You are given m queries to the tree. i-th query is represented by two numbers x__i and k__i, and the answer to this query is the minimum value of a__j among such vertices j such that j belongs to k__i-blocked subtree of x__i.

Write a program that would process these queries quickly!

Note that the queries are given in a modified way.

给你一棵包含 nn 个顶点的有根树。每个顶点上写有一个数字;顶点 ii 上写的数字为 aia_i。

记 d(i, j)d(i,\,j) 为树中顶点 ii 与 jj 之间的距离(即从 ii 到 jj 的最短路径所含的边数)。再定义顶点 xx 的 kk-阻塞子树(kk-blocked subtree)为满足以下两个条件的所有顶点 yy 构成的集合:

  • xx 是 yy 的祖先(每个顶点都是自身的祖先);
  • d(x, y)≤kd(x,\,y) \leq k。

你将收到关于该树的 mm 个查询。第 ii 个查询由两个数 xix_i 和 kik_i 表示,其答案为:在 xix_i 的 kik_i-阻塞子树中所有顶点 jj 对应的 aja_j 值中的最小值。

请编写一个能快速处理这些查询的程序!

注意:这些查询是以一种修改后的方式给出的。

输入格式

The first line contains two integers n and r (1 ≤ r ≤ n ≤ 100000) — the number of vertices in the tree and the index of the root, respectively.

The second line contains n integers _a_1, _a_2, ..., a__n (1 ≤ a__i ≤ 109) — the numbers written on the vertices.

Then n - 1 lines follow, each containing two integers x and y (1 ≤ x, y ≤ n) and representing an edge between vertices x and y. It is guaranteed that these edges form a tree.

Next line contains one integer m (1 ≤ m ≤ 106) — the number of queries to process.

Then m lines follow, i-th line containing two numbers p__i and q__i, which can be used to restore i-th query (1 ≤ p__i, q__i ≤ n).

i-th query can be restored as follows:

Let last be the answer for previous query (or 0 if i = 1). Then x__i = ((p__i + last) mod n) + 1, and k__i = (q__i + last) mod n.

第一行包含两个整数 nn 和 rr(1≤r≤n≤1000001 \le r \le n \le 100000)—— 分别表示树中顶点的数量和根节点的编号。

第二行包含 nn 个整数 a1, a2, …, ana_1,\,a_2,\,\dots,\,a_n(1≤ai≤1091 \le a_i \le 10^9)—— 表示写在各顶点上的数字。

接下来 n−1n-1 行,每行包含两个整数 xx 和 yy(1≤x, y≤n1 \le x,\,y \le n),表示顶点 xx 与 yy 之间存在一条边。保证这些边构成一棵树。

下一行包含一个整数 mm(1≤m≤1061 \le m \le 10^6)—— 表示需要处理的查询数量。

随后 mm 行,第 ii 行包含两个数 pip_i 和 qiq_i(1≤pi, qi≤n1 \le p_i,\,q_i \le n),用于还原第 ii 个查询。

第 ii 个查询可按如下方式还原:

设 lastlast 为上一个查询的答案(若 i=1i = 1,则 last=0last = 0)。则 xi=((pi+last) mod n)+1x_i = ((p_i + last)\bmod n) + 1,且 ki=(qi+last) mod nk_i = (q_i + last)\bmod n。

输出格式

Print m integers. i-th of them has to be equal to the answer to i-th query.

输出 mm 个整数。其中第 ii 个整数必须等于第 ii 个查询的答案。

输入输出样例

  • 输入#1

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

    输出#1

    2
    5

输入解题思路,AI测评打分。不知道怎么写?

首页