题解 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 ...
随机推荐
- ZBrush中独特功能Projection Master
Projection Master可以理解为投影大师,它是ZBrush®的一个独特功能,该功能可以让用户使用所有的2D和2.5D笔刷工具在3D模型上进行雕刻.纹理绘制和其他的操作. 简单来说,Proj ...
- 转 c#中 base64字符串与普通字符串互转
https://blog.csdn.net/hwt0101/article/details/79758912 转成 Base64 形式的 System.String: string a = &q ...
- vue v-if的使用
代码部分 <el-row> <el-col :span="20"> <template v-for="(node,i) of hierarc ...
- HDU-4221 Greedy? 贪心 从元素的相对位置开始考虑
题目链接:https://cn.vjudge.net/problem/HDU-4221 题意 给n个活动,每个活动需要一段时间C来完成,并且有一个截止时间D 当完成时间t大于截止时间完成时,会扣除t- ...
- parted 4T磁盘
parted /dev/vdg mklabel gpt mkpart primary ext4 0% 100%
- 紫书 例题 11-5 UVa 10048 (Floyd求最大权值最小的路径)
这道题是Floyd的变形 改成d[i][j] = min(d[i][j], max(d[i][k], d[k][j]))就好了. #include<cstdio> #include< ...
- 2015 Multi-University Training Contest 3 hdu 5316 Magician
Magician Time Limit: 18000/9000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total S ...
- 洛谷 P1990 覆盖墙壁
P1990 覆盖墙壁 题目描述 你有一个长为N宽为2的墙壁,给你两种砖头:一个长2宽1,另一个是L型覆盖3个单元的砖头.如下图: 0 0 0 00 砖头可以旋转,两种砖头可以无限制提供.你的任务是计算 ...
- Spring MVC 的 研发之路 (二)
二.web.xml的简单配置介绍1 1.启动Web项目时,容器回去读web.xml配置文件里的两个节点<context-param>和<listener> 2.接着容器会创建一 ...
- 2、Python列表和元组
2.Python序列 序列是一种数据存储方式,类似于C语言的数组.简单的说序列是一块用来存储多个值的连续内存空间,同一个序列的元素通常是相关的. Python中常用的序列结构有列表.元组.字典.字符串 ...