BZOJ_1803_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


子树第k小,树上主席树解决。

恶心的是需要输出编号,而再开一个来存主席树上节点对应树上节点编号就非常卡空间。

于是把权值离散化。

代码:

#include <cstdio>
#include <string.h>
#include <algorithm>
using namespace std;
#define N 100050
#define maxn n
int head[N],to[N<<1],nxt[N<<1],cnt,n,m,S[N],dfn[N],root[N],t[N*30],ls[N*30],rs[N*30],tot,val[N],son[N],rr[N];
inline void add(int u,int v) {
to[++cnt]=v; nxt[cnt]=head[u]; head[u]=cnt;
}
struct A {
int num,id,v;
}a[N];
bool cmp1(const A &x,const A &y){return x.num<y.num;}
bool cmp2(const A &x,const A &y){return x.id<y.id;}
void dfs(int x,int y) {
int i;
S[++S[0]]=x; dfn[x]=S[0];
for(i=head[x];i;i=nxt[i]) {
if(to[i]!=y) {
dfs(to[i],x);
}
}
son[x]=S[0];
}
void insert(int &y,int x,int l,int r,int v) {
y=++tot; t[y]=t[x]+1;
if(l==r) return ;
int mid=(l+r)>>1;
if(v<=mid) rs[y]=rs[x],insert(ls[y],ls[x],l,mid,v);
else ls[y]=ls[x],insert(rs[y],rs[x],mid+1,r,v);
}
int query(int x,int y,int l,int r,int k) {
if(l==r) return l;
int mid=(l+r)>>1,sizls=t[ls[x]]-t[ls[y]];
if(k<=sizls) return query(ls[x],ls[y],l,mid,k);
else return query(rs[x],rs[y],mid+1,r,k-sizls);
}
int main() {
scanf("%d",&n);
int i,x,y,k;
for(i=1;i<=n;i++) scanf("%d",&a[i].num),a[i].id=i;
sort(a+1,a+n+1,cmp1);
int j=0;a[0].num=43345;
for(i=1;i<=n;i++) {
if(a[i].num!=a[i-1].num) j++;
a[i].v=j; rr[j]=a[i].id;
}
sort(a+1,a+n+1,cmp2);
for(i=1;i<n;i++) {
scanf("%d%d",&x,&y); add(x,y); add(y,x);
}
dfs(1,0);
for(i=1;i<=n;i++) {
insert(root[i],root[i-1],0,maxn,a[S[i]].v);
}
scanf("%d",&m);
while(m--) {
scanf("%d%d",&x,&k);
printf("%d\n",rr[query(root[son[x]],root[dfn[x]-1],0,maxn,k)]);
}
}

BZOJ_1803_Spoj1487 Query on a tree III_主席树+dfs序的更多相关文章

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

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

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

  3. 51 nod 1681 公共祖先 (主席树+dfs序)

    1681 公共祖先 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题   有一个庞大的家族,共n人.已知这n个人的祖辈关系正好形成树形结构(即父亲向儿子连边). 在另 ...

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

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

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

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

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

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

  7. 【SPOJ】10628. Count on a tree(lca+主席树+dfs序)

    http://www.spoj.com/problems/COT/ (速度很快,排到了rank6) 这题让我明白了人生T_T 我知道我为什么那么sb了. 调试一早上都在想人生. 唉. 太弱. 太弱. ...

  8. Codeforces 893F(主席树+dfs序)

    在子树内和距离不超过k是一个二维限制,容易想到主席树,但主席树显然没法查最小值,因为不满足区间可减.kdtree和二维线段树可以干这事,但肯定会T飞.但事实上我们的问题有一个特殊性:对某个点x,查询其 ...

  9. 刷题总结——谈笑风生(主席树+dfs序的应用)

    题目: Description 设T 为一棵有根树,我们做如下的定义:• 设a和b为T 中的两个不同节点.如果a是b的祖先,那么称“a比b不知道高明到哪里去了”.• 设a 和 b 为 T 中的两个不同 ...

随机推荐

  1. aes加解密 Illegal key size

    做aes加密时,发生一个奇怪的错误,在本地环境是好的,发布到测试环境就出问题, java.security.InvalidKeyException: Illegal key size 想到本地环境之前 ...

  2. 使用 CSS3 实现 3D 图片滑块效果

    Slicebox – A fresh 3D image slider with graceful fallback 英文原文地址:http://tympanus.NET/codrops/2011/09 ...

  3. WebLogic SSRF

    本文主要记录一下Weblogic SSRF 利用的操作过程. 一.WebLogic SSRF漏洞简介 漏洞编号:CVE-2014-4210 漏洞影响: 版本10.0.2,10.3.6 Oracle W ...

  4. day11_jsp/EL/JSTL学习笔记

    一.jsp概述 JSP全称是Java Server Pages,它和servle技术一样,都是SUN公司定义的一种用于开发动态web资源的技术. JSP实际上就是Servlet. JSP这门技术的最大 ...

  5. Oracle12c中数据泵新特性之功能增强(expdp, impdp)

    Oracle的数据泵功能在10g中被引进.本文对数据泵在12c中的增强做一个概览. 1.   禁用日志选项(DISABLE_ARCHIVE_LOGGING) Impdp的TRANSFORM参数已经扩展 ...

  6. java之jsp内置对象

    1.out对象 <% out.println("金鳞岂是池中物,<br>"); out.println("一遇风云变化龙.<br>" ...

  7. Beta 冲刺day 6

    1.昨天的困难,今天解决的进度,以及明天要做的事情 昨天的困难:在导入导出方面遇到了困难,导出的文件不能直接导入. 今天解决的进度:完成了登录页面的背景设计,并再次测试了整个系统的功能. 明天要做的事 ...

  8. (一)SpringBoot基础篇- 介绍及HelloWorld初体验

    1.SpringBoot介绍: 根据官方SpringBoot文档描述,BUILD ANYTHING WITH SPRING BOOT (用SPRING BOOT构建任何东西,很牛X呀!),下面是官方文 ...

  9. 使用清华源替代Ubuntu源

    sudo nano /etc/apt/source.list 替换为如下文本 deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial main ...

  10. Unix 和· Linux 系统概述

    一.Unix 1.Unix 定义 Unix 是一个计算机操作系统,一个用来协调.管理和控制计算机硬件和软件资源的控制程序 '2.Unix 特点 ① 多用户:在同一时刻可以有多个用户同时使用Unix操作 ...