调半天原来是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的思想来利用子树的大量重复信息,树状数组配合查询。

 #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

【dsu || 线段树合并】bzoj4756: [Usaco2017 Jan]Promotion Counting的更多相关文章

  1. [BZOJ4756][Usaco2017 Jan]Promotion Counting 树状数组

    4756: [Usaco2017 Jan]Promotion Counting Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 305  Solved: ...

  2. BZOJ4756: [Usaco2017 Jan]Promotion Counting(线段树合并)

    题意 题目链接 Sol 线段树合并板子题 #include<bits/stdc++.h> using namespace std; const int MAXN = 400000, SS ...

  3. [BZOJ4756] [Usaco2017 Jan]Promotion Counting(线段树合并)

    传送门 此题很有意思,有多种解法 1.用天天爱跑步的方法,进入子树的时候ans-query,出去子树的时候ans+query,query可以用树状数组或线段树来搞 2.按dfs序建立主席树 3.线段树 ...

  4. bzoj4756 [Usaco2017 Jan]Promotion Counting

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=4756 [题解] dsu on tree,树状数组直接上 O(nlog^2n) # inclu ...

  5. 线段树合并 || 树状数组 || 离散化 || BZOJ 4756: [Usaco2017 Jan]Promotion Counting || Luogu P3605 [USACO17JAN]Promotion Counting晋升者计数

    题面:P3605 [USACO17JAN]Promotion Counting晋升者计数 题解:这是一道万能题,树状数组 || 主席树 || 线段树合并 || 莫队套分块 || 线段树 都可以写..记 ...

  6. BZOJ[Usaco2017 Jan]Promotion Counting——线段树合并

    题目描述 The cows have once again tried to form a startup company, failing to remember from past experie ...

  7. 2018.08.27 [Usaco2017 Jan]Promotion Counting(线段树合并)

    描述 The cows have once again tried to form a startup company, failing to remember from past experienc ...

  8. bzoj 4756 [Usaco2017 Jan]Promotion Counting——线段树合并

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4756 线段树合并裸题.那种返回 int 的与传引用的 merge 都能过.不知别的题是不是这 ...

  9. 【bzoj 4756】[Usaco2017 Jan] Promotion Counting

    Description The cows have once again tried to form a startup company, failing to remember from past ...

随机推荐

  1. SpringBoot2.0 基础案例(10):整合Mybatis框架,集成分页助手插件

    一.Mybatis框架 1.mybatis简介 MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获 ...

  2. JS高级学习历程-12

    冒充继承 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/T ...

  3. [软件工程基础]Alpha 软件测试报告

    PhyLab Alpha 测试报告 测试中发现的bug Alpha版本限制与问题 由于接手时数据库已经丢失,这一版本主要修复了大部分数据库,使得网站得以运行. 相比接手时网站的状况,有以下改进: 恢复 ...

  4. NET Core容器

    NET Core容器化之多容器应用部署@Docker-Compose   1.引言 紧接上篇.NET Core容器化@Docker,这一节我们先来介绍如何使用Nginx来完成.NET Core应用的反 ...

  5. response返回字符床

    response.getWriter().println() 本来一个html,JSP等WEB资源返回的就是一个String,只是有时候这个String是符合html格式的,而刚是浏览器接收的了,所以 ...

  6. 报错:Could not reserve enough space for object heap error

    windows命令行运行某个命令时出现: 解决办法: 设置开始->控制面板->系统和安全->系统->高级系统设置->环境变量->系统变量->新建: 变量名: ...

  7. 在使用seek()函数时,有时候会报错为 “io.UnsupportedOperation: can't do nonzero cur-relative seeks”,代码如下:

    __author__ = 'ZHHT' #!/usr/bin/env python # -*- coding:utf-8 -*- import os f = open("test1" ...

  8. 在使用添加按钮给table插入新的一行时遇见的问题总结及处理方法

    添加按钮的功能:点击添加按钮之后完成添加新的一行. 遇见的问题:当多次点击添加按钮生成新的多行之后,生成的每行内部按钮的保存按钮点击事件出现最晚添加的一行的行内保存点击事件执行一次,倒数第二次添加的行 ...

  9. JMeter进行压力测试

    一.jmeter的安装 1.从    http://jmeter.apache.org/download_jmeter.cgi 下载jmeter(图1正中间的apache-jmeter-2.13.tg ...

  10. Struts功能详解 ——ActionServlet

    ActionServlet类是Struts框架的内置核心控制器组件,它继承了javax.servlet.http.HttpServlet类.Struts的启动通常从 加载ActionServlet开始 ...