Count on a tree SPOJ
来源:互联网 发布:重返十七岁电影知乎 编辑:程序博客网 时间:2024/05/29 09:17
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
7
题意:给你一棵由n个节点,n-1条边组成的树。问u,v之间第k小的是什么。
题解:第k小,首先想到主席树,我们建一棵由根开始的线段树。则u,v之间的关系就可以表示成 root[u]+root[v]-root[lca(u,v)]-root[fa[lca(u,v)]].因为需要知道lca,所以再套一个lca。
代码:
#include<bits/stdc++.h>using namespace std;const int N=1e6+10;int cnt,u,v,k,tot,n,m,mx;int dep[N],fa[N],siz[N],son[N];int a[N],root[N];struct node{int to,next;}edge[N<<2];struct nodee{int l,r,sum;}T[N*40];int head[N<<2];vector<int>p;int getid(int x){return lower_bound(p.begin(),p.end(),x)-p.begin()+1;}void add(int u,int v){ edge[cnt].to=v,edge[cnt].next=head[u],head[u]=cnt++; edge[cnt].to=u,edge[cnt].next=head[v],head[v]=cnt++;}void update(int l,int r,int &x,int y,int pos){ T[++cnt]=T[y],x=cnt,T[x].sum++; if(l==r) return ; int mid=(r+l)>>1; if(pos<=mid) update(l,mid,T[x].l,T[y].l,pos); else update(mid+1,r,T[x].r,T[y].r,pos);}int query(int l,int r,int x,int y,int z,int rr,int k){ if(l>=r) return l; int mid=(r+l)>>1; int sum=T[T[y].l].sum+T[T[x].l].sum-T[T[z].l].sum-T[T[rr].l].sum; if(k<=sum) query(l,mid,T[x].l,T[y].l,T[z].l,T[rr].l,k); else query(mid+1,r,T[x].r,T[y].r,T[z].r,T[rr].r,k-sum);}void dfs1(int u,int f,int d){ dep[u]=d,fa[u]=f,siz[u]=1,son[u]=0; for(int i=head[u];i!=-1;i=edge[i].next) { int v=edge[i].to; if(v==f)continue; dfs1(v,u,d+1); siz[u]+=siz[v]; if(siz[son[u]]<siz[v]) son[u]=v; }}int top[N],pre[N],tree[N];void dfs2(int u,int tp){ top[u]=tp,tree[u]=++tot,pre[tree[u]]=u; update(1,mx,root[u],root[fa[u]],getid(a[u])); if(!son[u]) return ; dfs2(son[u],tp); for(int i=head[u];i!=-1;i=edge[i].next) { int v=edge[i].to; if(v==fa[u]||v==son[u]) continue; dfs2(v,v); }}int lca(int x,int y){ int fx=top[x],fy=top[y]; while(fx!=fy) { if(dep[fx]<dep[fy]) swap(x,y),swap(fx,fy); x=fa[fx],fx=top[x]; } if(dep[x]>dep[y]) swap(x,y); return x;}void init(){ tot=0; cnt=0; memset(head,-1,sizeof(head));}int main(){ init(); scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) { scanf("%d",&a[i]); p.push_back(a[i]); } for(int i=1;i<n;i++) { scanf("%d%d",&u,&v); add(u,v); } cnt=0; sort(p.begin(),p.end()),p.erase(unique(p.begin(),p.end()),p.end()); mx=p.size(); dfs1(1,0,1),dfs2(1,1); while(m--) { scanf("%d%d%d",&u,&v,&k); int lc=lca(u,v); printf("%d\n",p[query(1,mx,root[u],root[v],root[lc],root[fa[lc]],k)-1]); }}
- SPOJ Count on a tree
- Count on a tree SPOJ
- Count on a tree SPOJ
- spoj cot Count on a tree
- spoj 10628 Count on a tree
- SPOJ 10628. Count on a tree
- 【主席树】 SPOJ Count on a tree
- spoj Count on a tree【主席树】
- SPOJ COT 10628 Count on a tree
- spoj 10628 Count on a tree
- 2588: Spoj 10628. Count on a tree
- 【SPOJ】Count on a Tree Ⅱ (COT2)
- [BZOJ2588] Spoj 10628. Count on a tree
- 2588: Spoj 10628. Count on a tree
- bzoj2588: Spoj 10628. Count on a tree
- BZOJ2588: Spoj 10628. Count on a tree
- bzoj2588 Spoj 10628. Count on a tree
- 【bzoj2588】Spoj 10628. Count on a tree
- 对泰坦尼克号案例进行数据挖掘
- python hashlib sha1和MD5哈希算法
- android performClick使用说明
- android studio编译的时候提示gradle版本不对
- 运用贝塞尔曲线描绘android翻页效果
- Count on a tree SPOJ
- C++更改文件名
- PHP$a[]和$a的区别
- CentOs6.7 + hadoop2.8 安装
- Android开发之实现滑动RecyclerView,浮动按钮的显示和隐藏(一)
- 关于e瞳网社团web后端部门发展的一点想法
- zookeeper的重连思考
- 我的大学
- Maven提示failed while installing Dynamic web module 2.2的解决方案