bzoj1803 Spoj1487 Query on a tree III

Description

You are given a node-labeled rooted tree with n nodes.

Define the query (x, k): Find the node whose label is k-th largest in the subtree of the node x. Assume no two nodes have the same labels.

Input

The first line contains one integer n (1 <= n <= 10^5). The next line contains n integers li (0 <= li <= 109) which denotes the label of the i-th node.

Each line of the following n - 1 lines contains two integers u, v. They denote there is an edge between node u and node v. Node 1 is the root of the tree.

The next line contains one integer m (1 <= m <= 10^4) which denotes the number of the queries. Each line of the next m contains two integers x, k. (k <= the total node number in the subtree of x)

Output


For each query (x, k), output the index of the node whose label is the k-th largest in the subtree of the node x.

Sample Input

5

1 3 5 2 7

1 2

2 3

1 4

3 5

4

2 3

4 1

3 2

3 2

Sample Output


5

4

5

5


分析:
我们搞出一个dfs序,在此基础上建立主席树
然而题目要求输出结点编号
题目有交代:Assume no two nodes have the same labels.
没有两个结点具有一样的值,那么只要把找到的权值映射到编号上即可

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<map>

using namespace std;

const int N=100005;
map<int,int> mp;
int a[N],V[N],num[N],in[N],out[N],clo=0;
int n,m,tot=0,st[N],root[N],top=0;
struct po{
    int y,nxt;
};
po way[N<<1];
struct node{
    int l,r,sum,id;
};
node t[N*20];

void add(int x,int y)
{
    tot++;
    way[tot].y=y;way[tot].nxt=st[x];st[x]=tot;
    tot++;
    way[tot].y=x;way[tot].nxt=st[y];st[y]=tot;
}

void dfs(int now,int fa)
{
    in[now]=++clo;
    num[clo]=now;
    for (int i=st[now];i;i=way[i].nxt)
        if (way[i].y!=fa)
            dfs(way[i].y,now);
    out[now]=clo;
}

void insert(int &now,int l,int r,int x)
{
    top++;
    t[top]=t[now];
    now=top;
    t[now].sum++;
    if (l==r) return;
    int mid=(l+r)>>1;
    if (x<=mid) insert(t[now].l,l,mid,x);
    else insert(t[now].r,mid+1,r,x);
}

int ask(int x,int y,int l,int r,int k)
{
    if (l==r) return l;
    int mid=(l+r)>>1;
    int tmp=t[t[y].l].sum-t[t[x].l].sum;
    if (tmp>=k) return ask(t[x].l,t[y].l,l,mid,k);    //第k大 
    else return ask(t[x].r,t[y].r,mid+1,r,k-tmp);
}

int main()
{
    scanf("%d",&n);
    for (int i=1;i<=n;i++) scanf("%d",&a[i]),V[i]=a[i];
    sort(V+1,V+1+n);

    for (int i=1;i<n;i++)
    {
        int x,y;
        scanf("%d%d",&x,&y);
        add(x,y);
    }

    dfs(1,0);
    for (int i=1;i<=n;i++)
    {
        root[i]=root[i-1];
        int x=lower_bound(V+1,V+1+n,a[num[i]])-V;
        mp[x]=num[i];
        insert(root[i],1,n,x);
    }

    scanf("%d",&m);
    for (int i=1;i<=m;i++)
    {
        int x,k;
        scanf("%d%d",&x,&k);
        printf("%d\n",mp[ask(root[in[x]-1],root[out[x]],1,n,k)]);
    }
    return 0;
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值