您的位置:首页 > 其它

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

2017-08-28 07:46 483 查看

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 that cows make terrible managers!The cows, conveniently numbered 1…N1…N
(1≤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), 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).
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
;

int ref
,V
,ans
;

int ecnt,last
;
struct EDGE{int to,nt;}e
;
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
;

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
*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: