Promotion Counting晋升者计数

题目描述
The cows have once again tried to form a startup company, failing to remember from past experience that cows make terrible managers!

The cows, conveniently numbered 1 \ldots N1…N (1 \leq N \leq 100,0001≤N≤100,000), organize the company as a tree, with cow 1 as the president (the root of the tree). Each cow except the president has a single manager (its “parent” in the tree). Each cow ii has a distinct proficiency rating, p(i)p(i), which describes how good she is at her job. If cow ii is an ancestor (e.g., a manager of a manager 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 several of her subordinates, in which case the manager should consider promoting some of her subordinates. 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)p(j)>p(i).

奶牛们又一次试图创建一家创业公司,还是没有从过去的经验中吸取教训–牛是可怕的管理者!

为了方便,把奶牛从 1 \cdots N(1 \leq N \leq 100, 000)1⋯N(1≤N≤100,000) 编号,把公司组织成一棵树,1 号奶牛作为总裁(这棵树的根节点)。除了总裁以外的每头奶牛都有一个单独的上司(它在树上的 “双亲结点”)。所有的第 ii 头牛都有一个不同的能力指数 p(i)p(i),描述了她对其工作的擅长程度。如果奶牛 ii 是奶牛 jj 的祖先节点(例如,上司的上司的上司),那么我们我们把奶牛 jj 叫做 ii 的下属。

不幸地是,奶牛们发现经常发生一个上司比她的一些下属能力低的情况,在这种情况下,上司应当考虑晋升她的一些下属。你的任务是帮助奶牛弄清楚这是什么时候发生的。简而言之,对于公司的中的每一头奶牛 ii,请计算其下属 jj 的数量满足 p(j) > p(i)p(j)>p(i)。

输入格式
The first line of input contains NN.

The next NN lines of input contain the proficiency ratings p(1) \ldots p(N)p(1)…p(N) for the cows. Each is a distinct integer in the range 1 \ldots 1,000,000,0001…1,000,000,000.

The next N-1N−1 lines describe the manager (parent) for cows 2 \ldots N2…N. Recall that cow 1 has no manager, being the president.

输入的第一行包括一个整数 NN。

接下来的 NN 行包括奶牛们的能力指数 p(1) \cdots p(N)p(1)⋯p(N). 保证所有数互不相同,在区间 1 \cdots 10^91⋯10
9
之间。

接下来的 N-1N−1 行描述了奶牛 2 \cdots N2⋯N 的上司(双亲节点)的编号。再次提醒,1 号奶牛作为总裁,没有上司。

输出格式
Please print NN lines of output. The iith line of output should tell the number of subordinates of cow ii with higher proficiency than cow ii.

输出包括 NN 行。输出的第 ii 行应当给出有多少奶牛 ii 的下属比奶牛 ii 能力高。

输入输出样例
输入 #1复制

5
804289384
846930887
681692778
714636916
957747794
1
1
2
3
输出 #1复制
2
0
1
0
0


线段树动态开点+线段树合并。

也可以直接主席树+dfs序。

就是建立一颗权值线段树,然后从根节点向下合并即可。

注意空间开20倍。


动态开点:


#pragma GCC optimize(2)
#include<bits/stdc++.h>
//#define int long long
using namespace std;
const int N=1e5+10;
int n,m,t[N<<5],lc[N<<5],rc[N<<5],cnt,a[N],b[N],res[N],rt[N];
int head[N],to[N],nex[N],tot;
inline void add(int a,int b){
	to[++tot]=b; nex[tot]=head[a]; head[a]=tot;
}
inline int id(int x){
	return lower_bound(b+1,b+1+m,x)-b;
}
void change(int &p,int l,int r,int x){
	if(r<l)	return ;
	if(!p)	p=++cnt;
	if(l==r) return (void)(t[p]++);
	int mid=l+r>>1;
	if(x<=mid)	change(lc[p],l,mid,x);
	else	change(rc[p],mid+1,r,x);
	t[p]=t[lc[p]]+t[rc[p]];
}
int ask(int p,int l,int r,int x){
	if(!p)	return 0;
	if(l>=x)	return t[p];
	int mid=l+r>>1;
	if(mid<x)	return ask(rc[p],mid+1,r,x);
	else	return ask(lc[p],l,mid,x)+ask(rc[p],mid+1,r,x);
}
int merge(int x,int y){
	if(!x)	return y;
	if(!y)	return x;
	lc[x]=merge(lc[x],lc[y]);
	rc[x]=merge(rc[x],rc[y]);
	t[x]=t[lc[x]]+t[rc[x]];
	return x;
}
void dfs(int x){
	for(int i=head[x];i;i=nex[i]){
		dfs(to[i]);	rt[x]=merge(rt[x],rt[to[i]]);
	}
	res[x]=ask(rt[x],1,n,a[x]+1);
}
signed main(){
	cin>>n;
	for(int i=1;i<=n;i++)	scanf("%d",&a[i]),b[i]=a[i];
	sort(b+1,b+1+n); m=unique(b+1,b+1+n)-b-1;
	for(int i=1;i<=n;i++)	a[i]=id(a[i]);
	for(int i=2;i<=n;i++){
		int x;	scanf("%d",&x);	add(x,i);
	}
	for(int i=1;i<=n;i++)	change(rt[i],1,n,a[i]);
	dfs(1);
	for(int i=1;i<=n;i++)	printf("%d\n",res[i]);
	return 0;
}

主席树


#pragma GCC optimize(2)
#include<bits/stdc++.h>
//#define int long long
using namespace std;
const int N=1e5+10;
int n,m,rt[N],a[N],b[N],st[N],ed[N],dfn[N],cnt,idx;
int head[N],to[N],nex[N],tot;
int lc[N*20],rc[N*20],t[N*20];
inline void add(int a,int b){
	to[++tot]=b; nex[tot]=head[a]; head[a]=tot;
}
void dfs(int x){
	st[x]=++idx; dfn[idx]=x;
	for(int i=head[x];i;i=nex[i])	dfs(to[i]);
	ed[x]=idx;
}
void change(int l,int r,int &x,int y,int k){
	x=++cnt; t[x]=t[y]; lc[x]=lc[y]; rc[x]=rc[y]; t[x]++;
	if(l==r)	return ; int mid=l+r>>1;
	if(k<=mid)	change(l,mid,lc[x],lc[y],k);
	else	change(mid+1,r,rc[x],rc[y],k);
}
int ask(int l,int r,int x,int y,int k){
	if(r<k)	return 0;
	if(l>=k)	return t[y]-t[x];
	int mid=l+r>>1;
	if(k>mid)	return ask(mid+1,r,rc[x],rc[y],k);
	else	return ask(l,mid,lc[x],lc[y],k)+ask(mid+1,r,rc[x],rc[y],k);
}
signed main(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++)	scanf("%d",&a[i]),b[i]=a[i];
	sort(b+1,b+1+n); m=unique(b+1,b+1+n)-b-1;
	for(int i=1;i<=n;i++)	a[i]=lower_bound(b+1,b+1+m,a[i])-b;
	for(int i=2;i<=n;i++){
		int x;	scanf("%d",&x);	add(x,i);
	}
	dfs(1);
	for(int i=1;i<=n;i++)	change(1,m,rt[i],rt[i-1],a[dfn[i]]);
	for(int i=1;i<=n;i++){
		printf("%d\n",ask(1,m,rt[st[i]-1],rt[ed[i]],a[i]+1));
	}
	return 0;
}
全部评论

相关推荐

珩珺:那些经历都太大太空了,实习的情况不了解,大创项目连名字、背景、目的及意义都没体现出来;地摊经济更是看完连卖的什么产品都不知道,项目成果直接写营收多少都更直观真实一点;后面那个校文体部的更是工作内容是组织活动整理流程,成果变成了当志愿者,而且你们学校本科学生会大一入学就直接当部长吗,志愿里面还提到了疫情防控,全面解封是22年12月的事情,可能时间上也有冲突。可能你花了钱人家就用AI给你随便写了点内容改了一下,没什么体现个性化的点
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务