题意: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. JavaScript 中 return,return true,return false

    1.return: ①return + 表达式,调用函数,并返回表达式的值 ②return,终止函数 ③当代码执行到return语句时,函数返回一个结果就结束运行了,return后面的语句根本不会执行 ...

  2. urlib库的使用

    urlib库实际上不是很常用,因为其api调用复杂,已被requests模块取代. 1.request发送请求 from urllib import request #默认指定的是get请求 #url ...

  3. shell脚本报错:syntax error: unexpected end of file

    解决办法1: vi test.sh :set fileformat=unix :wq 解决办法2: yum install dos2unix dos2unix my.sh 原因剖析: DOS下文件和L ...

  4. jupyter notebook 动态图显示

    直接在import matplotlib.pyplot as plt 后面加%matplotlib,或者%matplotlib auto就可以通过弹出窗口的形式显示图片

  5. layer倒计时弹框/弹层 DEMO

    layer.msg("提示语...", { time: 5000, shade: 0.6, success: function (layero, index) { var msg ...

  6. vue评论显示隐藏,JavaScript显示关闭

    <template> <div id="vue_det"> <h1>{{details()}}</h1> <div>{{ ...

  7. Python练习:小程序,列车出票程序

    # 需求:列车某结车厢座位有15行,5列,初始状态均为'有票',当乘客选了座次,下单后,变更为'已售' # 下单时,要显示所有座位的状态(有票or已售) # 下单完毕后,显示下单后的座位状态. lin ...

  8. RAID技术详解

    RAID:Redundant Array of Independent Disks 中文我们称为独立冗余磁盘阵列.基本上是见名知意.RAID的基本思想就是将多个容量较小且价格实惠的磁盘进行组合起来构成 ...

  9. 可能是最简单的把C++Lib包装成C#可用dll的方法

    (想直接看结果的直接翻到最后) 之前对C++接触不多,最近工作需要,第三方给了一个C++的lib库,我们需要把它封装一下在C#中调用.对方要是直接给Dll就省事了... 研究了一下,基本有三个方向: ...

  10. jquery的cookie插件

    一.JS文件 /*! * jQuery Cookie Plugin v1.4.1 * https://github.com/carhartl/jquery-cookie * * Copyright 2 ...