A Heap of Heaps (Codeforces 538F)
Tags: Math, sortings

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!

Input: 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).

Output: 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.

Note: Pictures with the heaps for the first sample are given below; elements for which the property of the heap is violated are marked with red.     In the second sample all elements are equal, so the property holds for all pairs.

Sample Inputs:
5
1 5 4 3 2

6
2 2 2 2 2 2

Sample Outputs:
3 2 1 0

0 0 0 0 0

Solution (Python):
import sys

def myargsort(a):
    b = list(zip(a, range(0, len(a))))
    b.sort()
    r = [pr[1] for pr in b]
    return r
fin = sys.stdin
n = int(fin.readline())
a = [int(number) for number in fin.readline().split()]
p = myargsort(a)
p.reverse()
j = 0
aib = [0] * (n + 1)

def ultb(x):
    return -(x ^ -x) // 2

def add(p, a, aib, n):
    while p <= n:
        aib[p] += a
        p += ultb(p)

def suma(p, aib):
    r = 0
    while p > 0:
        r += aib[p]
        p -= ultb(p)
    return r
for i in range(0, n):
    add(i + 1, 1, aib, n)
r = [0] * (n + 1)
for i in range(0, n):
    if i > 0 and a[i - 1] > a[i]:
        r[1] += 1
    while j < n and a[p[j]] == a[p[i]]:
        add(p[j] + 1, -1, aib, n)
        j += 1
    k = 2
    while k < n and p[i] * k + 1 < n:
        dr = min(n, p[i] * k + k + 1)
        st = p[i] * k + 2
        r[k] += suma(dr, aib) - suma(st - 1, aib)
        k += 1
print(*r[1:n])

Solution (Python):
def f_sum(arr, index):
    summa = 0
    while index >= 0:
        summa += arr[index]
        index = (index & index + 1) - 1
    return summa

def f_add(arr, index, item):
    while index < len(arr):
        arr[index] += item
        index |= index + 1

def solve(n, pairs):
    answ = [0] * n
    fenw = [0] * 201000
    for pair in pairs:
        index = pair[1]
        k = 1
        while k * index + 1 < n and k < n:
            answ[k] += f_sum(fenw, min(k * index + k, n)) - f_sum(fenw, k * index)
            k += 1
        f_add(fenw, index, 1)
    print(' '.join(map(str, answ[1:])))

def read_data():
    n = int(input())
    arr = sorted([(int(item), index) for (index, item) in enumerate(input().split())], key=lambda x: x[0])
    return (n, arr)

def main():
    (n, arr) = read_data()
    solve(n, arr)
if __name__ == '__main__':
    main()

Solution (Python):
def f_sum(arr, index):
    summa = 0
    while index >= 0:
        summa += arr[index]
        index = (index & index + 1) - 1
    return summa

def f_add(arr, index, item):
    while index < len(arr):
        arr[index] += item
        index |= index + 1

def solve(n, pairs):
    answ = [0] * n
    fenw = [0] * 201000
    for pair in pairs:
        index = pair[1]
        k = 1
        while k * index + 1 < n and k < n:
            answ[k] += f_sum(fenw, min(k * index + k, n)) - f_sum(fenw, k * index)
            k += 1
        f_add(fenw, index, 1)
    print(' '.join(map(str, answ[1:])))

def read_data():
    n = int(input())
    arr = sorted([(int(item), index) for (index, item) in enumerate(input().split())], key=lambda x: x[0])
    return (n, arr)

def main():
    (n, arr) = read_data()
    solve(n, arr)
if __name__ == '__main__':
    main()