「SPOJ1487」Query on a tree III

传送门

把树的 \(\text{dfs}\) 序抠出来,子树的节点的编号位于一段连续区间,然后直接上建主席树区间第 \(k\) 大即可。

参考代码:

#include <algorithm>
#include <cstdio>
#define rg register
#define file(x) freopen(x".in", "r", stdin), freopen(x".out", "w", stdout)
using namespace std;
template < class T > inline void read(T& s) {
s = 0; int f = 0; char c = getchar();
while ('0' > c || c > '9') f |= c == '-', c = getchar();
while ('0' <= c && c <= '9') s = s * 10 + c - 48, c = getchar();
s = f ? -s : s;
} const int _ = 1e5 + 5; int tot, head[_], nxt[_ << 1], ver[_ << 1];
inline void Add_edge(int u, int v)
{ nxt[++tot] = head[u], head[u] = tot, ver[tot] = v; } int n, q, a[_], X[_], dfn[_], rev[_], siz[_], pos[_];
int tt, rt[_], lc[_ << 5], rc[_ << 5], cnt[_ << 5]; inline void build(int& p, int l = 1, int r = n) {
p = ++tt;
if (l == r) return ;
int mid = (l + r) >> 1;
build(lc[p], l, mid), build(rc[p], mid + 1, r);
} inline void update(int& p, int q, int v, int l = 1, int r = n) {
p = ++tt, lc[p] = lc[q], rc[p] = rc[q], cnt[p] = cnt[q] + 1;
if (l == r) return ;
int mid = (l + r) >> 1;
if (v <= mid) update(lc[p], lc[q], v, l, mid);
else update(rc[p], rc[q], v, mid + 1, r);
} inline int query(int p, int q, int k, int l = 1, int r = n) {
if (l == r) return l;
int mid = (l + r) >> 1, num = cnt[lc[p]] - cnt[lc[q]];
if (num >= k) return query(lc[p], lc[q], k, l, mid);
else return query(rc[p], rc[q], k - num, mid + 1, r);
} inline void dfs(int u, int f) {
rev[dfn[u] = ++dfn[0]] = u, siz[u] = 1;
for (rg int i = head[u]; i; i = nxt[i]) {
int v = ver[i]; if (v == f) continue ;
dfs(v, u), siz[u] += siz[v];
}
} int main() {
read(n);
for (rg int i = 1; i <= n; ++i) read(a[i]), X[i] = a[i];
sort(X + 1, X + n + 1);
for (rg int i = 1; i <= n; ++i)
a[i] = lower_bound(X + 1, X + n + 1, a[i]) - X, pos[a[i]] = i;
for (rg int u, v, i = 1; i < n; ++i)
read(u), read(v), Add_edge(u, v), Add_edge(v, u);
dfs(1, 0), build(rt[0]);
for (rg int i = 1; i <= n; ++i) update(rt[i], rt[i - 1], a[rev[i]]);
read(q);
for (rg int l, r, x, k; q--; ) {
read(x), read(k), l = dfn[x], r = dfn[x] + siz[x] - 1;
printf("%d\n", pos[query(rt[r], rt[l - 1], k)]);
}
return 0;
}

「SPOJ1487」Query on a tree III的更多相关文章

  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. 「luogu2633」Count on a tree

    「luogu2633」Count on a tree 传送门 树上主席树板子. 每个节点的根从其父节点更新得到,查询的时候差分一下就好了. 参考代码: #include <algorithm&g ...

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

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

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

  5. 「SPOJ10707」Count on a tree II

    「SPOJ10707」Count on a tree II 传送门 树上莫队板子题. 锻炼基础,没什么好说的. 参考代码: #include <algorithm> #include &l ...

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

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

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

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

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

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

随机推荐

  1. tkinter学习(5)messagebox、pack、grid和place方法

    1.messagebox信息弹出框 1.1 代码: import tkinter as tk #导出tk模块 import tkinter.messagebox #导出弹出信息框 #定义窗口.标题.大 ...

  2. mysql DATE_FORMAT 函数使用

    使用DATE_FORMAT 查询数据库中时间类型显示 SELECT a.name ,a.uuid ,p.assess_price as assessPrice ,p.assess_date as as ...

  3. css——伪类选择器

    <body> <div class="box">   <p>0</p>         <div>1</div&g ...

  4. EF中 GroupJoin 与 Join

    数据: GroupJoin: 返回左表所有数据 using (tempdbEntities context = new tempdbEntities()) { var query = context. ...

  5. 批量给文件加一个后缀 .bak , 一条命令去掉文件的后缀 linux

    给当前文件夹下面的所有文件加上一个后缀 == 一条命令解决 ll | awk '{print $9}' | grep -v '^[ ]*$' |sed -r 's#(.*)#mv \1 \1.bak# ...

  6. gatling高性能测试工具

    使用mvn命令直接测试.  (loadrunner-----) 1.新建maven工程 2.在pom中导入依赖 <dependencies> <dependency> < ...

  7. Python:json 模块

    字符串转dict.list data = "[{....},{...},...]" list_data = json.loads(data) dict.list转字符串 list ...

  8. FastDFS上传文件访问url地址直接下载

    fdfs 存储节点storage安装nginx,修改nginx配置文件 location ~/group[1-9]/M00 {  if ( $query_string ~* ^(.*)paramete ...

  9. Esp32的一些资料链接

    http://wiki.ai-thinker.com/doku.php/utils/esp32_idf_menuconfig   http://wiki.ai-thinker.com/doku.php ...

  10. adblock广告过滤

    1.在 img的 src中 出现 ad连在一起的情况,会被adblock过滤掉. 例如 <img id="adasdd" class="ad_mina" ...