描述


http://www.spoj.com/problems/COT/

给出一棵n个节点的树,树上每一个节点有权值.m次询问,求书上u,v路径中第k小的权值.

COT - Count on a tree

no tags 

You are given a tree with N nodes.The tree nodes are numbered from 1 to N.Each node has an integer weight.

We will ask you to perform the following operation:

  • u v k : ask for the kth minimum weight on the path from node u to node v

Input

In the first line there are two integers N and M.(N,M<=100000)

In the second line there are N integers.The ith integer denotes the weight of the ith node.

In the next N-1 lines,each line contains two integers u v,which describes an edge (u,v).

In the next M lines,each line contains three integers u v k,which means an operation asking for the kth minimum weight on the path from node u to node v.

Output

For each operation,print its result.

Example

Input:
8 5
8 5
105 2 9 3 8 5 7 7
1 2
1 3
1 4
3 5
3 6
3 7
4 8
2 5 1
2 5 2
2 5 3
2 5 4
7 8 2 
Output:
2
8
9
105
 
 

分析


POJ_2104_Kth(主席树)

现在是把原来的问题搬到树上去了.首先我们肯定要求lca,新学了Tarjan的离线算法.

每一个点建立到根节点的主席树,这样最后的结果就是u+v-2*lca,如果lca在所要求的区间内,还要再加上lca.(或者u+v-lca-p[lca]).

自己理解一下吧...挺简单的.

 #include <cstdio>
#include <algorithm>
#include <vector>
using namespace std; const int maxn=+;
int n,m,cnt,num;
int a[maxn],id[maxn],b[maxn],head[maxn],p[maxn],f[maxn],root[maxn];
bool vis[maxn];
struct edge{
int to,next;
edge(){}
edge(int a,int b):to(a),next(b){}
}g[maxn<<];
struct Qry{
int u,v,k,lca;
Qry(){}
Qry(int a,int b,int c,int d):u(a),v(b),k(c),lca(d){}
}Q[maxn];
struct node{ int l,r,s; }t[maxn*];
struct qry{
int v,id;
qry(){}
qry(int a,int b):v(a),id(b){}
};
vector <qry> q[maxn]; inline int find(int x){ return x==f[x]?x:f[x]=find(f[x]); }
void add_edge(int u,int v){
g[++cnt]=edge(v,head[u]); head[u]=cnt;
g[++cnt]=edge(u,head[v]); head[v]=cnt;
}
void update(int l,int r,int &pos,int d){
t[++num]=t[pos]; pos=num; t[pos].s++;
if(l==r) return;
int mid=l+(r-l)/;
if(d<=mid) update(l,mid,t[pos].l,d);
else update(mid+,r,t[pos].r,d);
}
bool cmp(int x,int y){ return a[x]<a[y]; }
void dfs(int u){
f[u]=u; root[u]=root[p[u]]; update(,n,root[u],b[u]);
for(int i=head[u];i;i=g[i].next){
if(g[i].to!=p[u]){
p[g[i].to]=u;
dfs(g[i].to);
f[g[i].to]=u;
}
}
vis[u]=true;
int size=q[u].size();
for(int i=;i<size;i++) if(vis[q[u][i].v]) Q[q[u][i].id].lca=find(q[u][i].v);
}
void init(){
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++) scanf("%d",&a[i]), id[i]=i;
sort(id+,id+n+,cmp);
for(int i=;i<=n;i++) b[id[i]]=i;
for(int i=;i<n;i++){
int u,v;
scanf("%d%d",&u,&v);
add_edge(u,v);
}
for(int i=;i<=m;i++){
scanf("%d%d%d",&Q[i].u,&Q[i].v,&Q[i].k);
q[Q[i].u].push_back(qry(Q[i].v,i)); q[Q[i].v].push_back(qry(Q[i].u,i));
}
}
int query(int l,int r,int x,int y,int ra,int a,int k){
if(l==r) return l;
int mid=l+(r-l)/;
int s=t[t[x].l].s+t[t[y].l].s-*t[t[ra].l].s;
if(b[a]>=l&&b[a]<=mid) s++;
if(k<=s) return query(l,mid,t[x].l,t[y].l,t[ra].l,a,k);
else return query(mid+,r,t[x].r,t[y].r,t[ra].r,a,k-s);
}
void solve(){
dfs();
for(int i=;i<=m;i++){
if(Q[i].u==Q[i].v){ printf("%d\n",a[Q[i].u]); continue; }
printf("%d\n",a[id[query(,n,root[Q[i].u],root[Q[i].v],root[Q[i].lca],Q[i].lca,Q[i].k)]]);
}
}
int main(){
init();
solve();
return ;
}

最新文章

  1. python学习笔记2-functools.wraps 装饰器
  2. jQuery Mobile 基础
  3. Razor引擎学习:RenderBody,RenderPage和RenderSection
  4. MySQL所有函数及操作符
  5. Android开发技术周报
  6. [基础]RHEL6下LINUX服务器批量部署
  7. openjduge 求简单表达式的值
  8. 【.Net Core】处理静态文件
  9. YARN调试过程中的常见问题
  10. Js/如何操作div下面的span元素或者是img之类的标签元素
  11. Redux使用教程
  12. centOS7环境下安装jdk1.8
  13. 从ACM会议分析我国计算机科学近十年发展情况
  14. ios 两个 TableView 之间的联动, TableView 与 CollectionView 之间的联动
  15. log4j的格式化打印
  16. kubeadm init 时从本地私有仓库下载镜像
  17. redis五种数据结构及使用场景
  18. 定义和使用EL函数
  19. MFC获得主窗体和父窗体指针
  20. inet_pton函数和inet_ntop函数的用法及简单实现

热门文章

  1. linux下神奇的script命令
  2. 类库探源——System.Configuration 配置信息处理
  3. _itoa_s, _i64toa_s, _ui64toa_s, _itow_s, _i64tow_s, _ui64tow_s
  4. 九度OJ 1433 FatMouse -- 贪心算法
  5. 【转】Nginx 服务器安装及配置文件详解
  6. Shell脚本——DNS自动部署
  7. javascript 单行向上滚动文字
  8. Sass中常用的函数
  9. [转]left join,right join,inner join区别
  10. PHP模块设计