PT07J - Query on a tree III

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 <= 105). 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 <= 104) 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.

Example

Input:
5
1 3 5 2 7
1 2
2 3
1 4
3 5
4
2 3
4 1
3 2
3 2 Output:
5
4
5
5
【分析】给你一棵树以及每个节点的权值,求某个节点以及往下子孙中第K大的节点是哪个。
先DFS给每个节点编号,然后对于每个节点记录他的开始编号和结束编号,然后就是划分树模板了。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <time.h>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#define met(a,b) memset(a,b,sizeof a)
#define pb push_back
#define lson(x) ((x<<1))
#define rson(x) ((x<<1)+1)
using namespace std;
typedef long long ll;
const int N=1e5+;
const int M=1e6+;
int le[N],re[N],lab[N],sorted[N];
int n,m,k,num=;
map<int,int>mp;
struct Edge {
int to,next;
} edg[N*];
int head[N],tot;
void init() {
tot = ;
memset(head,-,sizeof(head));
}
void addedge(int u,int v) {
edg[tot].to = v;
edg[tot].next = head[u];
head[u] = tot++;
}
void dfs(int u,int fa) {
le[u]=++num;
sorted[num]=lab[u];
for(int i=head[u]; i!=-; i=edg[i].next) {
int v=edg[i].to;
if(v!=fa) {
dfs(v,u);
}
}
re[u]=num;
}
int tree[][N];
int toleft[][N];
void build(int l,int r,int dep) {
if(l==r)return;
int mid=(l+r)>>;
int same=mid-l+;
for(int i=l; i<=r; i++)
if(tree[dep][i]<sorted[mid])
same--;
int lpos=l;
int rpos=mid+;
for(int i=l; i<=r; i++) {
if(tree[dep][i]<sorted[mid]) { //去左边
tree[dep+][lpos++]=tree[dep][i];
} else if(tree[dep][i]==sorted[mid]&&same>) { //去左边
tree[dep+][lpos++]=tree[dep][i];
same--;
} else //去右边
tree[dep+][rpos++]=tree[dep][i];
toleft[dep][i]=toleft[dep][l-]+lpos-l;//从1到i放左边的个数
}
build(l,mid,dep+);//递归建树
build(mid+,r,dep+);
}
void initBuild() {
for(int i=; i<; i++)tree[i][]=toleft[i][]=;
for(int i=; i<=n; i++) {
tree[][i]=sorted[i];
}
sort(sorted+,sorted+n+);
build(,n,);
} int query(int L,int R,int l,int r,int dep,int k) {
if(l==r)return tree[dep][l];
int mid=(L+R)>>;
int cnt=toleft[dep][r]-toleft[dep][l-];
if(cnt>=k) {
//L+查询区间前去左边的数的个数
int newl=L+toleft[dep][l-]-toleft[dep][L-];
//左端点+查询区间会分入左边的数的个数
int newr=newl+cnt-;
return query(L,mid,newl,newr,dep+,k);//注意
} else {
//r+区间后分入左边的数的个数
int newr=r+toleft[dep][R]-toleft[dep][r];
//右端点减去区间分入右边的数的个数
int newl=newr-(r-l-cnt);
return query(mid+,R,newl,newr,dep+,k-cnt);//注意
}
}
int main() {
int u,v,x;
init();
scanf("%d",&n);
for(int i=; i<=n; i++){
scanf("%d",&lab[i]);
mp[lab[i]]=i;
}
for(int i=; i<n; i++) {
scanf("%d%d",&u,&v);
addedge(u,v);
addedge(v,u);
}
dfs(,);
initBuild();
scanf("%d",&m);
while(m--){
scanf("%d%d",&x,&k);
u=le[x];v=re[x];
int ans=query(,n,u,v,,k);
printf("%d\n",mp[ans]);
}
return ;
}

SPOJ PT07J - Query on a tree III(划分树)的更多相关文章

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

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

  2. [ SPOJ PT07J ] Query on a tree III

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

  3. SP1487 PT07J - Query on a tree III 主席树+dfs序

    Code: #include<iostream> #include<cstdio> #include<algorithm> #include<string&g ...

  4. 【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 ...

  5. SPOJ 1487 Query on a tree III(划分树)

    题目链接:http://www.spoj.com/problems/PT07J/ 题意:给出一个有根树,1为根节点,每个节点有权值.若干询问,询问以u为根的子树中权值第K小的节点编号. 思路:DFS一 ...

  6. SPOJ 375. Query on a tree (动态树)

    375. Query on a tree Problem code: QTREE You are given a tree (an acyclic undirected connected graph ...

  7. Problem: Query on the tree(二分+划分树)

    题目链接: Problem: Query on the tree Time limit: 1s     Mem limit: 64 MB      Problem Description There ...

  8. BZOJ1803Spoj1487 Query on a tree III——主席树

    题目大意 给一棵有点权的n个点的有根树,保证任意两点的点权不同,m次询问每次询问x的子树中权值第k大的点. 输入 先输入n,然后每个点点权,再输入n-1行每行两个数x,y代表x和y相连,再输入m,之后 ...

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

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

随机推荐

  1. C&C++——C与C++知识点

    C++知识点系列之一(转+整理) 编程时类声明后面千万不要忘了加分号,不然会出现很多错误!! c系列之一一.#include “filename.h”和#include<filename.h&g ...

  2. 适用于iview的表格转Excel插件

    在网上找的一个表格转excel插件,经过修改后使其适用于iview中的table组件 let idTmr; const getExplorer = () => { let explorer = ...

  3. Clevo P950系列拆机

    Clevo P950系列(包括神舟精盾T96/T97/T96C/T96E/T97E,炫龙耀9000,铁头人T800同模具机型)拆机 拆机恢复时间:20181203 12:28-14:58   一.普通 ...

  4. vue2学习篇一 $mount()手动挂载

    $mount()手动挂载 //当Vue实例没有el属性时,则该实例尚没有挂载到某个dom中: //假如需要延迟挂载,可以在之后手动调用vm.$mount()方法来挂载.例如: new Vue({ // ...

  5. linux 服务器下入侵之后的日志清理

    1.web日志的清理:access.log 和auth.log 位置在/var/log/下面. 2.系统日志存放在:/root/.bash_history

  6. 【BZOJ3700】发展城市 [LCA][RMQ]

    发展城市 Time Limit: 20 Sec  Memory Limit: 512 MB[Submit][Status][Discuss] Description 众所周知,Hzwer学长是一名高富 ...

  7. POJ2154 Color

    Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10322   Accepted: 3360 Description Bead ...

  8. Android 性能优化 - 详解内存优化的来龙去脉

    前言 APP内存的使用,是评价一款应用性能高低的一个重要指标.虽然现在智能手机的内存越来越大,但是一个好的应用应该将效率发挥到极致,精益求精. 这一篇中我们将着重介绍Android的内存优化.本文的篇 ...

  9. 安装l Xposed Framework

    How to install Xposed Framework on Android 4.x.x :   1. For Android 4.0.3 through 4.4.4 Visit this X ...

  10. Flask应用部署

    1. 介绍 前面介绍了<Linux下Nginx使用>, 但是Nginx是一个提供静态文件访问的web服务 首先我们介绍一下Web服务器, 应用服务器和应用框架的关系 客户端: 浏览器或者a ...