[SPOJ 10628]Count on a tree
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的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- BZOJ 2588: Spoj 10628. Count on a tree( LCA + 主席树 )
Orz..跑得还挺快的#10 自从会树链剖分后LCA就没写过倍增了... 这道题用可持久化线段树..点x的线段树表示ROOT到x的这条路径上的权值线段树 ----------------------- ...
- 【BZOJ2588】Spoj 10628. Count on a tree 主席树+LCA
[BZOJ2588]Spoj 10628. Count on a tree Description 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lasta ...
- 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 ...
- 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 ...
- 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个节点的树,每个点 ...
- 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 ...
- 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 ...
随机推荐
- ssh框架-Struts2(一)
Struts2 概述 用我们自己的话来说: struts是web层框架, 相当于Servlet 作用: 1. 获得请求参数 2. 调用业务 3. 分发转向 常用的WEB层框架 Struts2入门 1. ...
- CodeBlocks使用介绍
CodeBlocks对单个文件没法调试,需要建工程后才能调试,所以使用CodeBlocks必须先建工程. 一.如何用CodeBlocks建工程文件. 选择File->New->Projec ...
- Linux学习--进程创建
进程创建 在Linux系统下,自己可以创建进程: 当进程执行时,它会被装载进虚拟内存,为程序变量分配空间,并把相关信息添到 task_struct里. 进程内存布局分为四个不同的段: • 文本段,包含 ...
- xcode修改代码目录结构出现clang:error:nosuchfileordirectory解决方法
需要迁移一个开源工程的一部分内容到自己工程,迁移对方的工程到自己工程之后,因目录结构配置整理需要,对嵌入的工程目录进行了结构改变,编译后出现: clang: error: no such file o ...
- JavaSE阶段初期的一些问题
对于如下问题1:编译阶段Demo1会报错,Demo2不会报错. class Demo1{ int i; i = 0; } class Demo2{ int i = 0; } 事实上,在java中 ...
- JAVA_SE基础——53.什么是异常?
尽管人人都希望自己的身体健康,处理事情都能顺利进行, 但是在实际生活中总会遇到各种状况,比如,感冒发烧,电脑突然蓝屏死机等..程序也一样,程序在运行过程中,也会发生各种非正常状况,比如程序运行时磁盘不 ...
- ajax和jquery使用技巧
1.使用ajax的方法的时候可以使用u方法来获取连接,这样更加安全:alert弹窗的时候需要单引号双引号火狐浏览器会报错!
- Web Api HttpWebRequest 请求 Api 及 异常处理
HttpWebRequest request = WebRequest.CreateHttp(url); request.Method = "post"; request.Head ...
- 新概念英语(1-1)Excuse me!
Excuse me!Whose handbag is it? A:Excuse me! B:Yes? A:Is this your handbag? B:Pardon? A:Is this your ...
- 新概念英语(1-119)who call out to the thieves in the dark?
who call out to the thieves in the dark? A true story Do you like stories? I want to tell you a true ...