题解 P3605 【[USACO17JAN]Promotion Counting晋升者计数】
这道题开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晋升者计数】的更多相关文章
- 线段树合并 || 树状数组 || 离散化 || BZOJ 4756: [Usaco2017 Jan]Promotion Counting || Luogu P3605 [USACO17JAN]Promotion Counting晋升者计数
题面:P3605 [USACO17JAN]Promotion Counting晋升者计数 题解:这是一道万能题,树状数组 || 主席树 || 线段树合并 || 莫队套分块 || 线段树 都可以写..记 ...
- 树状数组 P3605 [USACO17JAN]Promotion Counting晋升者计数
P3605 [USACO17JAN]Promotion Counting晋升者计数 题目描述 奶牛们又一次试图创建一家创业公司,还是没有从过去的经验中吸取教训--牛是可怕的管理者! 为了方便,把奶牛从 ...
- 洛谷P3605 [USACO17JAN] Promotion Counting 晋升者计数 [线段树合并]
题目传送门 Promotion Counting 题目描述 The cows have once again tried to form a startup company, failing to r ...
- 洛谷 P3605 [USACO17JAN]Promotion Counting晋升者计数
题目描述 The cows have once again tried to form a startup company, failing to remember from past experie ...
- luogu P3605 [USACO17JAN]Promotion Counting晋升者计数
题目链接 luogu 思路 可以说是线段树合并的练手题目吧 也没啥说的,就是dfs,然后合并... 看代码吧 错误 和写主席树错的差不多 都是变量写错.... 代码 #include <bits ...
- P3605 [USACO17JAN]Promotion Counting晋升者计数
思路 线段树合并的板子.. 和子节点合并之后在值域线段树上查询即可 代码 #include <cstdio> #include <algorithm> #include < ...
- Luogu3605 [USACO17JAN]Promotion Counting晋升者计数
Luogu3605 [USACO17JAN]Promotion Counting晋升者计数 给一棵 \(n\) 个点的树,点 \(i\) 有一个权值 \(a_i\) .对于每个 \(i\) ,求 \( ...
- [USACO17JAN]Promotion Counting晋升者计数
题目描述 奶牛们又一次试图创建一家创业公司,还是没有从过去的经验中吸取教训--牛是可怕的管理者! 为了方便,把奶牛从 1 \cdots N(1 \leq N \leq 100, 000)1⋯N(1≤N ...
- 题解 P3605 [USACO17JAN]Promotion Counting P
分块\(yyds\) ----关于线段树合并的题我用分块过掉这件事 题目传送门 先说正解 正解当然是线段树合并等一类做法了 至于解析...出门右转题解区第一篇 (就是他让我看不懂,然后用分块打的\(Q ...
随机推荐
- JavaScript语法高亮库highlight.js使用
highlight.js是一款基于JavaScript的语法高亮库,目前支持125种编程语言,有63种可供选择的样式,而且能够做到语言自动识别,和目前主流的JS框架都能兼容,可以混合使用. 这款高亮库 ...
- Jquery 设置class 和 div CSS
Jquery 设置class 和 div CSS 1 Jquery 根据标签内容获取标签div,从而修改该div CLASS //追加 $('label:contains("labelcon ...
- 【原创】MemCached中的参数解释
优化MemCached的主要目的为增加命中率和提高内存使用率,在优化的时候可以根据以下参数综合考虑分析: 首先是进程项: pid Memcached进程ID uptime Memcached运行时间, ...
- python下载网页转化成pdf
最近在学习一个网站补充一下cg基础.但是前几天网站突然访问不了了,同学推荐了waybackmachine这个网站,它定期的对网络上的页面进行缓存,但是好多图片刷不出来,很憋屈.于是网站恢复访问后决定把 ...
- ActiveMQ学习笔记(15)----Message Dispatch高级特性(一)
1. Message Cursors 1.1 概述 ActiveMQ发送持久化消息的典型的厝里方式是:当消息的消费者准备就绪时,消息发送系统把存储的消息按批次发送给消费者,在发送完一个批次的消息后,指 ...
- vue mint-ui swipe 不显示或显示空白
vue mint-ui swipe 不显示或显示空白? 解决需要在mt-swipe上层元素设置高度 <div> <div> <mt-header title=" ...
- BZOJ 3166 [HEOI2013]Alo (可持久化01Trie+链表)
题目大意:给你一个长度为$n$的序列,让你找出一段子序列,求其中的 次大值 异或 序列里一个数 能得到的最大值 先对序列建出可持久化$Trie$ 按元素的值从小到大遍历,设当前元素的位置是i,找出它左 ...
- 题解 UVA12206 【Stammering Aliens】
终于A了这道题啊(坑啊) 教练说:这道题不能用map吧,复杂度不一个O(nlogn)吗 于是我就一直想不出来,然后看题解代码,一看就是map... 所以我就在想,那复杂度是不是也不是O(nlogn)呢 ...
- android中的AlertDialog具体概述
android的AlertDialog具体解释 AlertDialog的构造方法所有是Protected的.所以不能直接通过new一个AlertDialog来创建出一个AlertDialog. 要创建 ...
- ASP.NET Web开发技术的深入总结
[IT168技术]在国内.Net开发这个环境里, 中小型公司.或者大公司但主营业务不是软件开发里面的软件小团队.针对.Net开发者的要求都是十项全能型的全才, 能做的了从前台页面展现到最后数据存储的全 ...