CF893F.Subtree Minimum Query

普及/提高-

通过率:0%

AC君温馨提醒

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

题目描述

You are given a rooted tree consisting of nn vertices. Each vertex has a number written on it; number aia_{i} is written on vertex ii .

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

  • xx is an ancestor of yy (every vertex is an ancestor of itself);
  • d(x,y)<=kd(x,y)<=k .

You are given mm queries to the tree. ii -th query is represented by two numbers xix_{i} and kik_{i} , and the answer to this query is the minimum value of aja_{j} among such vertices jj such that jj belongs to kik_{i} -blocked subtree of xix_{i} .

Write a program that would process these queries quickly!

Note that the queries are given in a modified way.

输入格式

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

The second line contains nn integers a1,a2,...,ana_{1},a_{2},...,a_{n} ( 1<=ai<=1091<=a_{i}<=10^{9} ) — the numbers written on the vertices.

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

Next line contains one integer mm ( 1<=m<=1061<=m<=10^{6} ) — the number of queries to process.

Then mm lines follow, ii -th line containing two numbers pip_{i} and qiq_{i} , which can be used to restore ii -th query ( 1<=pi,qi<=n1<=p_{i},q_{i}<=n ).

ii -th query can be restored as follows:

Let lastlast be the answer for previous query (or 00 if i=1i=1 ). Then xi=((pi+last)modn)+1x_{i}=((p_{i}+last)modn)+1 , and ki=(qi+last)modnk_{i}=(q_{i}+last)modn .

输出格式

Print mm integers. ii -th of them has to be equal to the answer to ii -th query.

输入输出样例

  • 输入#1

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

    输出#1

    2
    5
    
首页