http://www.spoj.com/problems/COT/ 树上第k小元素

LCA + 可持久化线段树

每个新的版本都是由其父亲版本转化而来。

 #include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring> using namespace std; const int maxn = 1e5 + ;
const int maxd = ;
struct Edge{
int v, next;
}p[maxn << ];
int head[maxn], e, d[maxn], f[maxn][maxd];
//LCA
void init(){
memset(d, , sizeof(d));
memset(f, , sizeof(f));
memset(head, -, sizeof(head));
e = ;
}
void addEdge(int u, int v){
p[e].v = v; p[e].next = head[u]; head[u] = e++;
swap(u, v);
p[e].v = v; p[e].next = head[u]; head[u] = e++;
}
int lca(int u, int v){
if (d[u] < d[v]) swap(u, v);
int k = d[u] - d[v];
for (int i = ; i < maxd; ++i)
if (( << i) & k) u = f[u][i];
if (u == v) return u;
for (int i = maxd - ; i >= ; --i){
if (f[u][i] != f[v][i]){
u = f[u][i];
v = f[v][i];
}
}
return f[u][];
}
//CMT
int ls[maxn * ], rs[maxn * ], sum[maxn * ], T[maxn], tot, rt;
int num[maxn], san[maxn], n, m; // 离散化前点数,离散化后点数
void init_hash(){
for (int i = ; i <= n; ++i) san[i] = num[i];
sort(san + , san + n + );
m = unique(san + , san + n + ) - san - ;
}
int hash(int x){
return lower_bound(san + , san + m + , x) - san;
}
void build(int l, int r, int& rt){
rt = ++ tot; sum[rt] = ;
if (l == r) return;
int mid = (l + r) >> ;
build(l, mid, ls[rt]);
build(mid + , r, rs[rt]);
}
void update(int last, int pos, int l, int r, int& rt){
rt = ++ tot;
ls[rt] = ls[last], rs[rt] = rs[last], sum[rt] = sum[last] + ;
if (l == r) return ;
int mid = (l + r) >> ;
if (pos <= mid) update(ls[last], pos, l, mid, ls[rt]);
else update(rs[last], pos, mid + , r, rs[rt]);
}
int query(int pos, int left_rt, int right_rt, int lca_rt, int l, int r, int k){
if (l == r) return l;
int mid = (l + r) >> ;
int cnt = sum[ls[left_rt]] + sum[ls[right_rt]] - * sum[ls[lca_rt]] + (pos >= l && pos <= mid);//注意lca为跟的时候
if (k <= cnt) return query(pos, ls[left_rt], ls[right_rt], ls[lca_rt], l, mid, k);
else return query(pos, rs[left_rt], rs[right_rt], rs[lca_rt], mid + , r, k - cnt);
}
//LCA && CMT
void dfs(int u, int fa){
f[u][] = fa;//注意
d[u] = d[f[u][]] + ;
update(T[fa], hash(num[u]), , m, T[u]);
for (int i = ; i < maxd; ++i) f[u][i] = f[ f[u][i - ] ][i - ];
for (int i = head[u]; ~i; i = p[i].next){
if (p[i].v == fa) continue;
dfs(p[i].v, u);
}
}
int main(){
int q, u, v, k;
while (scanf("%d%d", &n, &q) == ){
for (int i = ; i <= n; ++i) scanf("%d", &num[i]);
init();
init_hash();
tot = ;
for (int i = ; i < n; ++i){
scanf("%d%d", &u, &v);
addEdge(u, v);
}
build(, m, T[]);
dfs(, );
while (q--){
scanf("%d%d%d", &u, &v, &k);
printf("%d\n",san[query(hash(num[lca(u, v)]), T[u], T[v], T[lca(u, v)],, m, k)]);
}
}
return ;
}

spoj 10628的更多相关文章

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

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

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

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

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

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

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

  7. BZOJ 2588: Spoj 10628. Count on a tree-可持久化线段树+LCA(点权)(树上的操作) 无语(为什么我的LCA的板子不对)

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

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

  9. SPOJ 10628 Count on a tree(Tarjan离线 | RMQ-ST在线求LCA+主席树求树上第K小)

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

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

随机推荐

  1. win7安装centos7双系统

    采用硬盘安装 前景 打算用U盘安装,但是u盘是FAT32格式限制了文件4g大小,我官网下的iso镜像大于4g,只好采用硬盘安装. 其实U盘安装是最方便的,网上很多教程用UltraISO软件把U盘直接作 ...

  2. java中short、int、long、float、double取值范围

    一.分析基本数据类型的特点,最大值和最小值.1.基本类型:int 二进制位数:32包装类:java.lang.Integer最小值:Integer.MIN_VALUE= -2147483648 (-2 ...

  3. 深入解析淘宝Diamond之客户端架构

    转载:http://blog.csdn.net/u013970991/article/details/52088350 一.什么是Diamond diamond是淘宝内部使用的一个管理持久配置的系统, ...

  4. Codeforces Round #307 (Div. 2) D. GukiZ and Binary Operations (矩阵高速幂)

    题目地址:http://codeforces.com/contest/551/problem/D 分析下公式能够知道,相当于每一位上放0或者1使得最后成为0或者1.假设最后是0的话,那么全部相邻位一定 ...

  5. Laravel 学习 .env文件 getenv 获得环境变量的值

    Laravel 学习 .env文件 getenv 获得环境变量的值  我们还需要对应用的 .env 文件进行设置,为应用指定数据库名称 sample. .env . . . DB_DATABASE=s ...

  6. Linux下安卓ndk混合编译调用so方法——QuickStart学习

    转自:http://www.52pojie.cn/thread-313869-1-1.html #注意:.h 和.c中的错误eclipse不会检查,只会调用时在手机或虚拟机中死掉.因此需要仔细检查其中 ...

  7. android 类似微信的摇一摇实现

    一.在 AndroidManifest.xml 中添加操作权限 <uses-permission android:name="android.permission.VIBRATE&qu ...

  8. python学习日记:np.newaxis

    import numpy as np label = np.array([[1,2,3,4],[5,6,7,8]])print (label.shape)label = label[np.newaxi ...

  9. .bat批处理基础

    批处理(Batch),也称为批处理脚本.顾名思义,批处理就是对某对象进行批量的处理,通常被认为是一种简化的脚本语言,它应用于DOS和Windows系统中.批处理文件的扩展名为bat .目前比较常见的批 ...

  10. Hive 练习 简单任务处理

    1.2018年4月份的用户数.订单量.销量.GMV (不局限与这些统计量,你也可以自己想一些) -- -- -- 2018年4月份的用户数量 select count(a.user_id) as us ...