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. MvcSiteMapProvider 自定义模板

    MvcSiteMapProvider  介绍文字就省了,直接访问官方站点吧. 官方站点:https://github.com/maartenba/MvcSiteMapProvider 默认的模板文件 ...

  2. 使用lombok的@Data @NoArgsConstructor @AllArgsConstructor @EqualsAndHashCode注解,编译时报错 找不到符号

    使用lombok添加@AllArgsConstructor后报错"错误:找不到符号 符号: 问题:未启用lombok注解 解决: settings->build->compile ...

  3. 与班尼特·胡迪一起拿奖学金(HZNU-2273)

    与班尼特·胡迪一起拿奖学金 AC Time Limit:  2 s      Memory Limit:   256 MB Description 班尼特·胡迪这学期的体测终于上80分了,当期末考试的 ...

  4. 界面渐变特效 -- CSS实现 -- 兼容IE8

    特别注意:里面的RGB颜色值必须要全写,不能使用缩写.左右:background: -webkit-gradient(linear, 0 0, 0 100%, from(#80c1e7), to(#2 ...

  5. HTML标签fieldset

    一个不常用的HTML标签fieldset,不过我觉得比较有意思,其语法如下: <fieldset> <legend>fieldset名称</legend> < ...

  6. ubuntu16+zabbix3.4+grafana环境搭建记录

    最近研究了zabbix,稍后放上环境搭建教程,建议想学习搭建的同学记得参考zabbix官网

  7. 基于ASP.NET MVC 微信网页登录授权(scope为snsapi_base) 流程 上 获取OPENID

    流程图 我们需要判断是否存在OPENID  首先我们得先定义一个全局的OPENID 类似于普通账号密码登录系统的 当前登录用户ID  因为我是MVC 框架  我这里定义一个控制器基类 BaseCont ...

  8. SVN服务器搭建--Subversio与TortoiseSVN的配置安装(Windows)

    1.  Subversio和TortoiseSVN 简介 Subversio简介: Subversion是一个自由,开源的版本控制系统,可以随意地免费下载.修改.以及重新发布. 是一个通用系统,可以管 ...

  9. 关于eclipse的一些问题

    解决Eclipse,MyEclipse出现An error has occurred,See error log for more details的错误 方法1. 在"开始"--& ...

  10. nginx常用配置系列-反向代理

    接上篇,反向代理的原理与用途很多地方有讲,用文字说再多可能也表达不清楚,下面贴一张拓扑图,介绍下什么叫反向代理 以上图有两种情景 1. 访问者的客户端是 local ,要访问baidu的服务器,bai ...