4756: [Usaco2017 Jan]Promotion Counting

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 305  Solved: 217
[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

HINT

 

Source

Platinum鸣谢Acty提供译文

先离散化。

dfs遍历,用树状数组维护,比当前元素小的元素个数,答案显然为子树size-遍历完子树后比当前元素小的元素个数+进入子树时比当前元素小的元素个数。

 #include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
int read() {
int x=,f=;char ch=getchar();
while(!isdigit(ch)) {ch=getchar();}
while(isdigit(ch)) {x=x*+ch-'';ch=getchar();}
return x;
}
int n;
int p[];
int a[];
int sum[];
int lowbit(int x){return x&(-x);}
void update(int x,int val) {for(int i=x;i<=n;i+=lowbit(i)) sum[i]+=val;}
int query(int x) {
int ans=;
for(int i=x;i>;i-=lowbit(i)) ans+=sum[i];
return ans;
}
struct data {
int to,next;
}e[];
int head[],cnt,size[],ans[];
void add(int u,int v) {e[cnt].next=head[u];e[cnt].to=v;head[u]=cnt++;}
void dfs(int now) {
size[now]=;
int tmp=query(a[now]);
update(a[now],);
for(int i=head[now];i>=;i=e[i].next) {
int to=e[i].to;
dfs(to);
size[now]+=size[to];
}
tmp=query(a[now])-tmp;
ans[now]=size[now]-tmp;
}
int main() {
memset(head,-,sizeof(head));
n=read();
for(int i=;i<=n;i++) p[i]=a[i]=read();
sort(p+,p+n+);
for(int i=;i<=n;i++) a[i]=lower_bound(p+,p+n+,a[i])-p;
for(int i=;i<=n;i++) {
int v=i,u=read();
add(u,v);
}
dfs();
for(int i=;i<=n;i++) printf("%d\n",ans[i]);
}

[BZOJ4756][Usaco2017 Jan]Promotion Counting 树状数组的更多相关文章

  1. BZOJ_4756_[Usaco2017 Jan]Promotion Counting_树状数组

    BZOJ_4756_[Usaco2017 Jan]Promotion Counting_树状数组 Description n只奶牛构成了一个树形的公司,每个奶牛有一个能力值pi,1号奶牛为树根. 问对 ...

  2. 【dsu || 线段树合并】bzoj4756: [Usaco2017 Jan]Promotion Counting

    调半天原来是dsu写不熟 Description The cows have once again tried to form a startup company, failing to rememb ...

  3. [BZOJ4756] [Usaco2017 Jan]Promotion Counting(线段树合并)

    传送门 此题很有意思,有多种解法 1.用天天爱跑步的方法,进入子树的时候ans-query,出去子树的时候ans+query,query可以用树状数组或线段树来搞 2.按dfs序建立主席树 3.线段树 ...

  4. bzoj4756 [Usaco2017 Jan]Promotion Counting

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=4756 [题解] dsu on tree,树状数组直接上 O(nlog^2n) # inclu ...

  5. BZOJ4756: [Usaco2017 Jan]Promotion Counting(线段树合并)

    题意 题目链接 Sol 线段树合并板子题 #include<bits/stdc++.h> using namespace std; const int MAXN = 400000, SS ...

  6. 【bzoj4756】[Usaco2017 Jan]Promotion Counting 离散化+树状数组

    原文地址:http://www.cnblogs.com/GXZlegend/p/6832263.html 题目描述 The cows have once again tried to form a s ...

  7. 线段树合并 || 树状数组 || 离散化 || BZOJ 4756: [Usaco2017 Jan]Promotion Counting || Luogu P3605 [USACO17JAN]Promotion Counting晋升者计数

    题面:P3605 [USACO17JAN]Promotion Counting晋升者计数 题解:这是一道万能题,树状数组 || 主席树 || 线段树合并 || 莫队套分块 || 线段树 都可以写..记 ...

  8. bzoj 4756: [Usaco2017 Jan]Promotion Counting【dfs+树状数组】

    思路还是挺好玩的 首先简单粗暴的想法是dfs然后用离散化权值树状数组维护,但是这样有个问题就是这个全局的权值树状数组里并不一定都是当前点子树里的 第一反应是改树状数组,但是显然不太现实,但是可以这样想 ...

  9. HDU 4358 Boring counting 树状数组+思路

    研究了整整一天orz……直接上官方题解神思路 #include <cstdio> #include <cstring> #include <cstdlib> #in ...

随机推荐

  1. JsBridge "Uncaught TypeError: Cannot call method 'callHandler' of undefined", source

    h5和原生结合开发app越来越流行.其实就是webview 的js调用native的方法.也就是需要搭建一个桥.这样的桥早就有人搭建好了,那就是jsbridge. git地址: https://git ...

  2. python和matlab

    一.python简介 python是一种面向对象的解释型计算机程序设计语言.python是纯粹的自由软件,源代码和解释器CPython遵循GPL协议.Python语法简介清晰,特色之一是强制用空白符作 ...

  3. 2018"百度之星"程序设计大赛 - 资格赛 - 题集

    1001 $ 1 \leq m \leq 10 $ 像是状压的复杂度. 于是我们(用二进制)枚举留下的问题集合 然后把这个集合和问卷们的答案集合 $ & $ 一下 就可以只留下被选中的问题的答 ...

  4. Avril Lavigne : Everybody Hurts (Ver3)

    http://www.guitartabsexplorer.com/ http://www.guitartabsexplorer.com/lavigne-avril-Tabs/everybody-hu ...

  5. Jenkins拾遗--第三篇(用户权限管理)

    采访过很多实用Jenkins的同学,发现Jenkins的安全是一个很薄弱的地方.很多公司用作生产部署的Jenkins安全管理都不是很规范,就更别提测试用的Jenkins了. 其实Jenkins是一个很 ...

  6. 剑指Offer - 九度1513 - 二进制中1的个数

    剑指Offer - 九度1513 - 二进制中1的个数2013-11-29 23:35 题目描述: 输入一个整数,输出该数二进制表示中1的个数.其中负数用补码表示. 输入: 输入可能包含多个测试样例. ...

  7. C语言中强制类型转换总结

    C语言中强制类型转换总结  ● 字符型变量的值实质上是一个8位的整数值,因此取值范围一般是-128-127,char型变量也可以加修饰符unsigned,则unsigned char 型变量的取值范围 ...

  8. TensorFlow dataset API 使用

    # TensorFlow dataset API 使用 由于本人感兴趣的是自然语言处理,所以下面有关dataset API 的使用偏向于变长数据的处理. 1. 从迭代器中引入数据 import num ...

  9. linux 出错 “INFO: task xxxxxx: 634 blocked for more than 120 seconds.”的3种解决方案(转)

    linux 出错 “INFO: task xxxxxx: 634 blocked for more than 120 seconds.”的3种解决方案 1 问题描述 服务器内存满了,ssh登录失败 , ...

  10. URAL 1942 Attack at the Orbit

    B - Attack at the Orbit Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & % ...