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.
按dfs序建可持久化线段树,子树对应连续的区间,可以直接查询kth
#include<cstdio>
inline int _int(){
int x=,c=getchar();
while(c>||c<)c=getchar();
while(c>&&c<)x=x*+c-,c=getchar();
return x;
}
const int N=;
int n;
int v[N],rt[N],id[N],idr[N],idp=;
int es[N*],enx[N*],e0[N],ep=;
int ch[N*][],sz[N*],ids[N*],p=;
int ins(int w,int x,int id){
int u=++p,u0=u;
for(int i=;~i;i--){
int d=x>>i&;
ch[u][d^]=ch[w][d^];
sz[u=ch[u][d]=++p]=sz[w=ch[w][d]]+;
}
ids[u]=id;
return u0;
}
void dfs(int w,int pa){
id[w]=++idp;
rt[idp]=ins(rt[idp-],v[w],w);
for(int i=e0[w];i;i=enx[i]){
int u=es[i];
if(u!=pa)dfs(u,w);
}
idr[w]=idp;
}
void kth(int w1,int w2,int k){
for(int i=;~i;i--){
int s=sz[ch[w1][]]-sz[ch[w2][]];
if(s<k){
k-=s;
w1=ch[w1][];
w2=ch[w2][];
}else{
w1=ch[w1][];
w2=ch[w2][];
}
}
printf("%d\n",ids[w1]);
}
int main(){
n=_int();
for(int i=;i<=n;i++)v[i]=_int();
for(int i=;i<n;i++){
int a=_int(),b=_int();
es[ep]=b;enx[ep]=e0[a];e0[a]=ep++;
es[ep]=a;enx[ep]=e0[b];e0[b]=ep++;
}
dfs(,);
for(int q=_int();q;q--){
int x=_int();
kth(rt[idr[x]],rt[id[x]-],_int());
}
return ;
}

bzoj1803: Spoj1487 Query on a tree III的更多相关文章

  1. 【DFS序】【莫队算法】【权值分块】bzoj1803 Spoj1487 Query on a tree III

    基本等同这个,只是询问的东西不大一样而已. http://www.cnblogs.com/autsky-jadek/p/4159897.html #include<cstdio> #inc ...

  2. 【BZOJ1803】Spoj1487 Query on a tree III 主席树+DFS序

    [BZOJ1803]Spoj1487 Query on a tree III Description You are given a node-labeled rooted tree with n n ...

  3. 【bzoj1803】Spoj1487 Query on a tree III DFS序+主席树

    题目描述 You are given a node-labeled rooted tree with n nodes. Define the query (x, k): Find the node w ...

  4. bzoj 1803: Spoj1487 Query on a tree III(主席树)

    题意 你被给定一棵带点权的n个点的有根数,点从1到n编号. 定义查询 query(x,k): 寻找以x为根的k大点的编号(从小到大排序第k个点) 假设没有两个相同的点权. 输入格式: 第一行为整数n, ...

  5. 「SPOJ1487」Query on a tree III

    「SPOJ1487」Query on a tree III 传送门 把树的 \(\text{dfs}\) 序抠出来,子树的节点的编号位于一段连续区间,然后直接上建主席树区间第 \(k\) 大即可. 参 ...

  6. SP1487 PT07J - Query on a tree III (主席树)

    SP1487 PT07J - Query on a tree III 题意翻译 你被给定一棵带点权的n个点的有根数,点从1到n编号. 定义查询 query(x,k): 寻找以x为根的k大点的编号(从小 ...

  7. SPOJ PT07J - Query on a tree III(划分树)

    PT07J - Query on a tree III #tree You are given a node-labeled rooted tree with n nodes. Define the ...

  8. SPOJ Query on a tree III (树剖(dfs序)+主席树 || Splay等平衡树)(询问点)

    You are given a node-labeled rooted tree with n nodes. Define the query (x, k): Find the node whose ...

  9. [ SPOJ PT07J ] Query on a tree III

    \(\\\) Description 其实这题才是正版的 Qtree3...... 给定 \(n\) 个点,以 \(1\) 号节点为根的树,点有点权. \(m\) 次询问 以 \(x\) 为根的子树内 ...

随机推荐

  1. Install Fastx (zz)

    Fastx-toolkit installation on CentOS ===================================== Tested on CentOS release ...

  2. 345. Reverse Vowels of a String

    Write a function that takes a string as input and reverse only the vowels of a string. Example 1:Giv ...

  3. HDU 5775 树状数组

    Bubble Sort Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

  4. iPhone 6/iPhone 6 Plus硬件性能

    iPhone 6/iPhone 6 Plus硬件性能 评分 4 分 正 如之前传闻的,这次同时发布了两款iPhone产品,分别是4.7英寸的iPhone 6和5.5英寸的iPhone 6 Plus.苹 ...

  5. HTTP.sys漏洞验证及防护

    使用发包工具构造http请求包检测 以fiddler工具为例,构造如下图的请求包: 1 GET http://192.168.174.145/ HTTP/1.12 Host: 192.168.174. ...

  6. UVa 10082 WERTYU

    UVa 10082 题目大意:把手放在键盘上时,稍微不注意就会往右错一位.这样,输入Q就会变成输入W,输入J会变成输入K等等, 输入一个错位后敲出的字符串(所有字母均大写),输出程序员本来想打的句子. ...

  7. Python字符串分割

    代码如下: [root@localhost test]# cat 3.py #coding=utf-8 ev = """ 1evilxr 2www 3nihao 4evi ...

  8. 越狱Season 1-Episode 21: Go

    Season 1, Episode 21: Go -Michael: I need you to let me get us out of here. 我需要你帮我出去 -Patoshik: If y ...

  9. java的nio之:java的nio的原理

    转载:http://weixiaolu.iteye.com/blog/1479656 Java NIO原理图文分析及代码实现 前言: 最近在分析hadoop的RPC(Remote Procedure ...

  10. 另一个分区工具GNU的parted[转自vbird]

    利用 GNU 的 parted 进行分割行为 虽然你可以使用 fdisk 很快速的将你的分割槽切割妥当,不过 fdisk 却无法支持到高于 2TB 以上的分割槽! 此时就得需要 parted 来处理了 ...