[cf contest 893(edu round 33)] F - Subtree Minimum Query

时间:2023-11-22 21:20:50

[cf contest 893(edu round 33)] F - Subtree Minimum Query

time limit per test

6 seconds

memory limit per test

512 megabytes

input

standard input

output

standard output

You are given a rooted tree consisting of n vertices. Each vertex has a number written on it; number ai 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 xi and ki, and the answer to this query is the minimum value of aj among such vertices j such that j belongs to ki-blocked subtree of xi.

Write a program that would process these queries quickly!

Note that the queries are given in a modified way.

Input

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 a1, a2, ..., an (1 ≤ ai ≤ 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 pi and qi, which can be used to restore i-th query (1 ≤ pi, qi ≤ n).

i-th query can be restored as follows:

Let last be the answer for previous query (or 0 if i = 1). Then xi = ((pi + last) modn) + 1, and ki = (qi + last) modn.

Output

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

Example
input
5 21 3 2 3 52 35 13 44 121 22 3
output
25

我们对于树上的每一个节点开一个线段树(其实是可持久化的)。

每棵线段树都是一个[1..maxdepth]的按照绝对深度来建的线段树。

那么,对于每一个节点x,可以把所以它的子节点的线段树合并到它上面去。

但是这需要可持久化。因为当前节点的信息也要用到。如果不可持久化,可能会导致当前节点的地址在其他线段树里面存在。

实际上,每个地址只存在与一棵线段树中。

然后,由于父子节点之间的线段树只有1条链不同,所以要新建logn个节点。

总共有n-1对父子关系,所以空间为O(nlogn)。

时间复杂度为O(mlogn)。注意强制在线。

code:

 #include <cstdio>
 #include <cstring>
 #include <algorithm>
 typedef long long LL;
 using namespace std;

 void OJ() {
     #ifndef ONLINE_JUDGE
         freopen("in.txt","r",stdin);
         freopen("out.txt","w",stdout);
     #endif
 }

 namespace fastIO {
     #define gec(c) getchar(c)
     #define puc(c) putchar(c)
     char ch;
     inline int read() {
         ,f=; ch=getchar();
         ') {
             if (ch=='-') f=-f;
             ch=gec();
         }
         ') {
             x=(x<<)+(x<<)+(ch^);
             ch=gec();
         }
         return x*f;
     }
     ];
     template <class T> inline void write(T x) {
         ) {
             puc('); return;
         }
         ) x=-x,puc('-');
         ; x; x/=) nnn[++ttt]=x%;
         );
     }
     inline void newline () {
         puc('\n');
     }
 } using namespace fastIO;

 ,inf=2e9;
 int n,q,tot,lim;
 ],son[N<<],dep[N];
 struct node {
     node* l,* r;
     int v;
     node () {
         l=r=;
         v=inf;
     }
 } *ro[N];
 #define M ((l)+(r)>>1)
 inline void refresh (node* c) {
     c->v=inf;
     if (c->l) c->v=min(c->v,c->l->v);
     if (c->r) c->v=min(c->v,c->r->v);
 }
 inline void setup (node* &c,int l,int r,int x,int v) {
     c=new node();
     if (l==r) {
         c->v=v;
         return;
     }
     if (x<=M) setup(c->l,l,M,x,v);
     ,r,x,v);
     refresh(c);
 }
 inline node* merge (node* x,node* y) {
     if (!x||!y) return x?x:y;
     node* ret=new node();
     ret->l=merge(x->l,y->l);
     ret->r=merge(x->r,y->r);
     ret->v=min(x->v,y->v);
     return ret;
 }
 inline int reply (node* u,int l,int r,int x,int y) {
     if (!u) return inf;
     if (x<=l&&r<=y) return u->v;
     if (y<=M) return reply(u->l,l,M,x,y); else
     ,r,x,y); else
     ,r,x,y));
 }
 void add (int x,int y) {
     nxt[++tot]=lnk[x],lnk[x]=tot,son[tot]=y;
 }
 void dfs (int x,int p) {
     setup(ro[x],,n,dep[x]=dep[p]+,a[x]);
     lim=max(lim,dep[x]);
     for (int j=lnk[x]; j; j=nxt[j]) {
         if (son[j]==p) continue;
         dfs(son[j],x);
         ro[x]=merge(ro[x],ro[son[j]]);
     }
 }
 int main() {
     OJ(); ;
     n=read(),rot=read();
     ; i<=n; ++i) {
         a[i]=read(),ro[i]=;
     }
     ; i<n; ++i) {
         x=read(),y=read();
         add(x,y),add(y,x);
     }
     dep[]=lim=,dfs(rot,);
     q=read();
     for ( ; q; --q) {
         x=read(),y=read();
         x=(x+ans)%n+,y=(y+ans)%n;
         ans=reply(ro[x],,n,dep[x],min(lim,dep[x]+y));
         write(ans),newline();
     }
     ;
 }