【BZOJ1803】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 5

题解:这样例你告诉我是求子树第k大?(the k_th largest)分明是第k小啊!

直接主席树+DFS序搞定。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn=100010;
int n,m,cnt,tot;
int to[maxn<<1],next[maxn<<1],head[maxn],v[maxn],p[maxn],q[maxn],rt[maxn];
int ls[maxn*30],rs[maxn*30],siz[maxn*30],s[maxn*30];
struct number
{
int val,org;
}num[maxn];
bool cmp(number a,number b)
{
return a.val<b.val;
}
void insert(int x,int &y,int l,int r,int a,int b)
{
if(l>r) return ;
y=++tot,siz[y]=siz[x]+1;
if(l==r)
{
s[y]=b;
return ;
}
int mid=l+r>>1;
if(a<=mid) rs[y]=rs[x],insert(ls[x],ls[y],l,mid,a,b);
else ls[y]=ls[x],insert(rs[x],rs[y],mid+1,r,a,b);
}
int query(int a,int b,int l,int r,int c)
{
if(l==r) return s[a];
int mid=l+r>>1,sm=siz[ls[a]]-siz[ls[b]];
if(c<=sm) return query(ls[a],ls[b],l,mid,c);
else return query(rs[a],rs[b],mid+1,r,c-sm);
}
void dfs(int x,int fa)
{
p[x]=++p[0];
insert(rt[p[0]-1],rt[p[0]],1,n,v[x],x);
for(int i=head[x];i!=-1;i=next[i]) if(to[i]!=fa) dfs(to[i],x);
q[x]=p[0];
}
void add(int a,int b)
{
to[cnt]=b,next[cnt]=head[a],head[a]=cnt++;
}
int main()
{
scanf("%d",&n);
int i,a,b;
for(i=1;i<=n;i++) scanf("%d",&num[i].val),num[i].org=i;
sort(num+1,num+n+1,cmp);
for(i=1;i<=n;i++) v[num[i].org]=i;
memset(head,-1,sizeof(head));
for(i=1;i<n;i++)
{
scanf("%d%d",&a,&b);
add(a,b),add(b,a);
}
dfs(1,0);
scanf("%d",&m);
for(i=1;i<=m;i++)
{
scanf("%d%d",&a,&b);
printf("%d\n",query(rt[q[a]],rt[p[a]-1],1,n,b));
}
return 0;
}

【BZOJ1803】Spoj1487 Query on a tree III 主席树+DFS序的更多相关文章

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

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

  2. BZOJ_1803_Spoj1487 Query on a tree III_主席树+dfs序

    BZOJ_1803_Spoj1487 Query on a tree III_主席树 Description You are given a node-labeled rooted tree with ...

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

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

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

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

  7. 【DFS序】【莫队算法】【权值分块】bzoj1803 Spoj1487 Query on a tree III

    基本等同这个,只是询问的东西不大一样而已. http://www.cnblogs.com/autsky-jadek/p/4159897.html #include<cstdio> #inc ...

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

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

  9. 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 ...

随机推荐

  1. 火狐 http://localhost:8080自动跳转到http://www.localhost.com:8080

    用火狐调试PHP时 偶尔会出现连接被重置的情况:http://localhost:8080自动跳转到http://www.localhost.com:8080 解决方案1: 打开网络-属性,往下拉找到 ...

  2. 后缀数组suffix array

    倍增算法,时间复杂度O(nlogn) sa从小到大保存相对大小的下标 理解LSD,x数组,sa数组 char s[maxn]; int sa[maxn],t[maxn],t2[maxn],c[maxn ...

  3. UVA 11534 - Say Goodbye to Tic-Tac-Toe(博弈sg函数)

    UVA 11534 - Say Goodbye to Tic-Tac-Toe 题目链接 题意:给定一个序列,轮流放XO,要求不能有连续的XX或OO.最后一个放的人赢.问谁赢 思路:sg函数.每一段.. ...

  4. CSAPP:异常控制流

    在一般的情况下,处理器处理的指令序列是相邻的(顺序执行). 异常控制流提供了指令的跳转,它一部分是由硬件实现的,一部分是由操作系统实现的. 异常处理 在系统启动时,操作系统分配和初始化一张称为异常表的 ...

  5. 使用python-nmap 搭建基本端口扫描器

    代码地址如下:http://www.demodashi.com/demo/13255.html 一.前言 注意: 本文相关教程仅供个人学习使用,切勿用于非法用途,否则造成的相关损失及影响,作者不承担任 ...

  6. codeforces #550D Regular Bridge 构造

    题目大意:给定k(1≤k≤100),要求构造一张简单无向连通图,使得存在一个桥,且每一个点的度数都为k k为偶数时无解 证明: 将这个图缩边双,能够得到一棵树 那么一定存在一个叶节点,仅仅连接一条桥边 ...

  7. php对象序列化总出错false

    php unserialize 返回false的解决方法 php 提供serialize(序列化) 与unserialize(反序列化)方法. 使用serialize序列化后,再使用unseriali ...

  8. 使用Linux环境变量教程

    什么是环境变量? bash shell用一个叫作环境变量( environment variable)的特性来存储有关shell会话和工作环境的信息(这也是它们被称作环境变量的原因).这项特性允许你在 ...

  9. javascript跨浏览器事件对象类库

    一.前言 学习了javascript事件后,个人总结归纳了跨浏览器事件对象类库,方便以后使用,现分享给大家. 二.事件对象封装 将对浏览器事件对象的操作封装成eventObject.js方便调用 // ...

  10. MongoDB学习——持续更新

    參考MongoDB权威指南,学习阶段.大家多多交流问题.持续更新本文 MongoDB的长处 MongoDB具有丰富的数据模型,是面向文档的数据库. easy扩展.能够在多台server之间切割数据. ...