spoj 10628 Count on a tree

10628. Count on a tree

Problem code: COT


You are given a tree with N nodes.The tree nodes are numbered from 1 to N.Each node has an integer weight.

We will ask you to perform the following operation:

  • u v k : ask for the kth minimum weight on the path from node u to node v

 

Input

In the first line there are two integers N and M.(N,M<=100000)

In the second line there are N integers.The ith integer denotes the weight of the ith node.

In the next N-1 lines,each line contains two integers u v,which describes an edge (u,v).

In the next M lines,each line contains three integers u v k,which means an operation asking for the kth minimum weight on the path from node u to node v.

Output

For each operation,print its result.

Example

Input:

 
 
8 5
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 2 5 2 2 5 3 2 5 4 7 8 2 
Output:
2
8
9
105
7 


树径第K大数, 树套树
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>

using namespace std;

#define N 111111

struct node
{
    int l,r,len;
}t[N*20];

int pre[N*2],now[N],son[N*2];
int a[N],b[N],dep[N],root[N];
int anc[N][20];
int n,m,q,M,tot;

void add(int x,int y)
{
    tot++;
    pre[tot]=now[x];
    now[x]=tot;
    son[tot]=y;
}

int change(int i,int l,int r, int k )
{
    ++m; t[m]=t[i];
    i=m;
    t[i].len++;
    if (l!=r)
    {
        int mid=(l+r)>>1;
        if (k<=mid)
            t[i].l=change( t[i].l, l, mid ,k);
        else
            t[i].r=change( t[i].r, mid+1,r,k);
    }
    return i;
}

void dfs( int u, int fa )
{
    anc[u][0]=fa;
    dep[u]=dep[fa]+1;
    root[u]=change(root[fa],1,M, upper_bound(b+1,b+1+M, a[u])-(b+1) );
    int p=now[u];
    while (p>0)
    {
        int v=son[p];
        if (v!=fa)
            dfs(v, u);
        p=pre[p];
    }
}

int lca(int u,int v)
{
    if (dep[u]<dep[v]) swap(u,v);
    int l=0;
    while (1<<(l+1)<=dep[u])
        l++;
    for (int i=l; i>=0; i--)
        if (dep[u]-(1<<i)>=dep[v])
            u=anc[u][i];
    if (u==v)  return v;
    for (int i=l; i>=0; i--)
        if (anc[u][i]!=-1&&anc[u][i]!=anc[v][i])
        {
            u=anc[u][i];
            v=anc[v][i];
        }
    return anc[v][0];
}

int build (int l, int r)
{
    int i=++m;
    t[i].len=0;
    if (l!=r)
    {
        int mid=(l+r)>>1;
        t[i].l=build(l,mid);
        t[i].r=build(mid+1,r);
    }
    return i;
}

int query(int u, int v, int fa, int gfa,int l,int r, int k)
{
    if (l==r)
        return l;
    int mid=(l+r)>>1;
    int cur=t[ t[u].l ].len+ t[ t[v].l ].len- t[ t[fa].l ]. len - t[ t[gfa].l ].len;
    if (k<=cur)
        return query( t[u].l, t[v].l , t[fa].l , t[gfa].l ,l , mid , k);
    else
        return query( t[u].r, t[v].r , t[fa].r , t[gfa].r, mid+1,r , k-cur);
}

int main()
{
    while (scanf("%d%d",&n,&q)!=EOF)
    {
        memset(anc,-1,sizeof(anc));
        memset(now,0,sizeof(now));
        tot=0,m=0;
        scanf("%d%d",&n,&q);
        for (int i=1; i<=n; i++)
            scanf("%d",&a[i]);
        memcpy(b,a,sizeof(a));
        sort(b+1,b+1+n);
        m=unique(b+1,b+1+n)-(b+1);
        M=m;
        for (int i=1; i<=n-1; i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            add(x,y);  add(y,x);
        }
        dep[0]=1;
        root[0]=build(1,m);
        dep[1]=1;    dfs(1,0);
        for (int j=1; (1<<j) < n; j++)
            for (int i=1; i <= n; i++)
            {
                if (anc[i][j-1]!=-1)
                {
                    int p=anc[i][j-1];
                    anc[i][j]=anc[ p ][j-1];
                }
            }
        for (int i=1; i<=q; i++)
        {
            int u,v,k;
            scanf("%d%d%d",&u,&v,&k);
            int c=lca(u,v);
            printf("%d\n",b [query( root[u],root[v],root[c],root[anc[c][0]],1, M, k ) ] );
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值