【dsu || 线段树合并】bzoj4756: [Usaco2017 Jan]Promotion Counting

调半天原来是dsu写不熟

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大

题目分析

做法一:dsu $O(n \log n)$

用dsu的思想来利用子树的大量重复信息,树状数组配合查询。

 1 #include<bits/stdc++.h>
 2 const int maxn = 100035;
 3 const int maxm = 100035;
 4 
 5 struct node
 6 {
 7     int fa,tot,son;
 8 }a[maxn];
 9 int n,p[maxn],f[maxn],cnt[maxn],ans[maxn];
10 int edgeTot,head[maxn],nxt[maxm],edges[maxm];
11 
12 int read()
13 {
14     char ch = getchar();
15     int num = 0, fl = 1;
16     for (; !isdigit(ch); ch=getchar())
17         if (ch=='-') fl = -1;
18     for (; isdigit(ch); ch=getchar())
19         num = (num<<1)+(num<<3)+ch-48;
20     return num*fl;
21 }
22 void addedge(int u, int v)
23 {
24     edges[++edgeTot] = v, nxt[edgeTot] = head[u], head[u] = edgeTot;
25 }
26 void update(int x, int c)
27 {
28     for (; x<=cnt[0]; x+=(x&-x)) f[x] += c;
29 }
30 int query(int x)
31 {
32     int ret = 0;
33     for (; x; x-=(x&-x)) ret += f[x];
34     return ret;
35 }
36 void dfs1(int x, int fa)
37 {
38     a[x].fa = fa, a[x].tot = 1, a[x].son = -1;
39     for (int i=head[x]; i!=-1; i=nxt[i])
40     {
41         int v = edges[i];
42         if (v==fa) continue;
43         dfs1(v, x), a[x].tot += a[v].tot;
44         if (a[x].son==-1||a[a[x].son].tot < a[v].tot) a[x].son = v;
45     }
46 }
47 void color(int x, int c, int del)
48 {
49     update(p[x], c);
50     for (int i=head[x]; i!=-1; i=nxt[i])
51         if (edges[i]!=a[x].fa&&edges[i]!=del)
52             color(edges[i], c, 0);
53 }
54 void dfs2(int x, bool fl)
55 {
56     for (int i=head[x]; i!=-1; i=nxt[i])
57         if (edges[i]!=a[x].fa&&edges[i]!=a[x].son)
58             dfs2(edges[i], 0);
59     if (a[x].son!=-1) dfs2(a[x].son, 1);
60     color(x, 1, a[x].son);
61     ans[x] = query(p[x]-1);
62     if (!fl) color(x, -1, 0);
63 }
64 int main()
65 {
66     memset(head, -1, sizeof head);
67     cnt[0] = n = read();
68     for (int i=1; i<=n; i++) p[i] = cnt[i] = read();
69     std::sort(cnt+1, cnt+n+1);
70     cnt[0] = std::unique(cnt+1, cnt+n+1)-cnt-1;
71     for (int i=1; i<=n; i++)
72         p[i] = cnt[0]+1-(std::lower_bound(cnt+1, cnt+cnt[0]+1, p[i])-cnt);
73     for (int i=1; i<n; i++) addedge(read(), i+1);
74     dfs1(1, 0), dfs2(1, 1);
75     for (int i=1; i<=n; i++) printf("%d\n",ans[i]);
76     return 0;
77 }

 

做法二:线段树合并 $O(n \log n)$

暂时没写,好像常数比dsu小。

upd:写了一下发现常数(本题)确实比dsu小。

线段树合并的思想不难理解,就是仿照堆的合并思路处理,原理则是基于线段树结构相同。

在动态开点的前提下,时间空间复杂度都是$O(n\log n)$的。

 1 #include<bits/stdc++.h>
 2 const int maxn = 100035;
 3 const int maxm = 200035;
 4 const int maxNode = 1000035;
 5 
 6 struct node
 7 {
 8     int l,r,val;
 9 }a[maxNode];
10 int n,tot;
11 int rt[maxn],p[maxn],cnt[maxn],ans[maxn];
12 int edgeTot,head[maxn],nxt[maxm],edges[maxm];
13 
14 int read()
15 {
16     char ch = getchar();
17     int num = 0, fl = 1;
18     for (; !isdigit(ch); ch=getchar())
19         if (ch=='-') fl = -1;
20     for (; isdigit(ch); ch=getchar())
21         num = (num<<1)+(num<<3)+ch-48;
22     return num*fl;
23 }
24 void addedge(int u, int v)
25 {
26     edges[++edgeTot] = v, nxt[edgeTot] = head[u], head[u] = edgeTot;
27 }
28 void write(int x){if (x/10) write(x/10);putchar('0'+x%10);}
29 void merge(int &u, int v)
30 {
31     if (!v) return;
32     if (!u) u = v;
33     else{
34         a[u].val += a[v].val;
35         merge(a[u].l, a[v].l);
36         merge(a[u].r, a[v].r);
37     }
38 }
39 int query(int rt, int l, int r, int c)
40 {
41     if (!rt) return 0;
42     if (r <= c) return a[rt].val;
43     int mid = (l+r)>>1, ret = query(a[rt].l, l, mid, c);
44     if (mid < c) ret += query(a[rt].r, mid+1, r, c);
45     return ret;
46 }
47 void update(int &rt, int l, int r, int c)
48 {
49     if (!rt) rt = ++tot;
50     ++a[rt].val;
51     if (l==r) return;
52     int mid = (l+r)>>1;
53     if (c <= mid) update(a[rt].l, l, mid, c);
54     else update(a[rt].r, mid+1, r, c);
55 }
56 void dfs(int x)
57 {
58     for (int i=head[x]; i!=-1; i=nxt[i])
59     {
60         int v = edges[i];
61         dfs(v), merge(rt[x], rt[v]);
62     }
63     ans[x] = query(rt[x], 1, cnt[0], p[x]);
64     update(rt[x], 1, cnt[0], p[x]);
65 }
66 int main()
67 {
68     memset(head, -1, sizeof head);
69     n = read();
70     for (int i=1; i<=n; i++) p[i] = cnt[i] = read();
71     for (int i=1; i<n; i++) addedge(read(), i+1);
72     std::sort(cnt+1, cnt+n+1);
73     cnt[0] = std::unique(cnt+1, cnt+n+1)-cnt-1;
74     for (int i=1; i<=n; i++)
75         p[i] = cnt[0]+1-(std::lower_bound(cnt+1, cnt+cnt[0]+1, p[i])-cnt);
76     dfs(1);
77     for (int i=1; i<=n; i++) write(ans[i]), putchar('\n');
78     return 0;
79 }

 

 

END

posted @ 2019-01-16 21:16  AntiQuality  阅读(184)  评论(0编辑  收藏  举报