# [BZOJ2588/SPOJ10628]Count on a tree

Description

Input

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

Sample Output
2
8
9
105
7

HINT
N,M<=100000

(其实SPOJ原题是不需要异或Lastans的……)

/*program from Wolfycz*/
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define inf 0x7f7f7f7f
using namespace std;
typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
int x=0,f=1;char ch=getchar();
for (;ch<'0'||ch>'9';ch=getchar())	if (ch=='-')    f=-1;
for (;ch>='0'&&ch<='9';ch=getchar())	x=(x<<1)+(x<<3)+ch-'0';
return x*f;
}
inline void print(int x){
if (x>=10)	print(x/10);
putchar(x%10+'0');
}
const int N=1e5,M=1e7;
int root[N+10],list[N+10],val[N+10];
struct Segment{
int ls[M+10],rs[M+10],cnt[M+10],tot;
void insert(int &k,int p,int l,int r,int v){
cnt[k=++tot]=cnt[p]+1;
ls[k]=ls[p],rs[k]=rs[p];
if (l==r)	return;
int mid=(l+r)>>1;
if (v<=mid)	insert(ls[k],ls[p],l,mid,v);
else	insert(rs[k],rs[p],mid+1,r,v);
}
int Query(int u,int v,int lca,int lca_fa,int l,int r,int x){
if (l==r)	return list[l];
int mid=(l+r)>>1,tmp=cnt[ls[u]]+cnt[ls[v]]-cnt[ls[lca]]-cnt[ls[lca_fa]];
if (x<=tmp)	return Query(ls[u],ls[v],ls[lca],ls[lca_fa],l,mid,x);
else	return Query(rs[u],rs[v],rs[lca],rs[lca_fa],mid+1,r,x-tmp);
}
}Chairman;
struct S1{
int pre[(N<<1)+10],now[N+10],child[(N<<1)+10],tot;
int fa[N+10],size[N+10],top[N+10],deep[N+10],Rem[N+10];
void connect(int x,int y){pre[++tot]=now[x],now[x]=tot,child[tot]=y;}
void insert(int x,int y){connect(x,y),connect(y,x);}
void build(int x){
deep[x]=deep[fa[x]]+1,size[x]=1;
for (int p=now[x],son=child[p];p;p=pre[p],son=child[p]){
if (son==fa[x])	continue;
fa[son]=x,build(son),size[x]+=size[son];
if (size[son]>size[Rem[x]])	Rem[x]=son;
}
}
void dfs(int x){
if (!x)	return;
top[x]=Rem[fa[x]]==x?top[fa[x]]:x;
dfs(Rem[x]);
for (int p=now[x],son=child[p];p;p=pre[p],son=child[p]){
if (son==fa[x]||son==Rem[x])	continue;
dfs(son);
}
}
void Segment_build(int x,int T){
Chairman.insert(root[x],root[fa[x]],1,T,val[x]);
for (int p=now[x],son=child[p];p;p=pre[p],son=child[p]){
if (son==fa[x])	continue;
Segment_build(son,T);
}
}
int LCA(int x,int y){
while (top[x]!=top[y]){
if (deep[top[x]]<deep[top[y]])	swap(x,y);
x=fa[top[x]];
}
return deep[x]<deep[y]?x:y;
}
}Tree;
int main(){
sort(list+1,list+1+n);
int T=unique(list+1,list+1+n)-list-1;
for (int i=1;i<=n;i++)	val[i]=lower_bound(list+1,list+1+T,val[i])-list;
for (int i=1;i<n;i++){