题意: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. Mock Server 实现post方法的接口(三)

    Mock Server 实现post方法的接口(三) 1.mock server实现的接口,当request中未设置"method"时,会自动将所有method试一次,所以一定要指 ...

  2. WCF部署失败

    HTTP 错误 404.3 - Not Found 由于扩展配置问题而无法提供您请求的页面.如果该页面是脚本,请添加处理程序.如果应下载文件,请添加 MIME 映射. 最可能的原因:•可能是缺少处理程 ...

  3. 安装pwntools及对于解决问题方法搜索的经验总结

    安装pwntools 按照网站(https://www.cnblogs.com/xiao3c/p/6799745.html) 中的教程进行安装 下载pwntools 输入命令 git clone ht ...

  4. [LeetCode] 139. Word Break_ Medium tag: Dynamic Programming

    Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine ...

  5. Java实现RSA密钥对并在加解密、加签验签中应用的实例

    一.项目结构 二.代码具体实现 1.密钥对生成的两种方式:一种生成公钥私文件,一种生成公钥私串 KeyPairGenUtil.java package com.wangjinxiang.genkey. ...

  6. 一起ORA-00028案例的处理过程

    前言 最近客户在测试新系统A时,遭遇ORA28,回话被终止的问题. 先了解一下大致环境,系统A由系统B通过rman还原恢复,应用程序已经在系统B跑批通过,现在系统A上遇到下面问题. 应用程序报错如下 ...

  7. 2017 6 2php用PDO链接数据库前测试

    try { $dsn = "mysql:dbname=test;host=127.0.0.1";//链接mysql的DSN(数据库驱动) $user = 'root';//Mysq ...

  8. C# Unity依赖注入

    简介: 控制反转:我们向IOC容器发出获取一个对象实例的一个请求,IOC容器便把这个对象实例“注入”到我们的手中,在这个过程中你不是一个控制者而是一个请求者,依赖于容器提供给你的资源,控制权落到了容器 ...

  9. Asp.net(C#)年月日时分秒毫秒

    年月日时分秒毫秒格式:yyyyMMddHHmmssfff

  10. 激光三角法点云重建分享——halcon 方法

    如今视觉应用方面单纯依靠2D图像解决问题存在很多问题.稳定性差.定位不准确.还有配合机器人抓取方面只能抓高度一致的物品.其实在Halcon算法库有非常强大的三维算法.包括点云重建.激光三角法(2D相机 ...