2018.08.27 [Usaco2017 Jan]Promotion Counting(线段树合并)
描述
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号奶牛为树根。 问对于每个奶牛来说,它的子树中有几个能力值比它大的。
输入
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号奶牛的经理(树中的父亲)
输出
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大
样例输入
5
804289384
846930887
681692778
714636916
957747794
1
1
2
3
样例输出
2
0
1
0
0
康复训练ing。。。
这题本蒟蒻用线段树合并水过去了。。。
暂时没想到其它的做法毕竟实力太弱了
代码;
#include<bits/stdc++.h>
#define N 100005
using namespace std;
inline int read(){
int ans=0;
char ch=getchar();
while(!isdigit(ch))ch=getchar();
while(isdigit(ch))ans=(ans<<3)+(ans<<1)+(ch^48),ch=getchar();
return ans;
}
inline void write(int x){
if(x>9)write(x/10);
putchar((x%10)^48);
}
int ans[N],a[N],b[N],n,sig,first[N],tot=0,cnt=0,rt[N],siz[N*20],son[N*20][2];
struct edge{int v,next;}e[N<<1];
inline void add(int u,int v){e[++cnt].v=v,e[cnt].next=first[u],first[u]=cnt;}
inline void modify(int&p,int l,int r,int k){
if(!p)p=++tot;
if(l==r){siz[p]=1;return;}
int mid=l+r>>1;
if(k<=mid)modify(son[p][0],l,mid,k);
else modify(son[p][1],mid+1,r,k);
siz[p]=siz[son[p][0]]+siz[son[p][1]];
}
inline int merge(int x,int y,int l,int r){
if(!x||!y)return x+y;
if(l==r){siz[x]+=siz[y];return x;}
int mid=l+r>>1;
son[x][0]=merge(son[x][0],son[y][0],l,mid);
son[x][1]=merge(son[x][1],son[y][1],mid+1,r);
siz[x]=siz[son[x][0]]+siz[son[x][1]];
return x;
}
inline int query(int p,int l,int r,int v){
if(l==r)return siz[p];
int mid=l+r>>1;
if(v<=mid)return query(son[p][0],l,mid,v)+siz[son[p][1]];
return query(son[p][1],mid+1,r,v);
}
inline void dfs(int p){
for(int i=first[p];i;i=e[i].next){
int v=e[i].v;
dfs(v);
rt[p]=merge(rt[p],rt[v],1,sig);
}
ans[p]=query(rt[p],1,sig,a[p])-1;
}
int main(){
n=read();
for(int i=1;i<=n;++i)a[i]=b[i]=read();
sort(b+1,b+n+1),sig=unique(b+1,b+n+1)-b-1;
for(int i=1;i<=n;++i)modify(rt[i],1,sig,(a[i]=(lower_bound(b+1,b+sig+1,a[i])-b)));
for(int i=2;i<=n;++i)add(read(),i);
dfs(1);
for(int i=1;i<=n;++i)write(ans[i]),puts("");
return 0;
}
2018.08.27 [Usaco2017 Jan]Promotion Counting(线段树合并)的更多相关文章
- BZOJ4756: [Usaco2017 Jan]Promotion Counting(线段树合并)
题意 题目链接 Sol 线段树合并板子题 #include<bits/stdc++.h> using namespace std; const int MAXN = 400000, SS ...
- BZOJ[Usaco2017 Jan]Promotion Counting——线段树合并
题目描述 The cows have once again tried to form a startup company, failing to remember from past experie ...
- bzoj 4756 [Usaco2017 Jan]Promotion Counting——线段树合并
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4756 线段树合并裸题.那种返回 int 的与传引用的 merge 都能过.不知别的题是不是这 ...
- BZOJ4756:[USACO]Promotion Counting(线段树合并)
Description n只奶牛构成了一个树形的公司,每个奶牛有一个能力值pi,1号奶牛为树根. 问对于每个奶牛来说,它的子树中有几个能力值比它大的. Input n,表示有几只奶牛 n<=10 ...
- bzoj 4756 Promotion Counting —— 线段树合并
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4756 合并子树的权值线段树: merge 返回 int 或者是 void 都可以. 代码如下 ...
- 【bzoj4756】[Usaco2017 Jan]Promotion Counting 离散化+树状数组
原文地址:http://www.cnblogs.com/GXZlegend/p/6832263.html 题目描述 The cows have once again tried to form a s ...
- [BZOJ4756][Usaco2017 Jan]Promotion Counting 树状数组
4756: [Usaco2017 Jan]Promotion Counting Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 305 Solved: ...
- 线段树合并 || 树状数组 || 离散化 || BZOJ 4756: [Usaco2017 Jan]Promotion Counting || Luogu P3605 [USACO17JAN]Promotion Counting晋升者计数
题面:P3605 [USACO17JAN]Promotion Counting晋升者计数 题解:这是一道万能题,树状数组 || 主席树 || 线段树合并 || 莫队套分块 || 线段树 都可以写..记 ...
- 【dsu || 线段树合并】bzoj4756: [Usaco2017 Jan]Promotion Counting
调半天原来是dsu写不熟 Description The cows have once again tried to form a startup company, failing to rememb ...
随机推荐
- 普通web项目转maven项目
先要有pom.xml文件 1.首先你要确定你的开发工具是否已经安装上maven: 2.安装配置好后将你的项目导入到开发工具上: 3.右键点击java项目,选择maven选项,在选择Enable Dep ...
- 3 Python 函数介绍
1.函数的基本概念 定义: 函数是指将一组语句的集合通过一个名字(函数名)封装起来,要想执行这个函数,只需调用其函数名即可 特性: 减少重复代码 使程序变的可扩展 使程序变得易维护 语法定义 def ...
- python os模块常用命令
python编程时,经常和文件.目录打交道,这是就离不了os模块.os模块包含普遍的操作系统功能,与具体的平台无关.以下列举常用的命令 1. os.name()——判断现在正在实用的平台,Window ...
- GitLab CI 之 Java HelloWrold
问题描述 测试人员想在gitalb上跑 JUnit项目,也就是java代码. 听到这个之后,我当时都懵了,我他妈gitlab的runner是为运行.net core 安装的呀.后来一想,是我错了,我用 ...
- ArcGIS案例学习1_2
ArcGIS案例学习1_2 联系方式:谢老师,135_4855_4328, xiexiaokui#qq.com 时间:第一天下午 案例1:矢量提取,栅格提取和坐标系投影变换 目的:认识数据类型 教程: ...
- plot绘图
plot绘图 坐标系图(折线图) 折线图用于显示随时间或有序类别的变化趋势 plt.plot(x,y,format_string,**kwargs) y:Y轴数据,列表或数组,必选 x:X轴数据,列表 ...
- spring中的BeanFactory和FactoryBean的区别与联系
首先,这俩都是个接口… 实现 BeanFactory 接口的类表明此类是一个工厂,作用就是配置.新建.管理 各种Bean. 而 实现 FactoryBean 的类表明此类也是一个Bean,类型为工厂B ...
- jQuery源码解读二(apply和call)
一.apply方法和call方法的用法: apply方法: 语法:apply(thisObj,[,argArray]) 定义:应用某一对象的一个方法,用另一个对象替换当前对象. 说明:如果argArr ...
- java swing:文本框添加滚动条
有几点要注意: 1.默认的滚动条,仅在输入的文本超过文本框时才会显示..没有超过文本框是不会显示的: 2.设置矩形大小,是在滚动条上设置,而不是在文本框上设置: 示例代码如下: public clas ...
- Ubuntu中解决机箱前置耳机没声音
Ubuntu中解决机箱前置耳机没声音 安装pavucontrol软件: sudo apt-get install pavucontrol 然后直接运行pavucontrol打开软件: 将输出设备设置为 ...