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 ...
随机推荐
- 【BZOJ1093】【ZJOI2007】最大半联通子图 [DP][Tarjan]
最大半连通子图 Time Limit: 30 Sec Memory Limit: 162 MB[Submit][Status][Discuss] Description 一个有向图G=(V,E)称为 ...
- bzoj 1702: [Usaco2007 Mar]Gold Balanced Lineup 平衡的队列——map+hash+转换
Description N(1<=N<=100000)头牛,一共K(1<=K<=30)种特色, 每头牛有多种特色,用二进制01表示它的特色ID.比如特色ID为13(1101), ...
- 【BZOJ】3502 PA2012 Tanie linie
[算法]贪心,一般DP [题解] --- 胡策k≤10的环状DP做法: 1.钦定法:先确定第一位(可能和第n位)的状态,然后后面正常做DP,显然正确答案是一定会被记录的,因为从整体上看不会有影响. 2 ...
- 【BZOJ】1500: [NOI2005]维修数列
[算法]splay [题解]数据结构 感谢Occult的模板>_<:HYSBZ 1500 维修数列 #include<cstdio> #include<cctype> ...
- node起本地服务器以及实现代理,前端接口转发
上一篇文章写了使用docker来做nginx镜像实现本地的页面代理以及接口转发,但是需要下载docker,这个对于很多人来说还是显得比较麻烦,于是这个文章就是介绍如何只用node就可以代理本地的页面和 ...
- new操作符的内部运行解析
在加上new操作符,我们就能完成传统面向对象的class + new的方式创建对象,在Javascript中,我们将这类方式成为Pseudoclassical. 基于上面的例子,我们执行如下代码 ...
- js中的true和false
1.false undefined.NaN.0.null和空字符串''均被视为false 2.true 除上述以外的其它情况一律被视作true
- 安装FFMpeg CentOS 7
https://linuxadmin.io/install-ffmpeg-on-centos-7/
- 前端—css
css css概述 CSS是Cascading Style Sheets的简称,中文称为层叠样式表,用来控制网页数据的表现,可以使网页的表现与数据内容分离. 一.css的四种引入方式: 1.行内式 ...
- 【DeepLearning学习笔记】Coursera课程《Neural Networks and Deep Learning》——Week1 Introduction to deep learning课堂笔记
Coursera课程<Neural Networks and Deep Learning> deeplearning.ai Week1 Introduction to deep learn ...