题意:

求树上A,B两点路径上第K小的数

分析:

同样是可持久化线段树,只是这一次我们用它来维护树上的信息。

我们之前已经知道,可持久化线段树实际上是维护的一个前缀和,而前缀和不一定要出现在一个线性表上。

比如说我们从一棵树的根节点进行DFS,得到根节点到各节点的距离dist[x]——这是一个根-x路径上点与根节点距离的前缀和。

利用这个前缀和,我们可以解决一些树上任意路径的问题,比如在线询问[a,b]点对的距离——答案自然是dist[a]+dist[b]-2*dist[lca(a,b)]。

同理,我们可以利用可持久化线段树来解决树上任意路径的问题。

DFS遍历整棵树,然后在每个节点上建立一棵线段树,某一棵线段树的“前一版本”是位于该节点父亲节点fa的线段树。

利用与之前类似的方法插入点权(排序离散)。那么对于询问[a,b],答案就是root[a]+root[b]-root[lca(a,b)]-root[fa[lca(a,b)]]上的第k大。

// File Name: cot.cpp
// Author: Zlbing
// Created Time: 2013年10月09日 星期三 19时24分55秒 #include<iostream>
#include<string>
#include<algorithm>
#include<cstdlib>
#include<cstdio>
#include<set>
#include<map>
#include<vector>
#include<cstring>
#include<stack>
#include<cmath>
#include<queue>
using namespace std;
#define CL(x,v); memset(x,v,sizeof(x));
#define INF 0x3f3f3f3f
#define LL long long
#define REP(i,r,n) for(int i=r;i<=n;i++)
#define RREP(i,n,r) for(int i=n;i>=r;i--)
const int MAXN=1e5+;
const int POW=;
int num[MAXN],hash[MAXN];
int ls[MAXN*],rs[MAXN*];
int sum[MAXN*];
int root[MAXN];
vector<int> G[MAXN];
int d[MAXN];
int p[MAXN][POW];
int tot;
int f[MAXN];
void build(int l,int r,int& rt)
{
rt=++tot;
sum[rt]=;
if(l>=r)return;
int m=(l+r)>>;
build(l,m,ls[rt]);
build(m+,r,rs[rt]);
}
void update(int last,int p,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 m=(l+r)>>;
if(p<=m)update(ls[last],p,l,m,ls[rt]);
else update(rs[last],p,m+,r,rs[rt]);
}
int query(int left_rt,int right_rt,int lca_rt,int lca_frt,int l,int r,int k)
{
if(l>=r)return l;
int m=(l+r)>>;
int cnt=sum[ls[right_rt]]+sum[ls[left_rt]]-sum[ls[lca_rt]]-sum[ls[lca_frt]];
if(k<=cnt)
return query(ls[left_rt],ls[right_rt],ls[lca_rt],ls[lca_frt],l,m,k);
else
return query(rs[left_rt],rs[right_rt],rs[lca_rt],rs[lca_frt],m+,r,k-cnt);
}
void dfs(int u,int fa,int cnt)
{
f[u]=fa;
d[u]=d[fa]+;
p[u][]=fa;
for(int i=;i<POW;i++)p[u][i]=p[p[u][i-]][i-]; update(root[fa],num[u],,cnt,root[u]);
for(int i=;i<(int)G[u].size();i++)
{
int v=G[u][i];
if(v==fa)continue;
dfs(v,u,cnt);
}
}
int lca(int a,int b)
{
if(d[a]>d[b])a^=b,b^=a,a^=b;
if(d[a]<d[b])
{
int del=d[b]-d[a];
for(int i=;i<POW;i++)
if(del&(<<i))b=p[b][i];
}
if(a!=b)
{
for(int i=POW-;i>=;i--)
{
if(p[a][i]!=p[b][i])
{
a=p[a][i],b=p[b][i];
}
}
a=p[a][],b=p[b][];
}
return a;
}
int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
REP(i,,n)
{
G[i].clear();
}
CL(d,);
CL(p,);
CL(f,);
REP(i,,n)
{
scanf("%d",&num[i]);
hash[i]=num[i];
}
tot=;
sort(hash+,hash++n);
int cnt=unique(hash+,hash+n+)-hash-;
REP(i,,n)
{
num[i]=lower_bound(hash+,hash+cnt+,num[i])-hash;
}
int a,b,c;
REP(i,,n-)
{
scanf("%d%d",&a,&b);
G[a].push_back(b);
G[b].push_back(a);
}
build(,cnt,root[]);
dfs(,,cnt);
REP(i,,m)
{
scanf("%d%d%d",&a,&b,&c);
int t=lca(a,b);
int id=query(root[a],root[b],root[t],root[f[t]],,cnt,c);
printf("%d\n",hash[id]);
}
}
return ;
}

SPOJ-COT-Count on a tree(树上路径第K小,可持久化线段树)的更多相关文章

  1. Count on a tree(树上路径第K小)

    题目链接:https://www.spoj.com/problems/COT/en/ 题意:求树上A,B两点路径上第K小的数 思路:主席树实际上是维护的一个前缀和,而前缀和不一定要出现在一个线性表上. ...

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

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

  3. Count on a tree 树上区间第K小

    Count on a tree 题意:求路径 u到v上的 第k小的权重. 题解:先DFS建数, 然后对于每个节点往上跑出一颗主席树, 然后每次更新. 查询的时候, u, v, k, 找到  z = l ...

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

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

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

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

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

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

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

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

随机推荐

  1. 复杂对象创建终结者(Builder Pattern)

    捣鼓了很长时间,终于对建造者模式有初步理解,现在写篇记录下.缘起就是创建的对象比较复杂,需按功能分散.类似造一辆汽车,作为汽车厂家,你需要造车身,造轮胎等,精髓在于领导者(Director),领导者指 ...

  2. 图像本地预览插件(基于JQUERY、HTML5)

    最近是被这项目搞疯了.害我天天写插件,上周才写,现在就继续吧..... 说说这个吧.主要是用于本地图像预览的.我们知道在以前,图像预览一般都很麻烦,一般都是异步上传然后返回路径,动态设置路径,但是这样 ...

  3. List和ArrayList,LinkList的区别

    接口 List<E> 是一个接口: ArrayList<E> 是一个类:是一个实现了List接口的类,因此可以List里面定义的所有的方法都实现了. 1.ArrayList是实 ...

  4. 学习java随笔第六篇:数组

    一维数组 创建一维数组并输出 public class OneDimensionalArray { public static void main(String argas[]) { int i[]= ...

  5. Windows I/O模型、同步/异步、阻塞/非阻塞

    转载自:http://www.cppblog.com/tx7do/articles/5954.html 同步 所谓同步,就是在发出一个功能调用时,在没有得到结果之前,该调用就不返回.按照这个定义,其实 ...

  6. Activity以singleTask模式启动,intent传值的解决办法

    转载请注明出处,谢谢http://blog.csdn.net/harryweasley/article/details/46557827 因为项目中,有一个消息推送的功能,每次推送一个消息,就会开启F ...

  7. For each db / table

    use master go exec master..sp_MSforeachdb 'use [?]; IF (SELECT db_id(''?'')) > 4 and (SELECT DATA ...

  8. dense_rank()+hash提示改写优化SQL

    数据库环境:SQL SERVER 2005 今天看到一条SQL,返回10条数据,执行了50多S.刚好有空,就对它进行了优化,优化后1S出结果. 先看下原始SQL SELECT t1.line_no , ...

  9. line-height行高使用技巧

    若父元素标签高度一定,假设为150px,子元素需要垂直居中,再重新给子元素设置一个行高就好了,省事省力

  10. 在js脚本里计算多个小数的加法问题

    当在js脚本里计算多个小数的加法时,算得的结果往往会自动取整,这时候我们就应该加入以下代码: function toDecimal(x) { var val = Number(x); if (!isN ...