您的位置:首页 > 其它

BZOJ 4756 [Usaco2017 Jan]Promotion Counting dfs序+主席树

2017-10-02 23:01 357 查看

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

HINT

传送门
有一个比较简单的想法,就是维护权值的线段树,然后根据dfs序求出即可。
问题就是不同子树,
当然有很多做法,比如线段树合并。
但是本菜只会主席树……那就刚一发主席树。

一开始一直查不到错……后来发现root的标号竟然打错了。。zz。。
其实主席树这东西,我也不知道叫chairtree,还是chairmantree,还是都可。。
好吧总算A了一题……

#include<bits/stdc++.h>
using namespace std;
const int
N=100005;
int n,Ecnt,Tcnt,cnt;
int a
,ANS
,root
;

struct node{int x,id;}p
;
bool cmp(node a,node b){return a.x<b.x;}
struct ChairTree{int l,r,num;}ct[N*18];
struct Tree{int tid,MAX;}tree
;
struct Edge{int next,to;}E[N<<1];int head
;

void add(int u,int v){
E[++Ecnt].next=head[u];
E[Ecnt].to=v;
head[u]=Ecnt;
}
void insert(int L,int R,int &x,int g){
ct[++Tcnt]=ct[x];
x=Tcnt;
ct[x].num++;
if (L==R) return;
int mid=(L+R)>>1;
if (g<=mid) insert(L,mid,ct[x].l,g);
else insert(mid+1,R,ct[x].r,g);
}
int query(int L,int R,int x,int y,int gl,int gr){
if (L>=gl && R<=gr) return ct[y].num-ct[x].num;
int mid=(L+R)>>1,t=0;
if (gl<=mid) t+=query(L,mid,ct[x].l,ct[y].l,gl,gr);
if (gr>mid) t+=query(mid+1,R,ct[x].r,ct[y].r,gl,gr);
return t;
}
void ins(int x){
root[tree[x].tid]=root[tree[x].tid-1];
insert(1,n,root[tree[x].tid],a[x]);
}
void dfs(int u,int pre){
tree[u].tid=++cnt;
ins(u);
tree[u].MAX=tree[u].tid;
for (int i=head[u];i;i=E[i].next){
int v=E[i].to;
if (v==pre) continue;
dfs(v,u);
tree[u].MAX=max(tree[u].MAX,tree[v].MAX);
}
ANS[u]=query(1,n,root[tree[u].tid-1],root[tree[u].MAX],a[u]+1,n);
}
int main(){
scanf("%d",&n);
for (int i=1;i<=n;i++)
scanf("%d",&p[i]),p[i].id=i;
sort(p+1,p+1+n,cmp);
for (int i=1;i<=n;i++)
if (p[i].x==p[i-1].x) a[p[i].id]=a[p[i-1].id];
else a[p[i].id]=i;

int x;Ecnt=0;
for (int i=2;i<=n;i++)
scanf("%d",&x),add(x,i),add(i,x);

root[0]=Tcnt=cnt=0;
dfs(1,0);
for (int i=1;i<=n;i++) printf("%d\n",ANS[i]);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: