链接:

https://www.spoj.com/problems/COT/en/

思路:

首先看到求两点之前的第k小很容易想到用主席树去写,但是主席树处理的是线性结构,而这道题要求的是树形结构,我们可以用dfs跑出所有点离根的距离-dep[i](根为1,dep[1]也为1)在dfs的过程

中,我们对每一个节点建一棵线段树,那么【a,b】就是:root[a] + root[b] - root[lca(a,b)] - root[f[lca(a,b)]]; (因为a-b的路径上的权值还要算上lca(a,b)这个点,所以不是减2*root[lca(a,b)]);

实现代码:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define mid int m = (l + r) >> 1
const int M = 2e5 + ;
int p[M][],dep[M],head[M],sum[M*],ls[M*],rs[M*],root[M*];
int cnt1,n,idx,cnt,f[M]; struct node {
int to,next;
}e[M]; void add(int u,int v){
e[++cnt1].to=v;e[cnt1].next=head[u];head[u]=cnt1;
e[++cnt1].to=u;e[cnt1].next=head[v];head[v]=cnt1;
} int lca(int a,int b){
if(dep[a] > dep[b]) swap(a,b);
int h = dep[b] - dep[a]; //h为高度差
for(int i = ;(<<i)<=h;i++){ //(1<<i)&f找到h化为2进制后1的位置,移动到相应的位置
if((<<i)&h) b = p[b][i];
//比如h = 5(101),先移动2^0祖先,然后再移动2^2祖先
}
//cout<<a<<" "<<b<<endl;
if(a!=b){
for(int i = ;i >= ;i --){
if(p[a][i]!=p[b][i]){ //从最大祖先开始,判断a,b祖先,是否相同
a = p[a][i]; b = p[b][i]; //如不相同,a,b,同时向上移动2^j
}
}
a = p[a][]; //这时a的father就是LCA
}
return a;
} void build(int l,int r,int &rt){
rt = ++idx;
sum[rt] = ;
if(l == r) return;
mid;
build(l,m,ls[rt]);
build(m+,r,rs[rt]);
} void update(int p,int l,int r,int old,int &rt){
rt = ++idx;
ls[rt] = ls[old]; rs[rt] = rs[old]; sum[rt] = sum[old] + ;
if(l == r) return ;
mid;
if(p <= m) update(p,l,m,ls[old],ls[rt]);
else update(p,m+,r,rs[old],rs[rt]);
} int query(int a,int b,int lc,int cl,int l,int r,int k){
if(l == r) return l;
mid;
int cnt = sum[ls[a]] + sum[ls[b]] - sum[ls[lc]] - sum[ls[cl]];
if(k <= cnt)
return query(ls[a],ls[b],ls[lc],ls[cl],l,m,k);
else
return query(rs[a],rs[b],rs[lc],rs[cl],m+,r,k-cnt);
}
int a[M],b[M]; void dfs(int u,int fa){
f[u] = fa;
dep[u] = dep[fa] + ;
p[u][] = fa;
for(int i = ;i < ;i ++) p[u][i] = p[p[u][i-]][i-];
update(a[u],,cnt,root[fa],root[u]);
for(int i = head[u];i!=-;i = e[i].next){
int v = e[i].to;
if(v == fa) continue;
dfs(v,u);
}
} int main()
{
int m;
while(scanf("%d%d",&n,&m)!=EOF){
cnt1 = ;
memset(head,-,sizeof(head));
memset(dep,,sizeof(dep));
memset(p,,sizeof(p));
memset(f,,sizeof(f));
for(int i = ; i <= n;i ++){
scanf("%d",&a[i]);
b[i] = a[i];
}
idx = ;
int l,r,c;
sort(b+,b+n+);
cnt = unique(b+,b++n)-b-;
for(int i = ;i <= n;i ++)
a[i] = lower_bound(b+,b+cnt+,a[i]) - b;
for(int i = ;i <= n-;i ++){
scanf("%d%d",&l,&r);
add(l,r);
}
build(,cnt,root[]);
dfs(,);
for(int i = ;i <= m;i ++){
scanf("%d%d%d",&l,&r,&c);
int lc = lca(l,r);
int id = query(root[l],root[r],root[lc],root[f[lc]],,cnt,c);
printf("%d\n",b[id]);
}
}
return ;
}

spoj COT - Count on a tree (树上第K小 LCA+主席树)的更多相关文章

  1. E - Count on a tree 树上第K小

    主席树的入门题目,这道题的题意其实就是说,给你一棵树,询问在两个节点之间的路径上的区间第K小 我们如何把树上问题转换为区间问题呢? 其实DFS就可以,我们按照DFS的顺序,对线段树进行建树,那么这个树 ...

  2. SPOJ - COT Count on a tree

    地址:http://www.spoj.com/problems/COT/en/ 题目: COT - Count on a tree #tree You are given a tree with N  ...

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

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

  5. SPOJ COT Count on a tree(树上主席树 + LCA 求点第k小)题解

    题意:n个点的树,每个点有权值,问你u~v路径第k小的点的权值是? 思路: 树上主席树就是每个点建一棵权值线段树,具体看JQ博客,LCA用倍增logn求出,具体原理看这里 树上主席树我每个点的存的是点 ...

  6. spoj cot: Count on a tree 主席树

    10628. Count on a tree Problem code: COT You are given a tree with N nodes.The tree nodes are number ...

  7. SPOJ 10628. SPOJ COT Count on a tree 可持久化线段树

    这题是裸的主席树,每个节点建一棵主席树,再加个lca就可以了. 历尽艰辛,终于A掉了这一题,这般艰辛也显示出了打代码的不熟练. 错误:1.lca倍增的时候i和j写反了,RE了5次,实在要吸取教训 2. ...

  8. spoj COT - Count on a tree(主席树 +lca,树上第K大)

    您将获得一个包含N个节点的树.树节点的编号从1到Ñ.每个节点都有一个整数权重. 我们会要求您执行以下操作: uvk:询问从节点u到节点v的路径上的第k个最小权重 输入 在第一行中有两个整数Ñ和中号.( ...

  9. SPOJ 10628 COT - Count on a tree(在树上建立主席树)(LCA)

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

随机推荐

  1. golang 常用包安装

    常用到的: go get -v github.com/nsf/gocode go get -v github.com/rogpeppe/godef go get -v golang.org/x/too ...

  2. curl发送json格式数据

    php的curl方法详细的见官方手册. curl_setopt用法:  http://www.php.net/manual/en/function.curl-setopt.php <?php $ ...

  3. odooERP系统(框架)总结

    1:Odoo 是一个现代化的商业应用套件,使用 AGPL 许可证,并具有客户关系管理(CRM),人力资源,销售,采购,会计,制造,仓库管理,项目管理,以及众多社区模块. 2:它是基于一个模块化,可扩展 ...

  4. Luogu1514 NOIP2010 引水入城 BFS、贪心

    传送门 NOIP的题目都难以写精简题意 考虑最上面一排的某一个点对最下面一排的影响是什么样的,不难发现必须要是一段连续区间才能够符合题意. 如果不是一段连续区间,意味着中间某一段没有被覆盖的部分比周围 ...

  5. iisapp -a命令出现 :此脚本不能与WScript工作

    今天一个同事向我反馈,使用iis的命令时出现了如标题的问题. 通过百度,找到如下信息: iisapp实际上是存放在C:\windows \system32目录下的一个VBS脚本,全名为iisapp.v ...

  6. pandas:解决groupby().apply()方法打印两次

    对于以下dataframe执行dataframe.groupby(['name', 'course']).apply(lambda x: test(x)) 操作 其中test(x)函数为: def t ...

  7. Python 学习 第一篇:数据类型(数字,集合,布尔类型,操作符)

    Python语言最常用的对象是变量和常量,常量的值是字面意思,其值是不可变的,变量的值是可变的,例如,123,"上海"是常量,而a=1,a=2,其中a是变量名.内置的核心数据类型有 ...

  8. LVM : 快照

    LVM 机制还提供了对 LV 做快照的功能,也就是说可以给文件系统做一个备份,这也是设计 LVM 快照的主要目的.LVM 的快照功能采用写时复制技术(Copy-On-Write,COW),这比传统的备 ...

  9. High-level structure of a simple compiler高級結構的簡單編譯器

    1.lexical analysis,which analyzes the character string presented to it and divides it up into tokens ...

  10. 作业20171019 alpha-1成绩

    申诉 对成绩有疑问或不同意见的同学,请在群里[@杨贵福]. 申诉时间截止2017年11月21日 12:00. 总结 普遍成绩有明显上升,归功于1. 团队全都超额完成1次站立会议,多数团队完超额2次; ...