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. Java 数组 可变长参数 实例

    可以把类型相同但个数可变的参数传递给方法,方法中的参数声明如下: typeName...parameterName (类型名...参数名) 在方法声明中,指定类型后紧跟着省略号...,只能给方法指定一 ...

  2. Handler 引起的内存泄露

    先看一组简单的代码 1 2 3 4 5 6 7 8 9 public class SampleActivity extends Activity { private final Handler mHa ...

  3. 元素设置position:fixed属性后IE下宽度无法100%延伸

    元素设置position:fixed属性后IE下宽度无法100%延伸 IE bug 出现条件: 1.div1设置position:fixed属性,并且想要width:100%的效果. 2.div2(下 ...

  4. 探索react native首屏渲染最佳实践

    文 / 腾讯 龚麒 0.前言 react native给了我们使用javascript开发原生app的能力,在使用react native完成兴趣部落安卓端发现tab改造后,我们开始对由react n ...

  5. linux安装phpredis扩展

    1.下载扩展安装包 wget   https://github.com/nicolasff/phpredis/downloads 2.解压 tar -zxvf nicolasff-phpredis-2 ...

  6. CSS3 transform的skew属性值图文详解

    我刚刚接触transform的skew属性值时一头雾水,根本不知道种东西到底是咋变的.上网查,各个网站上也只说这个使用来做扭曲变换的,具体是咋变的就是不说....无奈我只好自己研究了,现把研究结果共享 ...

  7. 单页web应用(SPA)的简单介绍

    单页 Web 应用 (single-page application 简称为 SPA) 是一种特殊的 Web 应用.它将所有的活动局限于一个Web页面中,仅在该Web页面初始化时加载相应的HTML.J ...

  8. JAVA 23种设计模式(转)

    1.FACTORY?追MM少不了请吃饭了,麦当劳的鸡翅和肯德基的鸡翅都是MM爱吃的东西,虽然口味有所不同,但不管你带MM去麦当劳或肯德基,只管向服务员说“来四个鸡翅”就行了.麦当劳和肯德基就是生产鸡翅 ...

  9. vimrc配置文件_version1.0_+pathogen, taglist, wordcomplete插件说明

    为了表示对Ruchee的感谢,首先这是Ruchee的个人网站:http://www.ruchee.com/index.html,他的以前很多的代码都放到Git里面了,里面有链接. 看了整整一天,刚开始 ...

  10. 第一篇TC界面设计

    TC界面设计方法 第一:确定自己最终界面的大小. 第二:根据功能需要,选择相应的控价. 第三:美化界面,对界面的控件调整布局位置,设置字体颜色设置背景图片等等 脚本代码: function 按钮1_点 ...