Bzoj 2588 Spoj 10628. Count on a tree(树链剖分LCA+主席树)

2588: Spoj 10628. Count on a tree
Time Limit: 12 Sec Memory Limit: 128 MB
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

Source

/*

ans=tree[x].size+tree[y].size-tree[lca].size-tree[lca_father].size.
*/
#include<iostream>
#include<cstdio>
#include<algorithm>
#define MAXN 100001
using namespace std;
int fa[MAXN],pos[MAXN],top[MAXN],deep[MAXN],size[MAXN];
struct data{int lc,rc,size;}tree[MAXN*20];
struct node{int v,next;}e[MAXN*2];
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9') x=x*10+ch-48,ch=getchar();
return x*f;
}
{
}
void add(int &k,int last,int l,int r,int x)
{
k=++tot;tree[k].lc=tree[last].lc,tree[k].rc=tree[last].rc;
tree[k].size=tree[last].size+1;
if(l==r) return ;
int mid=(l+r)>>1;
return ;
}
void dfs1(int u,int f)
{
size[u]=1;
int p=lower_bound(s+1,s+n+1,a[u])-s;
{
int v=e[i].v;
if(v!=f) fa[v]=u,deep[v]=deep[u]+1,dfs1(v,u),size[u]+=size[v];
}
return ;
}
void dfs2(int u,int top1)
{
int k=0;pos[u]=++maxsize;top[u]=top1;
{
int v=e[i].v;
if(fa[v]==u&&size[v]>size[k]) k=v;
}
if(!k) return ;
dfs2(k,top1);
{
int v=e[i].v;
if(fa[v]==u&&v!=k) dfs2(v,v);
}
return ;
}
int lca(int x,int y)
{
fa[1]=1;
while(top[x]!=top[y])
{
if(deep[top[x]]<deep[top[y]]) swap(x,y);//1 R.
x=fa[top[x]];
}
fa[1]=0;
if(pos[x]>pos[y]) swap(x,y);
return x;
}
int query(int L,int R,int lc,int falc,int l,int r,int k)
{
if(l==r) return l;
int mid=(l+r)>>1;
int sum=tree[tree[L].lc].size+tree[tree[R].lc].size-
tree[tree[lc].lc].size-tree[tree[falc].lc].size;
if(sum>=k)
return query(tree[L].lc,tree[R].lc,tree[lc].lc,tree[falc].lc,l,mid,k);
else return query(tree[L].rc,tree[R].rc,tree[lc].rc,tree[falc].rc,mid+1,r,k-sum);
}
void slovequery(int x,int y,int k)
{
int lc=lca(x,y);
ans=query(root[x],root[y],root[lc],root[fa[lc]],1,n,k);
printf("%d",ans=s[ans]);return ;
}
int main()
{
int x,y,z;
}