题目描述

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
 
线段树合并练习题。
其实这道题直接用线段树查询子树区间就能做,但为了练一下线段树合并,所以写了一下线段树合并。
首先因为权值太大要离散化。
每个点动态开点建一棵权值线段树,每个点维护区间权值个数,从根开始dfs,回溯时合并每个点和子节点的线段树,都合并完之后单点查询即可。

#include<set>
#include<map>
#include<stack>
#include<queue>
#include<cmath>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int n,m;
int x;
int tot;
int cnt;
char ch[2];
int v[100010];
int h[100010];
int to[100010];
int ans[100010];
int ls[2000010];
int rs[2000010];
int sum[2000010];
int next[100010];
int head[100010];
int root[100010];
void add(int x,int y)
{
tot++;
next[tot]=head[x];
head[x]=tot;
to[tot]=y;
}
void insert(int &rt,int l,int r,int k)
{
if(!rt)
{
rt=++cnt;
}
if(l==r)
{
sum[rt]++;
return ;
}
int mid=(l+r)>>1;
if(k<=mid)
{
insert(ls[rt],l,mid,k);
}
else
{
insert(rs[rt],mid+1,r,k);
}
sum[rt]=sum[ls[rt]]+sum[rs[rt]];
}
void merge(int &x,int y)
{
if(!x||!y)
{
x=x+y;
return ;
}
sum[x]=sum[x]+sum[y];
merge(ls[x],ls[y]);
merge(rs[x],rs[y]);
}
int query(int rt,int l,int r,int k)
{
if(l==r)
{
return 0;
}
int mid=(l+r)>>1;
if(k<=mid)
{
return query(ls[rt],l,mid,k)+sum[rs[rt]];
}
else
{
return query(rs[rt],mid+1,r,k);
}
}
void dfs(int x)
{
for(int i=head[x];i;i=next[i])
{
dfs(to[i]);
merge(root[x],root[to[i]]);
}
ans[x]=query(root[x],1,m,v[x]);
}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",&v[i]);
h[i]=v[i];
}
sort(h+1,h+n+1);
m=unique(h+1,h+n+1)-h-1;
for(int i=2;i<=n;i++)
{
scanf("%d",&x);
add(x,i);
}
for(int i=1;i<=n;i++)
{
v[i]=lower_bound(h+1,h+1+m,v[i])-h;
insert(root[i],1,m,v[i]);
}
dfs(1);
for(int i=1;i<=n;i++)
{
printf("%d\n",ans[i]);
}
}

BZOJ[Usaco2017 Jan]Promotion Counting——线段树合并的更多相关文章

  1. bzoj 4756 [Usaco2017 Jan]Promotion Counting——线段树合并

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4756 线段树合并裸题.那种返回 int 的与传引用的 merge 都能过.不知别的题是不是这 ...

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

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

  3. BZOJ4756:[USACO]Promotion Counting(线段树合并)

    Description n只奶牛构成了一个树形的公司,每个奶牛有一个能力值pi,1号奶牛为树根. 问对于每个奶牛来说,它的子树中有几个能力值比它大的. Input n,表示有几只奶牛 n<=10 ...

  4. bzoj 4756 Promotion Counting —— 线段树合并

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4756 合并子树的权值线段树: merge 返回 int 或者是 void 都可以. 代码如下 ...

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

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

  6. [BZOJ4756][Usaco2017 Jan]Promotion Counting 树状数组

    4756: [Usaco2017 Jan]Promotion Counting Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 305  Solved: ...

  7. [BZOJ 1483] [HNOI2009] 梦幻布丁 (线段树合并)

    [BZOJ 1483] [HNOI2009] 梦幻布丁 (线段树合并) 题面 N个布丁摆成一行,进行M次操作.每次将某个颜色的布丁全部变成另一种颜色的,然后再询问当前一共有多少段颜色.例如颜色分别为1 ...

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

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

  9. 2018.08.27 [Usaco2017 Jan]Promotion Counting(线段树合并)

    描述 The cows have once again tried to form a startup company, failing to remember from past experienc ...

随机推荐

  1. 【Codeforces 3D】Least Cost Bracket Sequence

    Codeforces 3 D 题意:有一个括号序列,其中一些位置是问号,把第\(i\)个问号改成(需要\(a_i\)的代价,把它改成)需要\(b_i\)的代价. 问使得这个括号序列成立所需要的最小代价 ...

  2. 『转』统计一个日志文件里,单词出现频率的shell脚本

    原文地址:http://blog.csdn.net/taiyang1987912/article/details/39995175 #查找文本中n个出现频率最高的单词 #!/bin/bash coun ...

  3. VS2017上执行VS2013项目错误MSB802之解决方案

    进行想把我编写的数字图像处理软件MagicHouse更新到最新的VS2017开发环境下,原来的开发环境是VS2013.但是用VS2017打开项目并编译时,系统报错误MSB802,如下图所示. 其实Vi ...

  4. Spring Boot 之 Profile 使用

    Spring Boot 之 Profile 使用 一个应用为了在不同的环境下工作,常常会有不同的配置,代码逻辑处理.Spring Boot 对此提供了简便的支持. 关键词: @Profile.spri ...

  5. Ionic2 调用Custom Cordova Plugin方法

    APP升级到Ionic2之后,如何调用自己写的pulgin,一直测试不成功,现记录这一经过. plugin目前可以分为3类,A类是ionic-native自带的,可以直接导入Typescript类,直 ...

  6. [Oracle]数据库的Control File 取Dump后的样例

    [Oracle]数据库的Control File 取Dump后的样例: 片段截取-------------------------------(size = 40, compat size = 40, ...

  7. HNOI2018简要题解

    HNOI2018简要题解 D1T1 寻宝游戏 题意 某大学每年都会有一次 Mystery Hunt 的活动,玩家需要根据设置的线索解谜,找到宝藏的位置,前一年获胜的队伍可以获得这一年出题的机会. 作为 ...

  8. [开源 .NET 跨平台 Crawler 数据采集 爬虫框架: DotnetSpider] [二] 基本使用

    [DotnetSpider 系列目录] 一.初衷与架构设计 二.基本使用 三.配置式爬虫 四.JSON数据解析与配置系统 五.如何做全站采集 使用环境 Visual Studio 2017 .NET ...

  9. 2018年高教社杯全国大学生数学建模竞赛B题解题思路

    题目 先贴下B题的题目吧 问题B    智能RGV的动态调度策略 图1是一个智能加工系统的示意图,由8台计算机数控机床(Computer Number Controller,CNC).1辆轨道式自动引 ...

  10. Mysqldump备份说明及数据库备份脚本分享-运维笔记

    MySQLdump是MySQL自带的导出数据工具,即mysql数据库中备份工具,用于将MySQL服务器中的数据库以标准的sql语言的方式导出,并保存到文件中.Mysqldump是一个客户端逻辑备份的工 ...