1803: Spoj1487 Query on a tree III
来源:互联网 发布:软件系统分析 编辑:程序博客网 时间:2024/06/03 20:47
题目链接
题目大意:子树k大
题解:dfs序主席树
我的收获:主席树吼啊
#include <cstdio>#include <algorithm>#include <cstring>using namespace std;#define M 100005#define MX M*20#define lson tl[x],tl[y],l,mid#define rson tr[x],tr[y],mid+1,rint n,m,t,tot,cnt,tim;int root[M],a[M],num[M],head[M];int dfn[M],out[M],pre[M],mp[M];int tl[MX],tr[MX],sum[MX];struct edge{int to,nex;}e[M*2];inline void add(int u,int v){e[t].to=v,e[t].nex=head[u],head[u]=t++;}inline void node(int &x,int w){x=++cnt,sum[x]=sum[w]+1,tl[x]=tl[w],tr[x]=tr[w];}void insert(int x,int &y,int l,int r,int k){ node(y,x); if(l==r) return ; int mid=(l+r)>>1; if(k<=mid) insert(lson,k); else insert(rson,k);}int query(int x,int y,int l,int r,int k){ if(l==r) return l; int mid=(l+r)>>1,t=sum[tl[y]]-sum[tl[x]]; if(t>=k) return query(lson,k); return query(rson,k-t);}void dfs(int x,int fa){ dfn[x]=++tim;pre[dfn[x]]=x; for(int i=head[x];i!=-1;i=e[i].nex) if(e[i].to!=fa) dfs(e[i].to,x); out[x]=tim;}void work(){ for(int x,z,i=1;i<=m;i++){ scanf("%d%d",&x,&z); printf("%d\n",mp[query(root[dfn[x]-1],root[out[x]],1,tot,z)]); }}void lisan(){ sort(num+1,num+n+1); tot=unique(num+1,num+n+1)-(num+1); for(int i=1;i<=n;i++) a[i]=lower_bound(num+1,num+tot+1,a[i])-num,mp[a[i]]=i;}void init(){ scanf("%d",&n); memset(head,-1,sizeof(head)); for(int i=1;i<=n;i++) scanf("%d",&a[i]),num[i]=a[i]; lisan(); for(int x,y,i=1;i<n;i++) scanf("%d%d",&x,&y),add(x,y),add(y,x); dfs(1,0); scanf("%d",&m); for(int i=1;i<=n;i++) insert(root[i-1],root[i],1,tot,a[pre[i]]);}int main(){ init(); work();}
阅读全文
0 0
- 1803: Spoj1487 Query on a tree III
- BZOJ1803: Spoj1487 Query on a tree III
- [BZOJ1803] Spoj1487 Query on a tree III
- bzoj1803: Spoj1487 Query on a tree III
- bzoj1803: Spoj1487 Query on a tree III
- [主席树] BZOJ 1803 Spoj1487 Query on a tree III
- SPOJ1487. Query on a tree III 树链剖分+主席树
- bzoj 1803: Spoj1487 Query on a tree III(主席树)
- BZOJ 1803: Spoj1487 Query on a tree III 主席树题解
- [BZOJ1803]Spoj1487 Query on a tree III(dfs序+主席树)
- [BZOJ1803][Spoj1487]Query on a tree III(dfs序+主席树)
- Query on A Tree
- 3804 Query on a tree
- SPOJ Query on a tree
- Query on a tree II
- [SPOJ375]Query on a tree
- SPOJ375 Query on a tree
- SPOJ375 Query on a tree
- session传递参数
- HDU 1005 Number Sequence
- 2017 ACM-ICPC 亚洲区(乌鲁木齐赛区) 网络赛 H.Skiing(求有向无环图的最长路)
- 将字符串逆置(不改变里面单词的顺序)
- 2.11 动画系统基础
- 1803: Spoj1487 Query on a tree III
- Django缓存系统
- Kafka+Storm+HBase项目Demo(4)--Kafka使用
- 17年ICPC新疆网络赛G Query on a string(线段树 KMP)
- 2.12 动画系统进阶
- Java基础12:String、StringBuffer和StringBuilder的区别
- setTimeout与setInterval
- 计蒜客-2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛H题Skiing(拓扑序求DAG最长路)
- 特殊 矩阵 的 压缩