4756: [Usaco2017 Jan]Promotion Counting 线段树合并

4756: [Usaco2017 Jan]Promotion Counting

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 247  Solved: 171
[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号奶牛的经理(树中的父亲)

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

Sample Output

2
0
1
0
0

昨天晚上选的签到题

早上起来就1A辣 蛤蛤蛤 好开心


这个题做法挺多的

而且每种都不错

可以按dfs序建主席树,做子树查询

可以用树状树组,每个节点的答案为回来时-进来时

可以用线段树合并,递归子树,向上合并


#include<cmath>
#include<ctime>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<iomanip>
#include<vector>
#include<string>
#include<bitset>
#include<queue>
#include<map>
#include<set>
using namespace std;

inline int read()
{
	int x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch<='9'&&ch>='0'){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
	return x*f;
}
void print(int x)
{if(x<0)putchar('-'),x=-x;if(x>=10)print(x/10);putchar(x%10+'0');}

const int N=100100;

int n,maxn;

struct P{int x,pos;friend bool operator <(const P &x,const P &y){return x.x<y.x;}}p[N];

int ref[N],V[N],ans[N];

int ecnt,last[N];
struct EDGE{int to,nt;}e[N];
inline void add(int u,int v)
{e[++ecnt]=(EDGE){v,last[u]};last[u]=ecnt;}

struct president_tree{int ls,rs,w;}tr[N<<5];
int root[N];

int cnt;

void insert(int &k,int x,int l,int r,int val)
{
	k=++cnt;tr[k].w=tr[x].w+1;
	if(l==r)return ;
	int mid=(l+r)>>1;
	tr[k].ls=tr[x].ls;tr[k].rs=tr[x].rs;
	val<=mid?insert(tr[k].ls,tr[x].ls,l,mid,val):insert(tr[k].rs,tr[x].rs,mid+1,r,val);
}

int merge(int x,int y)
{
	if(!x)return y;if(!y)return x;
	tr[x].ls=merge(tr[x].ls,tr[y].ls);
	tr[x].rs=merge(tr[x].rs,tr[y].rs);
	tr[x].w=tr[x].w+tr[y].w;
	return x;
}

int query(int k,int l,int r,int val)
{
	if(l>=val)return tr[k].w;
	int mid=(l+r)>>1;
	if(val>mid)return query(tr[k].rs,mid+1,r,val);
	return query(tr[k].ls,l,mid,val)+query(tr[k].rs,mid+1,r,val);
}

void dfs(int u)
{
	for(int i=last[u];i;i=e[i].nt)
	{dfs(e[i].to);root[u]=merge(root[u],root[e[i].to]);}
	insert(root[u],root[u],1,maxn,V[u]);
	ans[u]=query(root[u],1,maxn,V[u]+1);
}

int main()
{
	n=read();
	register int i,u;
	for(i=1;i<=n;++i)p[i].x=read(),p[i].pos=i;
	sort(p+1,p+1+n);
	maxn=1;ref[maxn]=p[1].x;V[p[1].pos]=1;
	for(i=1;i<=n;++i){if(p[i].x^ref[maxn])maxn++,ref[maxn]=p[i].x;V[p[i].pos]=maxn;}//for(i=1;i<=n;++i)cout<<V[i]<<" ";cout<<endl;
	for(i=2;i<=n;++i){u=read();add(u,i);}
	dfs(1);
	for(i=1;i<=n;++i)print(ans[i]),puts("");
	return 0;
}
/*
5
804289384
846930887
681692778
714636916
957747794
1
1
2
3

2
0
1
0
0
*/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值