SPOJ_10628_Count_on_a_Tree_(主席树+Tarjan)

描述


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 ;
}
上一篇:PHP里获取一维数组里的最大值和最小值


下一篇:最小的MVC工程