[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.

输入示例

5
1 5 4 3 2

输出示例

3 2 1 0

数据规模及约定

见“输入

题解

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

 

posted @ 2017-02-01 09:30  xjr01  阅读(408)  评论(0编辑  收藏  举报