Codeforces 538 F. A Heap of Heaps
[$>Codeforces \space 538 F. A Heap of Heaps
题目大意 :给出 \(n\) 个点,编号为 \(1 - n\) ,每个点有点权,将这些点构建成 \(k\) 叉树的形式 \((k \in [1, n - 1])\) 。
对于编号为 \(i\) 的点,它的儿子区间是 \([\ k(i-1)+2, \ \min(ki + 1, n)\ ]\) 如果说一个点违反堆性质,当且仅当它的点权小于它父亲的点权,对于所有 \((k \in [1, n - 1])\) 叉树,求出按照给定规则构建后违反堆性质的点的数量
\(1≤ n ≤ 2 \times 10^5\) , \(-10^9 \leq a_i \leq 10^9\)
解题思路 :
观察发现,按照题目规则构建的 \(k\) 叉树,有儿子的点最多只有 \(\frac{n}{k}\) 个
不妨暴力枚举 \(k\) ,对于每一个 \(k\) 枚举树中有儿子的点,统计其对应的儿子区间里权值比他小的点的数量
考虑本质上是一个二维数点,那么离散化 \(+\) 主席树就可以在 \(O(logn)\) 的时间内完成单次查询
考虑枚举部分的复杂度是一个类似于 \(\frac{n}{1} + \frac{n}{2} +..+ \frac{n}{n-1}\) 的调和级数状物,复杂度是 \(O(nlogn)\)
所以算法的总复杂度是 \(O(nlog^2n)\)
/*program by mangoyang*/
#include<bits/stdc++.h>
#define inf (0x7f7f7f7f)
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define Min(a, b) ((a) < (b) ? (a) : (b))
typedef long long ll;
using namespace std;
template <class T>
inline void read(T &x){
int f = 0, ch = 0; x = 0;
for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = 1;
for(; isdigit(ch); ch = getchar()) x = x * 10 + ch - 48;
if(f) x = -x;
}
#define N (1000005)
int s[N], a[N], rt[N], n;
struct SegmentTree{
int rc[N*25], lc[N*25], sz[N*25], cnt;
inline void ins(int &u, int pr, int l, int r, int pos){
u = ++cnt, sz[u] = sz[pr] + 1;
lc[u] = lc[pr], rc[u] = rc[pr];
if(l == r) return; int mid = l + r >> 1;
if(pos <= mid) ins(lc[u], lc[pr], l, mid, pos);
else ins(rc[u], rc[pr], mid + 1, r, pos);
}
inline int query(int x, int y, int l, int r, int L, int R){
if(l >= L && r <= R) return sz[y] - sz[x];
int mid = l + r >> 1, res = 0;
if(L <= mid) res += query(lc[x], lc[y], l, mid, L, R);
if(mid < R) res += query(rc[x], rc[y], mid + 1, r, L, R);
return res;
}
}van;
inline int solve(int k){
int ans = 0;
for(int i = 1; i <= n; i++){
int l = k * (i - 1) + 2, r = Min(n, k * i + 1);
if(l > n) return ans;
if(a[i] == 1) continue;
ans += van.query(rt[l-1], rt[r], 1, n, 1, a[i] - 1);
}
return ans;
}
int main(){
read(n);
for(int i = 1; i <= n; i++)
read(a[i]), s[i] = a[i];
sort(s + 1, s + n + 1);
int sz = unique(s + 1, s + n + 1) - s - 1;
for(int i = 1; i <= n; i++){
a[i] = lower_bound(s + 1, s + sz + 1, a[i]) - s;
van.ins(rt[i], rt[i-1], 1, n, a[i]);
}
for(int k = 1; k < n; k++) printf("%d ", solve(k));
return 0;
}
Codeforces 538 F. A Heap of Heaps的更多相关文章
- Codeforces Round #300 F - A Heap of Heaps (树状数组 OR 差分)
F. A Heap of Heaps time limit per test 3 seconds memory limit per test 512 megabytes input standard ...
- Codeforces300 F. A Heap of Heaps
Codeforces题号:#300F 出处: Codeforces 主要算法:树状数组/线段树 难度:4.6 思路分析: 在没看到数据范围之前真是喜出望外,直到发现O(n^2)会被卡…… 其实也不是特 ...
- L - A Heap of Heaps CodeForces - 538F 主席树
L - A Heap of Heaps CodeForces - 538F 这个是一个还比较裸的静态主席树. 这个题目的意思是把这个数组变成k叉树,然后问构成的树的子树小于等于它的父节点的对数有多少. ...
- [codeforces538F]A Heap of Heaps
[codeforces538F]A Heap of Heaps 试题描述 Andrew skipped lessons on the subject 'Algorithms and Data Stru ...
- Codeforces 959 F. Mahmoud and Ehab and yet another xor task
\(>Codeforces\space959 F. Mahmoud\ and\ Ehab\ and\ yet\ another\ xor\ task<\) 题目大意 : 给出一个长度为 \ ...
- Codeforces 835 F. Roads in the Kingdom
\(>Codeforces\space835 F. Roads in the Kingdom<\) 题目大意 : 给你一棵 \(n\) 个点构成的树基环树,你需要删掉一条环边,使其变成一颗 ...
- Codeforces 731 F. Video Cards(前缀和)
Codeforces 731 F. Video Cards 题目大意:给一组数,从中选一个数作lead,要求其他所有数减少为其倍数,再求和.问所求和的最大值. 思路:统计每个数字出现的个数,再做前缀和 ...
- CodeForces 538F A Heap of Heaps
题意 给定一个长度为n的数组A,将它变为一颗k叉树(1 <= k <= n - 1)(堆的形式编号). 问对于每一个k,有多少个节点小于它的父节点. 解题 显然,最初的想法是暴力.因为树的 ...
- Codeforces 797 F Mice and Holes
http://codeforces.com/problemset/problem/797/F F. Mice and Holes time limit per test 1.5 ...
随机推荐
- 洛谷P2901 [USACO08MAR]牛慢跑Cow Jogging
题目描述 Bessie has taken heed of the evils of sloth and has decided to get fit by jogging from the barn ...
- 基于 Docker 的 Zabbix 微服务系统
zabbix 官网提供一个镜像 [ zabbix-appliance ], 可以直接拉起一个 zabbix-server. 但是数据库无法分离出来. 本实践使用 zabbix 官方提供的 Docker ...
- Grunt构建工具
Grunt是javascript的构建工具,对于需要反复重复的任务,例如压缩(minification).编译.单元测试.linting等,自动化工具可以简化工作.Grunt生态系统非常庞大.你可以利 ...
- Java多线程学习(一)Java多线程入门
转载请备注地址:https://blog.csdn.net/qq_34337272/article/details/79640870 系列文章传送门: Java多线程学习(一)Java多线程入门 Ja ...
- arpspoof dnsspoof中间人攻击
最近搞了一个监听神器,尽管使用了网卡混杂模式,不过监听到的几乎全是本地流量, 为了获取更多有用的数据,搞一下中间人攻击,最基本的就是arpspoof + IP转发,这样就可以获得局域网内任何人的上网流 ...
- Win10默认浏览器怎么设置
1.首先在Win10桌面左下角的开始菜单图标上右键单击鼠标,在弹出的菜单选项中,点击进入“控制面板”,如下图所示. 接下来就可以找到“默认程序”设置了,找到后点击进入设置,如下图所示. 打开Win10 ...
- Ubuntu 14.04开启ssh服务
sudo apt-get install openssh-server sudo apt-get install openssh-client sudo service ssh restart
- C 基础框架开发
引言 有的人真的是天命所归 延安时期炸弹 投到他院子都 没炸. 有些事无法改变 是命! 我们也快'老'了, 常回家看看. 前言 扯淡结束了,今天分享的可能有点多,都很简单,但是糅合在一起就是有点复杂. ...
- Android内存溢出解决方案总结
我的视频会议中有三个内存泄露的崆点: 1) BNLiveControlView mView = this; 未释放 (自定义view中自己引用自己造成) 2) 在自定义View中区注册了系统的网络变化 ...
- [hadoop][会装]HBase集群安装--基于hadoop ha模式
可以参考部署HBase系统(分布式部署) 和基于无HA模式的hadoop下部署相比,主要是修改hbase-site .xml文件,修改如下参数即可: <property> <name ...