# bzoj 2588 : Spoj 10628. Count on a tree

## Output

M行，表示每个询问的答案。最后一个询问不输出换行符

## Sample Input

8 5
105 2 9 3 8 5 7 7
1 2
1 3
1 4
3 5
3 6
3 7
4 8
2 5 1
0 5 2
10 5 3
11 5 4
110 8 2

2
8
9
105
7

## HINT

HINT：

N,M<=100000

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define mid int m = (l + r) >> 1
const int M = 2e5 + 10;
int cnt1,n,idx,cnt,f[M];

struct node {
int to,next;
}e[M];

}

int lca(int a,int b){
if(dep[a] > dep[b]) swap(a,b);
int h = dep[b] - dep[a];
for(int i = 0;(1<<i)<=h;i++){
if((1<<i)&h) b = p[b][i];
}

if(a!=b){
for(int i = 22;i >= 0;i --){
if(p[a][i]!=p[b][i]){
a = p[a][i]; b = p[b][i];
}
}
a = p[a][0];
}
return a;
}

void build(int l,int r,int &rt){
rt = ++idx;
sum[rt] = 0;
if(l == r) return;
mid;
build(l,m,ls[rt]);
build(m+1,r,rs[rt]);
}

void update(int p,int l,int r,int old,int &rt){
rt = ++idx;
ls[rt] = ls[old]; rs[rt] = rs[old]; sum[rt] = sum[old] + 1;
if(l == r) return ;
mid;
if(p <= m) update(p,l,m,ls[old],ls[rt]);
else update(p,m+1,r,rs[old],rs[rt]);
}

int query(int a,int b,int lc,int cl,int l,int r,int k){
if(l == r) return l;
mid;
int cnt = sum[ls[a]] + sum[ls[b]] - sum[ls[lc]] - sum[ls[cl]];
if(k <= cnt)
return query(ls[a],ls[b],ls[lc],ls[cl],l,m,k);
else
return query(rs[a],rs[b],rs[lc],rs[cl],m+1,r,k-cnt);
}
int a[M],b[M];

void dfs(int u,int fa){
f[u] = fa;
update(a[u],1,cnt,root[fa],root[u]);
for(int i = head[u];i;i = e[i].next){
int v = e[i].to;
if(v == fa) continue;
p[v][0] = u;
dep[v] = dep[u] + 1;
dfs(v,u);
}
}

void init()
{
cnt1 = 0; idx = 0;
memset(dep,0,sizeof(dep));
memset(p,0,sizeof(p));
memset(f,0,sizeof(f));
dep[1] = 1;
}

int main()
{
int m;
while(scanf("%d%d",&n,&m)!=EOF){
init();
for(int i = 1; i <= n;i ++){
scanf("%d",&a[i]);
b[i] = a[i];
}
int l,r,c;
sort(b+1,b+n+1);
cnt = unique(b+1,b+1+n)-b-1;
for(int i = 1;i <= n;i ++)
a[i] = lower_bound(b+1,b+cnt+1,a[i]) - b;
for(int i = 1;i <= n-1;i ++){
scanf("%d%d",&l,&r);
}
build(1,cnt,root[0]);
dfs(1,0);
for(int j = 1;(1<<j)<=n;j++)
for(int i = 1;i <= n;i++)
p[i][j] = p[p[i][j-1]][j-1];
int num = 0;
for(int i = 1;i <= m;i ++){
scanf("%d%d%d",&l,&r,&c);
l = l^num;
int lc = lca(l,r);
int id = query(root[l],root[r],root[lc],root[f[lc]],1,cnt,c);
num = b[id];
printf("%d\n",b[id]);
}
}
return 0;
}

posted @ 2018-11-08 18:08  冥想选手  阅读(126)  评论(0编辑  收藏  举报