Count on a tree SPOJ - COT (LCA+主席树)

题意:

在树上找到u->v的第K大

思路:

root[a]+root[b]-root[lca(a,b)]-root[fa[lca(a,b)]]上的第k大,具体说下代码,先找LCA(这里是用树链剖分的思想求的LCA),之后每一个树的

节点,都维护一棵子树, 这两棵树不属于同一类,因此用不同的标号来记录

 
#include
using namespace std;
const int N=100005;
int n,m,w[N],b[N];
struct edge
{
    int to,next;
} vec[N<<1];
int head[N],tot;

///主席树
int root[N] ,chairtree,rnu;
struct node
{
    int lc,rc,sum;
} ctree[N*20];
void build(int &i,int l,int r)
{
    ctree[++chairtree]=ctree[i];
    i=chairtree;
    ctree[i].sum=0;
    if(l>=r)return ;
    int m =((r-l)>>1)+l;
    build(ctree[i].lc,l,m);
    build(ctree[i].rc,m+1,r);
}

void update(int& i,int l,int r,int pos)
{
    ctree[++chairtree]=ctree[i];
    i=chairtree;
    ctree[i].sum++;
    if(l>=r) return ;
    int m=((r-l)>>1)+l;
    if(pos<=m)
        update(ctree[i].lc,l,m,pos);
    else
        update(ctree[i].rc,m+1,r,pos);
}

int query(int i,int l,int r,int j,int lca,int flca,int k)
{
    if(l>=r) return l;
    int m=((r-l)>>1)+l;
    int cnt=ctree[ctree[i].lc].sum+ctree[ctree[j].lc].sum-ctree[ctree[lca].lc].sum-ctree[ctree[flca].lc].sum;
    if(k<=cnt)
        query(ctree[i].lc,l,m,ctree[j].lc,ctree[lca].lc,ctree[flca].lc,k);
    else
        query(ctree[i].rc,m+1,r,ctree[j].rc,ctree[lca].rc,ctree[flca].rc,k-cnt);
}

///树链剖分
void add(int u,int v)
{
    vec[++tot].to=v,vec[tot].next=head[u],head[u]=tot;
    vec[++tot].to=u,vec[tot].next=head[v],head[v]=tot;
}


int dep[N],fa[N],siz[N],son[N];
void dfs1(int u,int f,int d)
{
    dep[u]=d;
    fa[u]=f;
    siz[u]=1;
    son[u]=0;
    for(int i=head[u];i;i=vec[i].next)
    {
        int v=vec[i].to;
        if(v==f) continue;
        dfs1(v,u,d+1);
        siz[u]+=siz[v];
        if(siz[son[u]]dep[y])swap(x,y);
    return x;
}

int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1; i<=n; i++)
    {
        scanf("%d",&w[i]);
        b[i]=w[i];
    }
    for(int i=1; i< n; i++)
    {
        int u,v;
        scanf("%d%d",&u,&v );
        add(u,v);
    }
    fa[1]=0;
    sort(b+1,b+n+1);
    rnu = unique(b+1,b+n+1)-(b+1);
    for(int i=1; i<=n; i++)
        w[i]=lower_bound(b+1,b+rnu+1,w[i])-b;
    build(root[0],1,rnu);
    dfs1(1,0,1);
    dfs2(1,1);
    while(m--)
    {
        int u,v,k;
        scanf("%d%d%d",&u,&v,&k);
        int lca=Lca(u,v);
        printf("%d\n",(b[query(root[u],1,rnu,root[v],root[lca],root[fa[lca]],k)]));
    }
    return 0;
}


你可能感兴趣的:(acm)