调半天原来是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号奶牛的经理(树中的父亲)
接下来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的思想来利用子树的大量重复信息,树状数组配合查询。
#include<bits/stdc++.h>
const int maxn = ;
const int maxm = ; struct node
{
int fa,tot,son;
}a[maxn];
int n,p[maxn],f[maxn],cnt[maxn],ans[maxn];
int edgeTot,head[maxn],nxt[maxm],edges[maxm]; int read()
{
char ch = getchar();
int num = , fl = ;
for (; !isdigit(ch); ch=getchar())
if (ch=='-') fl = -;
for (; isdigit(ch); ch=getchar())
num = (num<<)+(num<<)+ch-;
return num*fl;
}
void addedge(int u, int v)
{
edges[++edgeTot] = v, nxt[edgeTot] = head[u], head[u] = edgeTot;
}
void update(int x, int c)
{
for (; x<=cnt[]; x+=(x&-x)) f[x] += c;
}
int query(int x)
{
int ret = ;
for (; x; x-=(x&-x)) ret += f[x];
return ret;
}
void dfs1(int x, int fa)
{
a[x].fa = fa, a[x].tot = , a[x].son = -;
for (int i=head[x]; i!=-; i=nxt[i])
{
int v = edges[i];
if (v==fa) continue;
dfs1(v, x), a[x].tot += a[v].tot;
if (a[x].son==-||a[a[x].son].tot < a[v].tot) a[x].son = v;
}
}
void color(int x, int c, int del)
{
update(p[x], c);
for (int i=head[x]; i!=-; i=nxt[i])
if (edges[i]!=a[x].fa&&edges[i]!=del)
color(edges[i], c, );
}
void dfs2(int x, bool fl)
{
for (int i=head[x]; i!=-; i=nxt[i])
if (edges[i]!=a[x].fa&&edges[i]!=a[x].son)
dfs2(edges[i], );
if (a[x].son!=-) dfs2(a[x].son, );
color(x, , a[x].son);
ans[x] = query(p[x]-);
if (!fl) color(x, -, );
}
int main()
{
memset(head, -, sizeof head);
cnt[] = n = read();
for (int i=; i<=n; i++) p[i] = cnt[i] = read();
std::sort(cnt+, cnt+n+);
cnt[] = std::unique(cnt+, cnt+n+)-cnt-;
for (int i=; i<=n; i++)
p[i] = cnt[]+-(std::lower_bound(cnt+, cnt+cnt[]+, p[i])-cnt);
for (int i=; i<n; i++) addedge(read(), i+);
dfs1(, ), dfs2(, );
for (int i=; i<=n; i++) printf("%d\n",ans[i]);
return ;
}
做法二:线段树合并 $O(n \log n)$
暂时没写,好像常数比dsu小。
upd:写了一下发现常数(本题)确实比dsu小。
线段树合并的思想不难理解,就是仿照堆的合并思路处理,原理则是基于线段树结构相同。
在动态开点的前提下,时间空间复杂度都是$O(n\log n)$的。
#include<bits/stdc++.h>
const int maxn = ;
const int maxm = ;
const int maxNode = ; struct node
{
int l,r,val;
}a[maxNode];
int n,tot;
int rt[maxn],p[maxn],cnt[maxn],ans[maxn];
int edgeTot,head[maxn],nxt[maxm],edges[maxm]; int read()
{
char ch = getchar();
int num = , fl = ;
for (; !isdigit(ch); ch=getchar())
if (ch=='-') fl = -;
for (; isdigit(ch); ch=getchar())
num = (num<<)+(num<<)+ch-;
return num*fl;
}
void addedge(int u, int v)
{
edges[++edgeTot] = v, nxt[edgeTot] = head[u], head[u] = edgeTot;
}
void write(int x){if (x/) write(x/);putchar(''+x%);}
void merge(int &u, int v)
{
if (!v) return;
if (!u) u = v;
else{
a[u].val += a[v].val;
merge(a[u].l, a[v].l);
merge(a[u].r, a[v].r);
}
}
int query(int rt, int l, int r, int c)
{
if (!rt) return ;
if (r <= c) return a[rt].val;
int mid = (l+r)>>, ret = query(a[rt].l, l, mid, c);
if (mid < c) ret += query(a[rt].r, mid+, r, c);
return ret;
}
void update(int &rt, int l, int r, int c)
{
if (!rt) rt = ++tot;
++a[rt].val;
if (l==r) return;
int mid = (l+r)>>;
if (c <= mid) update(a[rt].l, l, mid, c);
else update(a[rt].r, mid+, r, c);
}
void dfs(int x)
{
for (int i=head[x]; i!=-; i=nxt[i])
{
int v = edges[i];
dfs(v), merge(rt[x], rt[v]);
}
ans[x] = query(rt[x], , cnt[], p[x]);
update(rt[x], , cnt[], p[x]);
}
int main()
{
memset(head, -, sizeof head);
n = read();
for (int i=; i<=n; i++) p[i] = cnt[i] = read();
for (int i=; i<n; i++) addedge(read(), i+);
std::sort(cnt+, cnt+n+);
cnt[] = std::unique(cnt+, cnt+n+)-cnt-;
for (int i=; i<=n; i++)
p[i] = cnt[]+-(std::lower_bound(cnt+, cnt+cnt[]+, p[i])-cnt);
dfs();
for (int i=; i<=n; i++) write(ans[i]), putchar('\n');
return ;
}
END