描述


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 ;
}

SPOJ_10628_Count_on_a_Tree_(主席树+Tarjan)的更多相关文章

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

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

  3. Newnode's NOI(P?)模拟赛 第三题 (主席树优化建图 + tarjan)

    题目/题解戳这里 这道题题目保证a,b,ca,b,ca,b,c各是一个排列-mdzz考场上想到正解但是没看到是排列,相等的情况想了半天-然后写了暴力60分走人- 由于两两间关系一定,那么就是一个竞赛图 ...

  4. 【SPOJ】10628. Count on a tree(lca+主席树+dfs序)

    http://www.spoj.com/problems/COT/ (速度很快,排到了rank6) 这题让我明白了人生T_T 我知道我为什么那么sb了. 调试一早上都在想人生. 唉. 太弱. 太弱. ...

  5. 【BZOJ】1146: [CTSC2008]网络管理Network(树链剖分+线段树套平衡树+二分 / dfs序+树状数组+主席树)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1146 第一种做法(时间太感人): 第二种做法(rank5,好开心) ================ ...

  6. HDU 4729 An Easy Problem for Elfness(主席树)(2013 ACM/ICPC Asia Regional Chengdu Online)

    Problem Description Pfctgeorge is totally a tall rich and handsome guy. He plans to build a huge wat ...

  7. 【BZOJ 3772】精神污染 主席树+欧拉序

    这道题的内存…………………真·精神污染……….. 这道题的思路很明了,我们就是要找每一个路径包含了多少其他路径那么就是找,有多少路径的左右端点都在这条路径上,对于每一条路径,我们随便选定一个端点作为第 ...

  8. 【学术篇】SPOJ COT 树上主席树

    这是学完主席树去写的第二道题_(:з」∠)_ 之前用树上莫队水过了COT2... 其实COT也可以用树上莫队水过去不过好像复杂度要带个log还是怎么样可能会被卡常数.. 那就orz主席吧.... 写了 ...

  9. bzoj3207--Hash+主席树

    题目大意: 给定一个n个数的序列和m个询问(n,m<=100000)和k,每个询问包含k+2个数字:l,r,b[1],b[2]...b[k],要求输出b[1]~b[k]在[l,r]中是否出现. ...

随机推荐

  1. 免费vpn

    http://www.freevpnmac.com/macvpn/ 下边有个滑动解锁,拖动鼠标至右边解锁,点击Get vpn info,得到vpn地址ip和用户密码. 我用安卓手机测试了下完全可以,但 ...

  2. OC - 1.面向过程和面向对象的思想对比

    一.面向过程 1> 思想 面向过程是一种以过程为中心的最基础编程思想,不支持面向对象的特性. 面向过程是一种模块化程序设计方法 2> 开发方法 面向过程的开发方法是以过程(也可以说是模块) ...

  3. ArcGis for WPF(1)

    这篇文章主要是讲窗体中怎么加载一张在线地图. 第一步:首先引用ESRI.ArcGIS.Client.dll类库. 第二步:在XAML中添加如下代码: <Window x:Class=" ...

  4. 九度OJ 1042 Coincidence -- 动态规划(最长公共子序列)

    题目地址:http://ac.jobdu.com/problem.php?pid=1042 题目描述: Find a longest common subsequence of two strings ...

  5. python 访问php程序,实现定时

    #!/usr/bin/python #test2.py import sys import urllib2 j = True jj = 1##########用于统计,所以分页, url = 'htt ...

  6. AAABBBBCCCC

    语单词词性简写语的意义:,n. 名词 ,noun的缩写v. 动词 , verb的缩写pron. 代词 , pronoun的缩写adj. 形容词, adjective的缩写adv. 副词, adverb ...

  7. 点击listview 的列头对其item进行自动排序

    若要自定义排序顺序,必须编写一个实现 IComparer 接口的类,并将 ListViewItemSorter 属性设置为该类的一个对象.当设置 ListViewItemSorter 属性值时,将自动 ...

  8. 京东电话面试——PHP开发

    1.学过<数据结构>吗?你学过的计算机相关课程有哪些? 2.web操作中,当你输入一个url到看到页面,描述一下这中间发生了什么? 3.TCP/IP的请求方式都有哪些? 4.POST和GE ...

  9. [記錄用]python py2app 檔案批次重新命名

    demo.py 主要作用為 將同目錄下 *.mp4 檔案批次重新命名 例如: aaa001.mp4 ---重新命名為--> 001.mp4 aaa002.mp4 ---重新命名為--> 0 ...

  10. Ubuntu启动项设置——之update-rc.d 命令使用

    http://blog.csdn.net/typ2004/article/details/38712887 apache2.nginx.redis这些服务安装之后,会随开机启动,当这些服务并不需要时, ...