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. 哎哟,我的.NET呀

    今早无意看到一篇文章,看着.Net被喷得越来越惨,终于还是有人忍不住要奋起反抗了哇! 虽然我已经转了java(大致方向)(没想到我是转的java方向!)好吧!其实也不算是转,就像文章所说的,我最多算是 ...

  2. dede取子栏目时重复显示同级栏目的终极解决方法

    使用channelartlist标签时,当栏目没有子栏目是,会出现重复同级栏目的问题,解决方法如下: 先看下面的代码{dede:channelartlist typeid='2'}  {dede:ty ...

  3. 自动刷新ALV

    转自http://blog.sina.com.cn/s/blog_701594f40100l8ms.html ABAP:利用SAP定时器自动刷新ALV 曾于无意之中发现,SAP系统中有个名为CL_GU ...

  4. C#堆栈讲解

    1:栈就是堆栈,因为堆和堆栈这样说太拗口了,搞得像绕口令,所以有些时候就把堆栈简称为栈.堆和栈,你看这又多舒服.但无论什么时候,堆栈都不等于堆和栈,必须说,堆和栈或者堆和堆栈. 2:值类型变量和引用类 ...

  5. TensorFlow简单介绍和在centos上的安装

    ##tensorflow简单介绍: TensorFlow™ is an open source software library for numerical computation using dat ...

  6. c 深度剖析 6

    函数的编码风格 1.注释 2,空行 3,缩进 4,参数长度,代码长度,语句长度要合适. 5,少用全局变量 6,指针仅作输入参数时,可用const 设置其为只读属性,避免其在函数中被修改. 7,函数默认 ...

  7. 《Java程序设计》第3周学习总结

    学号20145220<Java程序设计>第3周学习总结 教材学习内容总结 使用jave撰写程序几乎都在使用对象(Object),要产生对象必须先定义类(Class),类是对象的设计图,对象 ...

  8. awesome-nlp

    awesome-nlp  A curated list of resources dedicated to Natural Language Processing Maintainers - Keon ...

  9. c++语法集锦

    1.指针的引用 他也是引用,引用是特定内存块的别名 2.变量定义 更准确的说是内存使用约定,并为该约定命名 命名3.指向常变量的指针和常指针 有点拗口,都是指针,但对于所在内存块的使用约定不同.常变量 ...

  10. linux mount (挂载命令)详解

    挂接命令(mount) 首先,介绍一下挂接(mount)命令的使用方法,mount命令参数非常多,这里主要讲一下今天我们要用到的. 命令格式:mount [-t vfstype] [-o option ...