[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;
}

最新文章

  1. 欧洲宇航局(ESA)的协同设计室(CDF)
  2. 利用反射实现类通用的DAO层
  3. 十、Android学习第九天——小结(转)
  4. SAM4E单片机之旅——18、通过AFEC(ADC)获取输入的电压
  5. eclipse SDK更新管理器安装插件
  6. xcode中的一些快捷键
  7. 快速界面:QML。
  8. Codevs 1371 浴火银河跑运输
  9. CSS属性--过渡(transtion)
  10. Aisen仿新浪微博客户端项目源码
  11. 通过模板类简单实现Spark的JobServer
  12. Extjs4.2.1学习笔记[更新]
  13. Windows下用C语言获取进程cpu使用率,内存使用,IO情况
  14. iptables查看、添加、删除规则
  15. php五种常用的设计模式
  16. Java使用POI为Excel打水印,调整列宽并设置Excel只读(用户不可编辑)
  17. 【转】浅析Python中的struct模块
  18. Nginx (LNMP+https)
  19. MySQL查询命令_SELECT 子查询
  20. VUE2.0 饿了吗视频学习笔记(二):新版本添加路由和显示Header

热门文章

  1. poj1857 To Europe! To Europe!
  2. 第3章 接口与API设计 52条笔记
  3. 字符串循环右移-c语言
  4. 利用Jenkins打包ISO和QCOW2镜像文件
  5. HTTP 200 OK和HTTP 304 Not modified的由来
  6. SQLite – GROUP BY
  7. Android(java)学习笔记179:多媒体之加载大图片到内存(Bitmap API)
  8. mysql登录(linux)
  9. wkhtmltopdf导出html到pdf
  10. project .mpp 查看当天工作任务 1.选择自己 2.选择起始和终止时间 就显示当天的任务了