题目描述

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. ubuntu 系统查看opencv 的版本

    有很多的时候 ,我们想知道自己的电脑里面安装的opencv版本是多少 在终端中运行下面的命令. pkg-config --modversion opencv 为什么要知道自己电脑的opencv 版本, ...

  2. linux内存源码分析 - 页表的初始化

    本文为原创,转载请注明:http://www.cnblogs.com/tolimit/ 本文章中系统我们假设为x86下的32位系统,暂且不分析64位系统的页表结构. linux分页 linux下采用四 ...

  3. SVM的简单介绍

    ng的MI-003中12 ——SVM 一.svm目标函数的由来 视频先将LR的损失函数: 在上图中,先将y等于0 和y等于1的情况集合到一起成为一个损失函数,然后分别讨论当y等于1的时候损失函数的结果 ...

  4. VS诊断工具打开失败

    使用管理员模式打开cmd,输入以下命令~ C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis -iC:\Windows\Micros ...

  5. LOJ564 613的天网 构造

    题目传送门 题意:给出一个$N \times N \times N$的方块,你可以在每一个$1 \times 1 \times 1的方块上放上一个摄像头,摄像头的监视范围为6个方向的无限远距离.问最少 ...

  6. xshell替代工具finalShell

    主要特性:1.多平台支持Windows,Mac OS X,Linux2.多标签,批量服务器管理.3.支持登录Ssh和Windows远程桌面.4.漂亮的平滑字体显示,内置100多个配色方案.5.终端,s ...

  7. Ionic下的Jpush消息推送与内容显示

    本文测试Jpush将消息推送给手机端,手机端点击通知栏,即可看到具体的推送内容. 1.极光推送消息设置 设置附加字段: 点击发送,手机端收到消息通知. 2.手机接收到的消息通知 点击之后进入具体的页面 ...

  8. SQLServer 中发布与订阅

    在对数据库做迁移的时候,会有很多方法,用存储过程,job,也可以用开源工具kettle,那么今天这些天变接触到了一种新的方法,就是SqlServer中自带的发布与订阅. 首先说明一下数据复制的流程.如 ...

  9. C-数据结构-typedef的用法

    .typedef的用法 # include <stdio.h> typedef int zhang; //为数据类为int从新取名为zhang 等价于int typedef struct ...

  10. Peer Programming Project: 4 Elevators Scheduler 附加题 157 165

    1.改进电梯调度的interface 设计, 让它更好地反映现实, 更能让学生练习算法, 更好地实现信息隐藏和信息共享. 每个电梯增加目标楼层数组,这样可以更好地进行任务的分配,在我们的电梯中,这个数 ...