BZOJ 4756 主席树+dfs
来源:互联网 发布:Java全套视频 编辑:程序博客网 时间:2024/05/22 01:39
Promotion Counting
Time Limit: 10 Sec Memory Limit: 128 MB[Submit][Status][Discuss]
Description
The cows have once again tried to form a startup company, failing to remember from past experience t
hat cows make terrible managers!The cows, conveniently numbered 1…N1…N (1≤N≤100,000), organize t
he company as a tree, with cow 1 as the president (the root of the tree). Each cow except the presid
ent has a single manager (its "parent" in the tree). Each cow ii has a distinct proficiency rating,
p(i), which describes how good she is at her job. If cow ii is an ancestor (e.g., a manager of a man
ager of a manager) of cow jj, then we say jj is a subordinate of ii.
Unfortunately, the cows find that it is often the case that a manager has less proficiency than seve
ral of her subordinates, in which case the manager should consider promoting some of her subordinate
s. Your task is to help the cows figure out when this is happening. For each cow ii in the company,
please count the number of subordinates jj where p(j)>p(i).
n只奶牛构成了一个树形的公司,每个奶牛有一个能力值pi,1号奶牛为树根。
问对于每个奶牛来说,它的子树中有几个能力值比它大的。
问对于每个奶牛来说,它的子树中有几个能力值比它大的。
Input
The first line of input contains N
The next N lines of input contain the proficiency ratings p(1)…p(N)
for the cows. Each is a distinct integer in the range 1…1,000,000,000
The next N-1 lines describe the manager (parent) for cows 2…N
Recall that cow 1 has no manager, being the president.
n,表示有几只奶牛 n<=100000
接下来n行为1-n号奶牛的能力值pi
接下来n-1行为2-n号奶牛的经理(树中的父亲)
接下来n行为1-n号奶牛的能力值pi
接下来n-1行为2-n号奶牛的经理(树中的父亲)
Output
Please print N lines of output. The ith line of output should tell the number of
subordinates of cow ii with higher proficiency than cow i.
共n行,每行输出奶牛i的下属中有几个能力值比i大
Sample Input
5
804289384
846930887
681692778
714636916
957747794
1
1
2
3
804289384
846930887
681692778
714636916
957747794
1
1
2
3
Sample Output
2
0
1
0
0
0
1
0
0
题解:我们可以dfs出每个点序和每个点的子树的最大序,然后用主席树处理下,最后询问n次。
复杂度n*logn*logn
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<deque>#include<map>using namespace std;int a[200010],b[200010],lc[2000005],rc[2000005],root[200005],sum[2000005],cnt=0,head[200005],tot=0,c[100005],vis[100005],dt=1,dp[100005]; void add(int l,int r,int x,int &cur,int cur1){ cur=++cnt; lc[cur]=lc[cur1]; rc[cur]=rc[cur1]; sum[cur]=sum[cur1]+1; if(l==r)return; int mid=l+r>>1; if(x<=mid)add(l,mid,x,lc[cur],lc[cur1]); else add(mid+1,r,x,rc[cur],rc[cur1]); } int query(int l,int r,int cur,int cur1,int k){ if(l==r)return l; int mid=l+r>>1; int t=sum[lc[cur]]-sum[lc[cur1]]; if(t>=k)return query(l,mid,lc[cur],lc[cur1],k); else return query(mid+1,r,rc[cur],rc[cur1],k-t); } struct node{int to,nex;}edge[200005];void adds(int u,int v){edge[tot].to=v;edge[tot].nex=head[u];head[u]=tot++;}int dfs(int t){int i;for(i=head[t];~i;i=edge[i].nex){int v=edge[i].to;if(!vis[v]){vis[v]=++dt;dp[v]=dt;a[dt]=c[v];b[dt]=c[v];vis[t]=max(dfs(v),vis[t]);}}return vis[t];}int main(){ int i,j,n,m; memset(head,-1,sizeof(head)); scanf("%d",&n); for(i=1;i<=n;i++){ scanf("%d",&c[i]); } int ds; for(i=2;i<=n;i++){ scanf("%d",&ds); adds(ds,i); adds(i,ds); } dp[1]=1; vis[1]=1; a[dt]=c[1]; b[dt]=a[dt]; dfs(1); sort(b+1,b+1+n); for(i=1;i<=n;i++){ a[i]=lower_bound(b+1,b+1+n,a[i])-b; add(1,n,a[i],root[i],root[i-1]); } for(i=1;i<=n;i++){int l=1,r=vis[i]-dp[i]+1;while(l+1<r){//二分查找当前的数在其子树区间第几大int mid=l+r>>1;if(b[query(1,n,root[vis[i]],root[dp[i]-1],mid)]<=c[i]){l=mid;}else r=mid;} if(b[query(1,n,root[vis[i]],root[dp[i]-1],r)]==c[i])printf("%d\n",vis[i]-dp[i]+1-r); else printf("%d\n",vis[i]-dp[i]+1-l); } return 0; }
0 0
- BZOJ 4756 主席树+dfs
- BZOJ 4756 [Usaco2017 Jan]Promotion Counting dfs序+主席树
- bzoj 1803(主席树+dfs序)
- bzoj 1803(DFS序+主席树)
- bzoj 3772: 精神污染 (主席树+dfs序)
- bzoj 3653: 谈笑风生 (主席树+dfs序)
- [BZOJ 3772]精神污染 主席树+dfs序
- bzoj 3551 kruskal重构树dfs序上的主席树
- bzoj 3932(主席树)
- BZOJ 2588 主席树
- [BZOJ Contest-2017省队十连测推广赛2·T2][BZOJ4771][主席树][dfs序]七彩树
- [BZOJ 2588][SPOJ COT]Count On a Tree(DFS序主席树)
- [省选前题目整理][BZOJ 2588][SPOJ COT]Count On a Tree(DFS序主席树)
- spoj Query on a tree3/bzoj 1803(DFS序+主席树)
- 【BZOJ 2809】dispatching(主席树)
- 【主席树】 BZOJ 2653 middle
- 【主席树】BZOJ 2653 middle
- BZOJ 2653: middle|主席树
- nodejs系列之原生系列
- Neo4jOgm2.1的使用
- 日期的一些方法,日期转字符串,字符串转日期,前一个月和后一个月
- request.getParameter() 、 request.getInputStream()和request.getReader() 使用体会
- python UUID
- BZOJ 4756 主席树+dfs
- app高亮引导蒙层的实现
- 访问修饰符(C# 编程指南)
- js运行注册表
- Opengl超级宝典 Xcode环境配置
- Gerrit新用户基本操作小结
- easyui datebox 只显示年月
- 简单的状态防火墙
- http 状态码 小结