题意:n个点的树,每个点有权值,问你u~v路径第k小的点的权值是?

思路:

树上主席树就是每个点建一棵权值线段树,具体看JQ博客,LCA用倍增logn求出,具体原理看这里

树上主席树我每个点的存的是点u到源点1的权值线段树,那我求点u到v的所有点,显然是 u + v - lca - fa[lca],就是u到1 + v到1 - 多算的lca - 多算的fa[lca]。不能减去两个lca不然少一个点了,

LCA板子:

//LCA
int fa[maxn][];
int dep[maxn];
void lca_dfs(int u, int pre, int d){
dep[u] = d;
fa[u][] = pre;
for(int i = head[u]; i != -; i = edge[i].next)
if(edge[i].v != pre)
lca_dfs(edge[i].v, u, d + );
}
void lca_update(){
for (int i = ; ( << i) <= n; i++)
for(int u = ; u <= n; u++)
fa[u][i] = fa[fa[u][i - ]][i - ];
}
int lca_query(int u, int v){
if(dep[u] < dep[v]) swap(u, v);
int d = dep[u] - dep[v];
for(int i = ; ( << i) <= d; i++) {
if(d & ( << i)) {
u = fa[u][i];
}
}
if(u != v) {
for(int i = (int)log2(n); i >= ; i--) {
if(fa[u][i] != fa[v][i]) {
u = fa[u][i];
v = fa[v][i];
}
}
u = fa[u][];
}
return u;
}

代码:

#include<cmath>
#include<set>
#include<queue>
#include<cstdio>
#include<vector>
#include<cstring>
#include <iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = + ;
const int M = maxn * ;
const ull seed = ;
const int INF = 0x3f3f3f3f;
const int MOD = ;
int n, m;
int root[maxn], a[maxn], tot;
struct Edge{
int v, next;
}edge[maxn << ];
int head[maxn], tol;
void addEdge(int u, int v){
edge[tol].v = v;
edge[tol].next = head[u];
head[u] = tol++;
}
struct node{
int lson, rson;
int sum;
}T[maxn * ];
void init(){
memset(T, , sizeof(T));
memset(root ,, sizeof(root));
memset(head, -, sizeof(head));
tot = tol = ;
}
vector<int> ve;
int getid(int x){
return lower_bound(ve.begin(), ve.end(), x) - ve.begin() + ;
}
void update(int l, int r, int &now, int pre, int v, int pos){
T[++tot] = T[pre], T[tot].sum += v, now = tot;
if(l == r) return;
int m = (l + r) >> ;
if(pos <= m)
update(l, m, T[now].lson, T[pre].lson, v, pos);
else
update(m + , r, T[now].rson, T[pre].rson, v, pos);
}
void build(int now, int pre){
update(, n, root[now], root[pre], , getid(a[now]));
for(int i = head[now]; i != -; i = edge[i].next){
int v = edge[i].v;
if(v == pre) continue;
build(v, now);
}
}
int query(int l, int r, int now, int pre, int lca, int flca, int k){
if(l == r) return l;
int m = (l + r) >> ;
int sum = T[T[now].lson].sum + T[T[pre].lson].sum - T[T[lca].lson].sum - T[T[flca].lson].sum;
if(sum >= k)
return query(l, m, T[now].lson, T[pre].lson, T[lca].lson, T[flca].lson, k);
else
return query(m + , r, T[now].rson, T[pre].rson, T[lca].rson, T[flca].rson, k - sum);
} //LCA
int fa[maxn][];
int dep[maxn];
void lca_dfs(int u, int pre, int d){
dep[u] = d;
fa[u][] = pre;
for(int i = head[u]; i != -; i = edge[i].next)
if(edge[i].v != pre)
lca_dfs(edge[i].v, u, d + );
}
void lca_update(){
for (int i = ; ( << i) <= n; i++)
for(int u = ; u <= n; u++)
fa[u][i] = fa[fa[u][i - ]][i - ];
}
int lca_query(int u, int v){
if(dep[u] < dep[v]) swap(u, v);
int d = dep[u] - dep[v];
for(int i = ; ( << i) <= d; i++) {
if(d & ( << i)) {
u = fa[u][i];
}
}
if(u != v) {
for(int i = (int)log2(n); i >= ; i--) {
if(fa[u][i] != fa[v][i]) {
u = fa[u][i];
v = fa[v][i];
}
}
u = fa[u][];
}
return u;
}
int main(){
init();
ve.clear();
scanf("%d%d", &n, &m);
for(int i = ; i <= n; i++)
scanf("%d", &a[i]), ve.push_back(a[i]);
sort(ve.begin(), ve.end());
ve.erase(unique(ve.begin(), ve.end()), ve.end());
for(int i = ; i <= n - ; i++){
int u, v;
scanf("%d%d", &u, &v);
addEdge(u, v);
addEdge(v, u);
}
lca_dfs(, , );
lca_update();
build(, );
while(m--){
int u, v, k;
scanf("%d%d%d", &u, &v, &k);
int lca = lca_query(u, v);
int ans = query(, n, root[u], root[v], root[lca], root[fa[lca][]], k);
printf("%d\n", ve[ans - ]);
}
return ;
}

SPOJ COT Count on a tree(树上主席树 + LCA 求点第k小)题解的更多相关文章

  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. Count on a tree 树上主席树

    Count on a tree 树上主席树 给\(n\)个树,每个点有点权,每次询问\(u,v\)路径上第\(k\)小点权,强制在线 求解区间静态第\(k\)小即用主席树. 树上主席树类似于区间上主席 ...

  3. spoj COT - Count on a tree (树上第K小 LCA+主席树)

    链接: https://www.spoj.com/problems/COT/en/ 思路: 首先看到求两点之前的第k小很容易想到用主席树去写,但是主席树处理的是线性结构,而这道题要求的是树形结构,我们 ...

  4. bzoj 2588 Spoj 10628. Count on a tree(主席树)

    Description 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第K小的点权.其中lastans是上一个询问的答案,初始 ...

  5. 【bzoj2588】Spoj 10628. Count on a tree 离散化+主席树

    题目描述 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第K小的点权.其中lastans是上一个询问的答案,初始为0,即第一个 ...

  6. bzoj 2588: Spoj 10628. Count on a tree【主席树+倍增】

    算是板子,把值离散化,每个点到跟上做主席树,然后查询的时候主席树上用u+v-lca-fa[lca]的值二分 #include<iostream> #include<cstdio> ...

  7. [Bzoj2588]Count on a tree(主席树+LCA)

    Description 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第K小的点权.其中lastans是上一个询问的答案,初始 ...

  8. 【BZOJ2588】Count On a Tree(主席树)

    [BZOJ2588]Count On a Tree(主席树) 题面 题目描述 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第 ...

  9. 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. RMAN备份filesperset用法

    用filesperset控制备份集的尺寸 当指定filesperset参数时,rman比较filesperset与自动计算出来的值(对每个已分配通道的文件数目) 并取其中较小的那个值来保证所有的通道被 ...

  2. spring-data-redis2.0以上配置redis连接

    1.所需的maven依赖 <dependency> <groupId>org.springframework.data</groupId> <artifact ...

  3. Scala中 zip或者zipWithIndex的用法

    问题:你要遍历一个有序集合,同时你又想访问一个循环计数器,但最重要的是你真的不需要手动创建这个计数器.解决方案:    使用zipWithIndex或者zip方法来自动地创建一个计数器,假设你有一个有 ...

  4. 自定义bash提示符

    http://adamcavendish.is-programmer.com/posts/35659.html

  5. 做一个有产品思维的研发:部署(Tomcat配置,Nginx配置,JDK配置)

    每天10分钟,解决一个研发问题. 如果你想了解我在做什么,请看<做一个有产品思维的研发:课程大纲>传送门:https://www.cnblogs.com/hunttown/p/104909 ...

  6. 详解Java内存区域?虚拟机类加载机制?

    一.Java运行时数据区域 1.程序计数器 “线程私有”的内存,是一个较小的内存空间,它可以看做当前线程所执行的字节码的行号指示器.Java虚拟机规范中唯一一个没有OutOfMemoryError情况 ...

  7. 安装rosetta2016时出现git@172.16.25.11s password: \r\nPermission denied错误,解决方法。

    当在source目录执行 ./external/scons-local/scons.py -j8 mode=release bin 时,报错 git@.11s password: \r\nPermis ...

  8. 使用datagrid时json的格式

    EasyUI的DataGrid要求返回的JSON数据集是这样的形式: {"total":总记录数量,"rows":[数据记录数组]}. 例如: {"t ...

  9. 201902<<百岁人生>>

    过年的那段时间,在家看到公司推荐的10本2019年必读书籍,里面有这本书,于是就开始了.... 第一次这么认真的看这类书籍,看完之后感触颇多,毕竟这个问题我从没思考过,很少站在这样的高度去看所有方方面 ...

  10. HttpClient 知识点

    1. httpClient 默认超时时间是 60秒 2.httpClient 是模拟表单提交,所以服务端接口要用HttpServletRequest request 接收  例如: request.g ...