这道题开10倍左右一直MLE+RE,然后尝试着开了20倍就A了。。。窒息


对于这道题目,我们考虑使用线段树合并来做。

所谓线段树合并,就是把结构相同的线段树上的节点的信息合在一起,合并的方式比较类似左偏树什么的。


我们对于每个节点用权值线段树查询大于它的子节点数量,然后把当前节点并到它的父亲上面去。

对于此类型的题目我们通常使用动态开点的线段树(不然炸的没边)。

时间复杂度应该是$O(nlogn)$


AC代码如下:

455ms 32824kb

 #include <bits/stdc++.h>

 using namespace std;

 namespace StandardIO {

     template<typename T> inline void read (T &x) {
x=;T f=;char c=getchar();
for(; c<''||c>''; c=getchar()) if(c=='-') f=-;
for(; c>=''&&c<=''; c=getchar()) x=x*+c-'';
x*=f;
}
template<typename T>inline void write (T x) {
if (x<) putchar('-'),x*=-;
if (x>=) write(x/);
putchar(x%+'');
} } using namespace StandardIO; namespace Solve { const int N=; int n;
int cnt;
struct node {
int id,v;
inline bool operator < (const node &x) const {
return v<x.v;
}
}p[N];
vector<int>graph[N];
int tree_node;
int val[N],tree[(int)(N*)],ls[(int)(N*)],rs[(int)(N*)],root[N],ans[N]; void build (int l,int r,int v,int &root) {
if (!root) root=++tree_node;
tree[root]++;
if (l==r) return;
int mid=(l+r)>>;
if (v<=mid) build(l,mid,v,ls[root]);
else build(mid+,r,v,rs[root]);
}
int query (int l,int r,int v,int root) {
if (!root) return ;
if (v<=l) return tree[root];
int mid=(l+r)>>;
if (v<=mid) return query(l,mid,v,ls[root])+query(mid+,r,v,rs[root]);
return query(mid+,r,v,rs[root]);
}
int merge (int x,int y) {
if (!x||!y) return x+y;
int root=++tree_node;
tree[root]=tree[x]+tree[y];
ls[root]=merge(ls[x],ls[y]);
rs[root]=merge(rs[x],rs[y]);
return root;
}
void dfs (int now) {
for (register int i=; i<graph[now].size(); ++i) {
int to=graph[now][i];
dfs(to);
root[now]=merge(root[now],root[to]);
}
ans[now]=query(,cnt,val[now]+,root[now]);
build(,cnt,val[now],root[now]);
} inline void solve () {
read(n);
for (register int i=; i<=n; ++i) {
read(p[i].v),p[i].id=i;
}
sort(p+,p+n+);
for (register int i=; i<=n; ++i) {
if (p[i].v!=p[i-].v) val[p[i].id]=++cnt;
else val[p[i].id]=cnt;
}
for (register int i=; i<=n; ++i) {
int x;read(x);
graph[x].push_back(i);
}
dfs();
for (register int i=; i<=n; ++i) {
write(ans[i]),putchar('\n');
}
}
} using namespace Solve; int main () {
solve();
}

题解 P3605 【[USACO17JAN]Promotion Counting晋升者计数】的更多相关文章

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

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

  2. 树状数组 P3605 [USACO17JAN]Promotion Counting晋升者计数

    P3605 [USACO17JAN]Promotion Counting晋升者计数 题目描述 奶牛们又一次试图创建一家创业公司,还是没有从过去的经验中吸取教训--牛是可怕的管理者! 为了方便,把奶牛从 ...

  3. 洛谷P3605 [USACO17JAN] Promotion Counting 晋升者计数 [线段树合并]

    题目传送门 Promotion Counting 题目描述 The cows have once again tried to form a startup company, failing to r ...

  4. 洛谷 P3605 [USACO17JAN]Promotion Counting晋升者计数

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

  5. luogu P3605 [USACO17JAN]Promotion Counting晋升者计数

    题目链接 luogu 思路 可以说是线段树合并的练手题目吧 也没啥说的,就是dfs,然后合并... 看代码吧 错误 和写主席树错的差不多 都是变量写错.... 代码 #include <bits ...

  6. P3605 [USACO17JAN]Promotion Counting晋升者计数

    思路 线段树合并的板子.. 和子节点合并之后在值域线段树上查询即可 代码 #include <cstdio> #include <algorithm> #include < ...

  7. Luogu3605 [USACO17JAN]Promotion Counting晋升者计数

    Luogu3605 [USACO17JAN]Promotion Counting晋升者计数 给一棵 \(n\) 个点的树,点 \(i\) 有一个权值 \(a_i\) .对于每个 \(i\) ,求 \( ...

  8. [USACO17JAN]Promotion Counting晋升者计数

    题目描述 奶牛们又一次试图创建一家创业公司,还是没有从过去的经验中吸取教训--牛是可怕的管理者! 为了方便,把奶牛从 1 \cdots N(1 \leq N \leq 100, 000)1⋯N(1≤N ...

  9. 题解 P3605 [USACO17JAN]Promotion Counting P

    分块\(yyds\) ----关于线段树合并的题我用分块过掉这件事 题目传送门 先说正解 正解当然是线段树合并等一类做法了 至于解析...出门右转题解区第一篇 (就是他让我看不懂,然后用分块打的\(Q ...

随机推荐

  1. 50个极好的bootstrap框架

    转自:http://sudasuta.com/bootstrap-admin-templates.html  https://www.cnblogs.com/sanhao/p/9184323.html ...

  2. “.”开头,以"}"结尾,中间是任意字符的正则

    "."开头,以"}"结尾,中间是任意字符的正则 /^\..+\{$/

  3. 计算机组成原理--64位CPU装载32位操作系统,它的寻址能力还是4GB吗?

    借由这个问题,今天我们就把 32 位 CPU.64 位 CPU.32 位操作系统.64 位操作系统之间的区别与联系彻底搞清楚.对于这个问题,博主也是一知半解了好长时间啊~ 基本概念 32位的CPU与6 ...

  4. 快速沃尔什变换(FWT)笔记

    开头Orz hy,Orz yrx 部分转载自hy的博客 快速沃尔什变换,可以快速计算两个多项式的位运算卷积(即and,or和xor) 问题模型如下: 给出两个多项式$A(x)$,$B(x)$,求$C( ...

  5. 使用VUE开发微信小程序

    使用 mpvue 开发小程序,你将在小程序技术体系的基础上获取到这样一些能力: 彻底的组件化开发能力:提高代码复用性完整的 Vue.js 开发体验方便的 Vuex 数据管理方案:方便构建复杂应用快捷的 ...

  6. 《鸟哥的Linux私房菜》读书笔记--第0章 计算机概论 硬件部分

    一个下午看了不少硬件层面的知识,看得太多太快容易忘记.于是在博客上写下读书笔记. 有关硬件 个人计算机架构&接口设备 主板芯片组为“南北桥”的统称,南北桥用于控制所有组件之间的通信. 北桥连接 ...

  7. cogs 141. [USACO Jan08] 奶牛的选举

    141. [USACO Jan08] 奶牛的选举 ★   输入文件:elect.in   输出文件:elect.out   简单对比时间限制:1 s   内存限制:16 MB 在推翻了Farmer J ...

  8. Android中处理Touch Icon的方案

    苹果的Touch Icon相对我们都比较熟悉,是苹果为了支持网络应用(或者说网页)添加到桌面需要的图标,有了这些Touch Icon的网页链接更加和Native应用更相像了.由于苹果设备IPod,IP ...

  9. ios面试题1

    iOS面试题   1.写一个NSString类的实现 + (id)initWithCString:(c*****t char *)nullTerminatedCString encoding:(NSS ...

  10. Hdoj 1176 免费馅饼 【动态规划】

    免费馅饼 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submi ...