Description

题库链接

求不带修改的树上路径第 \(K\) 小。 \(N\) 个节点 \(M\) 组询问。

\(1\leq N,M\leq 100000\)

Solution

主席树维护树上前缀和。分离一段路径的技巧: \(val_u+val_v-val_{lca_{u,v}}-val_{fa_{lca_{u,v}}}\) 。其余的就是 \(K-th~number\) 了。

Code

//It is made by Awson on 2018.2.28
#include <bits/stdc++.h>
#define LL long long
#define dob complex<double>
#define Abs(a) ((a) < 0 ? (-(a)) : (a))
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define Min(a, b) ((a) < (b) ? (a) : (b))
#define Swap(a, b) ((a) ^= (b), (b) ^= (a), (a) ^= (b))
#define writeln(x) (write(x), putchar('\n'))
#define lowbit(x) ((x)&(-(x)))
using namespace std;
const int N = 100000;
void read(int &x) {
char ch; bool flag = 0;
for (ch = getchar(); !isdigit(ch) && ((flag |= (ch == '-')) || 1); ch = getchar());
for (x = 0; isdigit(ch); x = (x<<1)+(x<<3)+ch-48, ch = getchar());
x *= 1-2*flag;
}
void print(int x) {if (x > 9) print(x/10); putchar(x%10+48); }
void write(int x) {if (x < 0) putchar('-'); print(Abs(x)); } int a[N+5], n, m, b[N+5], u, v, last, tot, k;
struct tt {int to, next; }edge[(N<<1)+5];
int path[N+5], Top, dep[N+5], fa[N+5], top[N+5], size[N+5], son[N+5];
struct Segment_tree {
int root[N+5], key[N*40+5], ch[N*40+5][2], pos;
int cpynode(int o) {++pos, ch[pos][0] = ch[o][0], ch[pos][1] = ch[o][1], key[pos] = key[o]; return pos; }
void insert(int &o, int l, int r, int loc) {
o = cpynode(o); ++key[o];
if (l == r) return; int mid = (l+r)>>1;
if (loc <= mid) insert(ch[o][0], l, mid, loc); else insert(ch[o][1], mid+1, r, loc);
}
int query(int a, int b, int c, int d, int l, int r, int k) {
if (l == r) return l; int mid = (l+r)>>1;
int tmp = key[ch[a][0]]+key[ch[b][0]]-key[ch[c][0]]-key[ch[d][0]];
if (tmp >= k) return query(ch[a][0], ch[b][0], ch[c][0], ch[d][0], l, mid, k);
else return query(ch[a][1], ch[b][1], ch[c][1], ch[d][1], mid+1, r, k-tmp);
}
}T; void dfs1(int o, int father, int depth) {
T.root[o] = T.root[father]; T.insert(T.root[o], 1, tot, lower_bound(b+1, b+tot+1, a[o])-b);
size[o] = 1, dep[o] = depth, fa[o] = father;
for (int i = path[o]; i; i = edge[i].next)
if (edge[i].to != father) {
dfs1(edge[i].to, o, depth+1);
size[o] += size[edge[i].to];
if (size[edge[i].to] > size[son[o]]) son[o] = edge[i].to;
}
}
void dfs2(int o, int tp) {
top[o] = tp;
if (son[o]) dfs2(son[o], tp);
for (int i = path[o]; i; i = edge[i].next)
if (edge[i].to != fa[o] && edge[i].to != son[o]) dfs2(edge[i].to, edge[i].to);
}
int get_lca(int x, int y) {
while (top[x] != top[y]) {
if (dep[top[x]] < dep[top[y]]) Swap(x, y);
x = fa[top[x]];
}
return dep[x] > dep[y] ? y : x;
}
void add(int u, int v) {edge[++Top].to = v, edge[Top].next = path[u], path[u] = Top; }
void work() {
read(n), read(m); for (int i = 1; i <= n; i++) read(a[i]), b[i] = a[i];
for (int i = 1; i < n; i++) read(u), read(v), add(u, v), add(v, u);
sort(b+1, b+n+1); tot = unique(b+1, b+n+1)-b-1;
dfs1(1, 0, 1); dfs2(1, 1);
for (int i = 1; i <= m; i++) {
read(u), read(v); u ^= last; read(k); int lca = get_lca(u, v);
write(last = b[T.query(T.root[u], T.root[v], T.root[lca], T.root[fa[lca]], 1, tot, k)]);
if (i != m) putchar('\n');
}
}
int main() {
work(); return 0;
}

[SPOJ 10628]Count on a tree的更多相关文章

  1. BZOJ 2588: Spoj 10628. Count on a tree [树上主席树]

    2588: Spoj 10628. Count on a tree Time Limit: 12 Sec  Memory Limit: 128 MBSubmit: 5217  Solved: 1233 ...

  2. BZOJ 2588: Spoj 10628. Count on a tree 树上跑主席树

    2588: Spoj 10628. Count on a tree Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/J ...

  3. Bzoj 2588: Spoj 10628. Count on a tree 主席树,离散化,可持久,倍增LCA

    题目:http://www.lydsy.com/JudgeOnline/problem.php?id=2588 2588: Spoj 10628. Count on a tree Time Limit ...

  4. BZOJ 2588: Spoj 10628. Count on a tree( LCA + 主席树 )

    Orz..跑得还挺快的#10 自从会树链剖分后LCA就没写过倍增了... 这道题用可持久化线段树..点x的线段树表示ROOT到x的这条路径上的权值线段树 ----------------------- ...

  5. 【BZOJ2588】Spoj 10628. Count on a tree 主席树+LCA

    [BZOJ2588]Spoj 10628. Count on a tree Description 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lasta ...

  6. 2588: Spoj 10628. Count on a tree

    2588: Spoj 10628. Count on a tree Time Limit: 12 Sec  Memory Limit: 128 MBSubmit: 5766  Solved: 1374 ...

  7. bzoj 2588 Spoj 10628. Count on a tree (可持久化线段树)

    Spoj 10628. Count on a tree Time Limit: 12 Sec  Memory Limit: 128 MBSubmit: 7669  Solved: 1894[Submi ...

  8. Bzoj 2588 Spoj 10628. Count on a tree(树链剖分LCA+主席树)

    2588: Spoj 10628. Count on a tree Time Limit: 12 Sec Memory Limit: 128 MB Description 给定一棵N个节点的树,每个点 ...

  9. SPOJ 10628. Count on a tree (树上第k大,LCA+主席树)

    10628. Count on a tree Problem code: COT You are given a tree with N nodes.The tree nodes are number ...

  10. SPOJ 10628 Count on a tree(Tarjan离线LCA+主席树求树上第K小)

    COT - Count on a tree #tree You are given a tree with N nodes.The tree nodes are numbered from 1 to  ...

随机推荐

  1. Week03-面向对象入门

    1. 本周学习总结 1.1 写出你认为本周学习中比较重要的知识点关键词,如类.对象.封装等 类 对象 封装 继承 覆盖 重载 构造函数 static public private toString f ...

  2. 简单的C语言编译器--概述

      在学习了编译原理的相关知识后,逐渐的掌握一个编译器的结构.作用和实现方法.同时,希望自己在不断的努力下写出一个简单的C语言编译器. 实现步骤 词法分析器:将C语言测试代码分解成一个一个的词法单元: ...

  3. 201421123042 《Java程序设计》第4周学习总结

    1. 本周学习总结 1.1 写出你认为本周学习中比较重要的知识点关键词 继承中的关键词:Soper,object,override,project, 1.2 尝试使用思维导图将这些关键词组织起来.注: ...

  4. Entity Framework Core Code First

    参考地址:https://docs.microsoft.com/zh-cn/ef/core/get-started/aspnetcore/new-db

  5. EasyUI中DataGrid隔行改变背景颜色。

    <table id="dg" class="easyui-datagrid" style="width: 1000px; height: 300 ...

  6. NFS PersistentVolume - 每天5分钟玩转 Docker 容器技术(151)

    上一节我们介绍了 PV 和 PVC,本节通过 NFS 实践. 作为准备工作,我们已经在 k8s-master 节点上搭建了一个 NFS 服务器,目录为 /nfsdata: 下面创建一个 PV mypv ...

  7. python全栈开发-logging模块(日记专用)

    一.概述 很多程序都有记录日志的需求,并且日志中包含的信息即有正常的程序访问日志,还可能有错误.警告等信息输出,python的logging模块提供了标准的日志接口,你可以通过它存储各种格式的日志,l ...

  8. Nokia大事录

    1994年,接通中国第一个GSM电话. 1995年,接通中国第一个无线数据电话. 1996年,接通中国第一个GSM1800网络电话.首家推出同时支持简繁中文短讯的移动电话--诺基亚8110.  199 ...

  9. C# JavaScriptSerializer找不到引用

    遇到一个问题,还是第一次遇到,虽然比较简单,还是记录一下 一.写了一个小工具,为了方便就建了个Form窗体,结果用到了JavaScriptSerializer类,可是怎么都找不到System.Web. ...

  10. Java中对List去重, Stream去重

    问题 当下互联网技术成熟,越来越多的趋向去中心化.分布式.流计算,使得很多以前在数据库侧做的事情放到了Java端.今天有人问道,如果数据库字段没有索引,那么应该如何根据该字段去重?大家都一致认为用Ja ...