bzoj4756: [Usaco2017 Jan]Promotion Counting 线段树合并
2018-02-06 22:26
423 查看
bzoj4756: [Usaco2017 Jan]Promotion Counting
Description
The cows have once again tried to form a startup company, failing to remember from past experience that 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 NThe 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 ofsubordinates of cow ii with higher proficiency than cow i.
共n行,每行输出奶牛i的下属中有几个能力值比i大
Sample Input
5804289384
846930887
681692778
714636916
957747794
1
1
2
3
Sample Output
20
1
0
0
知识点:线段树合并
话说省选前学那么多新算法真是作死的节奏可是仍然还有一堆算法没学
不过反正这个算法挺水的
线段树合并的话其实就是暴力合并,吧一棵子树合并到另一颗子树上,有点类似于可并堆的操作,但是比它更暴力一点。当然是基于动态开点的线段树
严格地写一下算法流程吧
如果当前两个线段树节点其中一个为空,返回另一个节点编号,结束算法。
合并左右子树。
更新当前结点
返回当前结点编号
就这样吧,看一眼代码应该就懂了。
关于复杂度的话,正比于两颗线段树的公共节点个数。
不过可以证明,将n棵含有单个元素的树合并成一颗(n-1次),复杂度是O(nlogn)O(nlogn)的
因为单个元素和多个元素的公共部分不可能超过O(logn)O(logn)
例题分析
对于每颗子树建立其权值线段树,查询大于当前权值的数的个数。建立权值线段树的过程中从叶子节点网上合并线段树即可。
复杂度的话,每个节点初始插入权值线段树中lognlogn个节点。
由于是单个元素的合并,复杂度就是O(nlogn)O(nlogn)的
代码
/************************************************************** Problem: 4756 User: 2014lvzelong Language: C++ Result: Accepted Time:700 ms Memory:31560 kb ****************************************************************/ #include<iostream> #include<cstdlib> #include<cstdio> #include<cstring> #include<algorithm> #include<map> #include<cmath> using namespace std; const int N = 110000; const int T = 2e6 + 10; int read() { char ch = getchar(); int x = 0, f = 1; while(ch < '0' || ch > '9') {if(ch == '-') f = -1; ch = getchar();} while(ch >= '0' && ch <= '9') {x = x * 10 + ch - '0'; ch = getchar();} return x * f; } int to , pre , nxt , rt , ans , a , b , s[T], ls[T], rs[T], top, sz, mx; void add(int u, int v) {to[++top] = v; nxt[top] = pre[u]; pre[u] = top;} void Ins(int &p, int L, int R, int pos) { ++s[p = ++sz]; if(L == R) return; int mid = L + R >> 1; if(pos <= mid) Ins(ls[p], L, mid, pos); else Ins(rs[p], mid + 1, R, pos); } int Merge(int u, int v) { if(!u || !v) return u + v; ls[u] = Merge(ls[u], ls[v]); rs[u] = Merge(rs[u], rs[v]); s[u] = s[ls[u]] + s[rs[u]]; return u; } int Query(int p, int L, int R, int st, int ed) { if(st > ed) return 0; if(L == st && ed == R) return s[p]; int mid = L + R >> 1, ans = 0; if(L <= mid) ans += Query(ls[p], L, mid, st, min(ed, mid)); if(R > mid) ans += Query(rs[p], mid + 1, R, max(st, mid + 1), ed); return ans; } void dfs(int u) { Ins(rt[u], 1, mx, a[u]); for(int i = pre[u]; i; i = nxt[i]) dfs(to[i]), rt[u] = Merge(rt[u], rt[to[i]]); ans[u] = Query(rt[u], 1, mx, a[u] + 1, mx); } int main() { int n = read(); for(int i = 1;i <= n; ++i) a[i] = b[i] = read(); sort(b + 1, b + n + 1); mx = unique(b + 1, b + n + 1) - b - 1; for(int i = 1;i <= n; ++i) a[i] = lower_bound(b + 1, b + mx + 1, a[i]) - b; for(int i = 2;i <= n; ++i) add(read(), i); dfs(1); for(int i = 1;i <= n; ++i) printf("%d\n", ans[i]); return 0; }
相关文章推荐
- BZOJ 4756 [Usaco2017 Jan]Promotion Counting 线段树合并
- [BZOJ4756] [Usaco2017 Jan]Promotion Counting(线段树合并)
- BZOJ[4756][Usaco2017 Jan]Promotion Counting 线段树合并
- BZOJ 4756 [Usaco2017 Jan]Promotion Counting(线段树合并)
- bzoj 4756: [Usaco2017 Jan]Promotion Counting (线段树合并)
- 4756: [Usaco2017 Jan]Promotion Counting 线段树合并
- [BZOJ4756][Usaco2017 Jan]Promotion Counting(可持久化线段树)
- [Usaco2017 Jan]Promotion Counting 线段树合并模板/dfs序
- [BZOJ] 4756: [Usaco2017 Jan]Promotion Counting #线段树合并+权值线段树
- BZOJ 4756 线段树合并(线段树)
- bzoj 4756: [Usaco2017 Jan]Promotion Counting【dfs+树状数组】
- BZOJ_4756_[Usaco2017 Jan]Promotion Counting_树状数组
- 【BZOJ】4756: [Usaco2017 Jan]Promotion Counting
- BZOJ 4756 Promotion Counting(线段树合并 || dfs)
- bzoj 4756: [Usaco2017 Jan]Promotion Counting dsu on tree+树状数组
- 【bzoj4756】[Usaco2017 Jan]Promotion Counting
- bzoj 4756: [Usaco2017 Jan]Promotion Counting
- [BZOJ4756][Usaco2017 Jan]Promotion Counting 树状数组
- 【bzoj 4756】[Usaco2017 Jan] Promotion Counting
- BZOJ4756 - [Usaco2017 Jan]Promotion Counting