[codeforces538F]A Heap of Heaps
[codeforces538F]A Heap of Heaps
试题描述
Andrew skipped lessons on the subject 'Algorithms and Data Structures' for the entire term. When he came to the final test, the teacher decided to give him a difficult task as a punishment.
The teacher gave Andrew an array of n numbers a1, ..., an. After that he asked Andrew for each k from 1 to n - 1 to build a k-ary heap on the array and count the number of elements for which the property of the minimum-rooted heap is violated, i.e. the value of an element is less than the value of its parent.
Andrew looked up on the Wikipedia that a k-ary heap is a rooted tree with vertices in elements of the array. If the elements of the array are indexed from 1 to n, then the children of element v are elements with indices k(v - 1) + 2, ..., kv + 1 (if some of these elements lie outside the borders of the array, the corresponding children are absent). In any k-ary heap every element except for the first one has exactly one parent; for the element 1 the parent is absent (this element is the root of the heap). Denote p(v) as the number of the parent of the element with the number v. Let's say that for a non-root element v the property of the heap is violated if av < ap(v).
Help Andrew cope with the task!
输入
The first line contains a single integer n (2 ≤ n ≤ 2·105).
The second line contains n space-separated integers a1, ..., an ( - 109 ≤ ai ≤ 109).
输出
in a single line print n - 1 integers, separate the consecutive numbers with a single space — the number of elements for which the property of the k-ary heap is violated, for k = 1, 2, ..., n - 1.
输入示例
输出示例
数据规模及约定
见“输入”
题解
从 1 到 n-1 枚举 k 的大小,对于每个 k,从序列的第二个元素开始每 k 个一组进行一次询问,具体来说就是询问一个长度为 k 的区间小于 x 的数有多少个。
询问显然可以用主席树轻松做到;对于每个 k 我们暴力扫一遍的总复杂度是调和级数的,所以是 O(n·logn);总复杂度 O(n·log2n)。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <stack>
#include <vector>
#include <queue>
#include <cstring>
#include <string>
#include <map>
#include <set>
using namespace std; const int BufferSize = 1 << 16;
char buffer[BufferSize], *Head, *Tail;
inline char Getchar() {
if(Head == Tail) {
int l = fread(buffer, 1, BufferSize, stdin);
Tail = (Head = buffer) + l;
}
return *Head++;
}
int read() {
int x = 0, f = 1; char c = Getchar();
while(!isdigit(c)){ if(c == '-') f = -1; c = Getchar(); }
while(isdigit(c)){ x = x * 10 + c - '0'; c = Getchar(); }
return x * f;
} #define maxn 200010
#define maxnode 4000010 int ToT, sumv[maxnode], lc[maxnode], rc[maxnode];
void update(int& y, int x, int l, int r, int p) {
sumv[y = ++ToT] = sumv[x] + 1;
if(l == r) return ;
int mid = l + r >> 1; lc[y] = lc[x]; rc[y] = rc[x];
if(p <= mid) update(lc[y], lc[x], l, mid, p);
else update(rc[y], rc[x], mid + 1, r, p);
return ;
}
int query(int o, int l, int r, int qr) {
if(!o) return 0;
if(r <= qr) return sumv[o];
int mid = l + r >> 1, ans = query(lc[o], l, mid, qr);
if(qr > mid) ans += query(rc[o], mid + 1, r, qr);
return ans;
} int n, rt[maxn], A[maxn], num[maxn]; int main() {
n = read();
for(int i = 1; i <= n; i++) num[i] = A[i] = read(); sort(num + 1, num + n + 1);
for(int i = 1; i <= n; i++) A[i] = lower_bound(num + 1, num + n + 1, A[i]) - num;
for(int i = 1; i <= n; i++) update(rt[i], rt[i-1], 1, n, A[i]);
for(int k = 1; k < n; k++) {
int ans = 0;
for(int i = 2, j = 1; i <= n; i += k, j++)
ans += query(rt[min(i+k-1,n)], 1, n, A[j] - 1) - query(rt[i-1], 1, n, A[j] - 1);
printf("%d%c", ans, k < n - 1 ? ' ' : '\n');
} return 0;
}
[codeforces538F]A Heap of Heaps的更多相关文章
- Codeforces538F A Heap of Heaps(函数式线段树)
题意:给你一个数组a[n],对于数组每次建立一个完全k叉树,对于每个节点,如果父节点的值比这个节点的值大,那么就是一个违规点,统计出1~n-1完全叉树下的违规点的各自的个数. 一个直觉的思想就是暴力, ...
- 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 ...
- Codeforces 538 F. A Heap of Heaps
\(>Codeforces \space 538 F. A Heap of Heaps<\) 题目大意 :给出 \(n\) 个点,编号为 \(1 - n\) ,每个点有点权,将这些点构建成 ...
- L - A Heap of Heaps CodeForces - 538F 主席树
L - A Heap of Heaps CodeForces - 538F 这个是一个还比较裸的静态主席树. 这个题目的意思是把这个数组变成k叉树,然后问构成的树的子树小于等于它的父节点的对数有多少. ...
- Codeforces300 F. A Heap of Heaps
Codeforces题号:#300F 出处: Codeforces 主要算法:树状数组/线段树 难度:4.6 思路分析: 在没看到数据范围之前真是喜出望外,直到发现O(n^2)会被卡…… 其实也不是特 ...
- CodeForces 538F A Heap of Heaps
题意 给定一个长度为n的数组A,将它变为一颗k叉树(1 <= k <= n - 1)(堆的形式编号). 问对于每一个k,有多少个节点小于它的父节点. 解题 显然,最初的想法是暴力.因为树的 ...
- [CF538F]A Heap of Heaps(主席树)
题面 题意:给你一个数组a[n],对于数组每次建立一个完全k叉树,对于每个节点,如果父节点的值比这个节点的值大,那么就是一个违规点,统计出1~n-1完全叉树下的违规点的各自的个数. 分析 注意到完全k ...
- CF数据结构练习
1. CF 438D The Child and Sequence 大意: n元素序列, m个操作: 1,询问区间和. 2,区间对m取模. 3,单点修改 维护最大值, 取模时暴力对所有>m的数取 ...
- Codeforces Round #300 解题报告
呜呜周日的时候手感一直很好 代码一般都是一遍过编译一遍过样例 做CF的时候前三题也都是一遍过Pretest没想着去检查... 期间姐姐提醒说有Announcement也自信不去看 呜呜然后就FST了 ...
随机推荐
- (026)[工具软件]剪切板管理:Ditto
剪切板管理软件:Ditto官网:http://ditto-cp.sourceforge.net/
- python中函数参数
默认参数注意点 优点:灵活,当没有指定与形参对应的实参时就会使用默认参数 缺陷: 例子: >>> def h(m, l=[]): #默认参数时列 ...
- ping localhost 返回 ::1的导致不能打开http://localhost的原因及解决
虽然可以在浏览器中正常访问http://localhost但用file,file_get_contents等函数打开http://localhost异常.用127.0.0.1也可以打开,本地hosts ...
- 搭建一个高可用的redis环境
一.环境准备 我的环境: Fedora 25 server 64位版 6台: 192.168.10.204 192.168.10.205 192.168.10.206 192.168.10.203 ...
- hihocoder offer收割编程练习赛8 B 拆字游戏
思路: 模拟,dfs. 注意题目中的trick,输出一块的时候不要把其他块也输出了. 实现: #include <cstring> #include <iostream> #i ...
- 如何在win7、win8、win8.1上安装使用vb6.0
https://jingyan.baidu.com/article/915fc414fdf8fb51384b2062.html如何在win7.win8.win8.1上安装使用vb6.0 如何在win7 ...
- NestedScrollView嵌套RecycleView发生的小问题
1.解决方法:嵌套滑动不激活. recycleView.setNestedScrollingEnable(false); 这样做有个弊端,RecycleView的item会一次性加载完,不管是否显示, ...
- 【HEVC简介】ALF-Adative Loop Filter
由于HEVC在HM4.0之后,就把ALF去掉,所以ALF的介绍是基于AVS2. <HEVC标准介绍.HEVC帧间预测论文笔记>系列博客,目录见:http://www.cnblogs.com ...
- vue项目中常用的一些公共方法
//校验手机号码 export function isSpecialPhone(num) { return /^1[2,3,4,5,7,8]\d{9}$/.test(num) } //校验中英文姓名 ...
- js数组常用方法整理
学疏才浅,若有不对的地方,希望大家可以帮忙指正出来. 1. Array.push(),向数组的末尾添加一个或多个元素,并返回新的数组长度.原数组改变. 2. Array.pop(),删除并返回数组的最 ...