【bzoj4756】[Usaco2017 Jan]Promotion Counting

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

[ Submit][ Status][ Discuss]




线段树合并

主席树也可

代码:
#include<cstdio>
#include<vector>
#include<queue>
#include<ctime>
#include<algorithm>
#include<cstdlib>
#include<stack>
#include<cstring>
#include<cmath>
using namespace std;

typedef long long LL;

const int INF = 2147483647;
const int maxn = 100100;
const int maxseg = 20 * maxn;

vector<int> e[maxn];
int n;
int a[maxn],ans[maxn],rt[maxn];
int siz[maxseg],lc[maxseg],rc[maxseg],tot;
int ha[maxn],totha,data[maxn];

inline LL getint()
{
	LL ret = 0,f = 1;
	char c = getchar();
	while (c < '0' || c > '9')
	{
		if (c == '-') f = -1;
		c = getchar();
	}
	while (c >= '0' && c <= '9')
		ret = ret * 10 + c - '0',c = getchar();
	return ret * f;
}

inline void maintain(int o)
{
	siz[o] = siz[lc[o]] + siz[rc[o]];
}

inline int merge(int lo,int ro)
{
	if (!lo) return ro;
	if (!ro) return lo;
	int now = lo;
	lc[now] = merge(lc[lo],lc[ro]);
	rc[now] = merge(rc[lo],rc[ro]);
	siz[now] = siz[lo] + siz[ro];
	return now;
}

inline void insert(int &o,int l,int r,int pos)
{
	if (!o) o = ++tot;
	if (l == r) {siz[o]++; return;}
	int mid = l + r >> 1;
	if (pos <= mid) insert(lc[o],l,mid,pos);
	else insert(rc[o],mid + 1,r,pos);
	maintain(o);
}

inline int query(int o,int l,int r,int x)
{
	if (l == r) return 0;
	int mid = l + r >> 1;
	if (x <= mid) return query(lc[o],l,mid,x) + siz[rc[o]];
	else return query(rc[o],mid + 1,r,x);
}

inline void dfs(int u,int fa)
{
	insert(rt[u],1,totha,a[u]);
	for (int i = 0; i < e[u].size(); i++)
	{
		int v = e[u][i];
		if (v == fa) continue;
		dfs(v,u);
		rt[u] = merge(rt[u],rt[v]);
	}
	ans[u] = query(rt[u],1,totha,a[u]);
}

int main()
{
	n = getint();
	for (int i = 1; i <= n; i++)
		data[i] = a[i] = getint();
	for (int i = 2; i <= n; i++)
	{
		int father = getint();
		e[father].push_back(i);
		e[i].push_back(father);
	}
	sort(data + 1,data + n + 1);
	for (int i = 1; i <= n; i++)
		if (data[i] != data[i + 1])
			ha[++totha] = data[i];
	for (int i = 1; i <= n; i++) a[i] = lower_bound(ha + 1,ha + totha + 1,a[i]) - ha;
	dfs(1,0);
	for (int i = 1; i <= n; i++)
		printf("%d\n",ans[i]);
	return 0;
}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,这是一道经典的单调栈问题。题目描述如下: 有 $n$ 个湖,第 $i$ 个湖有一个高度 $h_i$。现在要在这些湖之间挖一些沟渠,使得相邻的湖之间的高度差不超过 $d$。请问最少需要挖多少个沟渠。 这是一道单调栈的典型应用题。我们可以从左到右遍历湖的高度,同时使用一个单调栈来维护之前所有湖的高度。具体来说,我们维护一个单调递增的栈,栈中存储的是湖的下标。假设当前遍历到第 $i$ 个湖,我们需要在之前的湖中找到一个高度最接近 $h_i$ 且高度不超过 $h_i-d$ 的湖,然后从这个湖到第 $i$ 个湖之间挖一条沟渠。具体的实现可以参考下面的代码: ```c++ #include <cstdio> #include <stack> using namespace std; const int N = 100010; int n, d; int h[N]; stack<int> stk; int main() { scanf("%d%d", &n, &d); for (int i = 1; i <= n; i++) scanf("%d", &h[i]); int ans = 0; for (int i = 1; i <= n; i++) { while (!stk.empty() && h[stk.top()] <= h[i] - d) stk.pop(); if (!stk.empty()) ans++; stk.push(i); } printf("%d\n", ans); return 0; } ``` 这里的关键在于,当我们遍历到第 $i$ 个湖时,所有比 $h_i-d$ 小的湖都可以被舍弃,因为它们不可能成为第 $i$ 个湖的前驱。因此,我们可以不断地从栈顶弹出比 $h_i-d$ 小的湖,直到栈顶的湖高度大于 $h_i-d$,然后将 $i$ 入栈。这样,栈中存储的就是当前 $h_i$ 左边所有高度不超过 $h_i-d$ 的湖,栈顶元素就是最靠近 $h_i$ 且高度不超过 $h_i-d$ 的湖。如果栈不为空,说明找到了一个前驱湖,答案加一。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值