F. A Heap of Heaps
time limit per test

3 seconds

memory limit per test

512 megabytes

input

standard input

output

standard output

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.

Sample test(s)
input
5
1 5 4 3 2
output
3 2 1 0
input
6
2 2 2 2 2 2
output
0 0 0 0 0

题意:给定一个序列,一次建立k叉堆(1 <= k <= n-1), 求n-1个堆中 不合法的点的个数。
按照题解:两种做法,,感觉还是第一种比较好,。 大致做法: 我们先把所有数字按照大小排序(记录他们在原始序列的位置), 然后对于 第i个数字, 假设它在原始数组中的位置为pos, 那么他的孩子的index是 k*(pos-1)+2 ~ k*pos+1, 我们按照从小到大加入到树状数组中, 那么对于这个数字 他的孩子中不合法的个数就是 query (min(k*pos+1, n)) - query(k*(pos-1)+2)。 然后做n次这样的操作就OK了。。
 #include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5+;
int arr[MAXN];
int lowbit (int x)
{
return x & -x;
}
void modify(int x, int d)
{
while (x < MAXN)
{
arr[x] += d;
x += lowbit(x);
}
}
int query (int x)
{
int ans = ;
while (x)
{
ans += arr[x];
x -= lowbit(x);
}
return ans;
}
typedef pair <int, int>pii;
pii a[MAXN];
int n, ans[MAXN];
void solve ()
{
memset(arr, , sizeof (arr));
memset(ans, , sizeof (ans));
for (int i = ; i <= n; )
{
int tmp = i;
while (tmp <= n && a[tmp].first == a[i].first)
tmp++;
for (int j = i; j < tmp; j++)
{
for (int k = ; k <= n- && k*(a[j].second-)+ <= n; k++)
{
ans[k] += query(min(n, k*a[j].second+)) - query(k*(a[j].second-)+);
}
}
for (int j = i; j < tmp; j++)
modify(a[j].second, );
i = tmp;
}
for (int i = ; i <= n-; i++)
{
printf("%d%c", ans[i], " \n"[i==n-]);
}
}
int main(void)
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif // ONLINE_JUDGE
while (~scanf ("%d", &n))
{
for (int i = ; i < n; i++)
{
scanf ("%d", &a[i+].first);
a[i+].second = i+;
}
sort (a+, a+n+);
solve();
}
return ;
}

最新文章

  1. ros::spin() 和 ros::spinOnce() 区别及详解
  2. html5 弹框 可用于安卓手机弹出输入框
  3. Struts2 源码分析——项目分析
  4. 用php模拟做服务端侦听端口
  5. 使用Swift操作NSDate类型基础
  6. C#的同步和异步调用方法
  7. 一些.Net面试题 (BS 方向)
  8. iphone6闪存检测
  9. 内存排查 valgrind
  10. ASP.Net零碎
  11. 如何在苹果手机上安装自制的AD证书
  12. MySQL最佳实践
  13. DWM1000 多个基站定位讨论 --[蓝点无限]
  14. 非root安装fastDFS及启动
  15. 转载:MySQL:亲测备份策略实例(线上真实备份案例)
  16. 【理论】X理论、Y理论及Z理论
  17. 源码:Java集合源码之:数组与链表(一)
  18. Android 解决在初次打开Activity加载布局文件时,ScrollView滚动条不在顶部的问题
  19. 有复选框情况下,sql拼写技巧
  20. 前端基础——html

热门文章

  1. HDU1007 Quoit Design 【分治】
  2. Zend框架2入门(二) (转)
  3. Android面试,IntentService的原理及使用
  4. N皇后问题--递归回溯
  5. 在C#中internal关键字是什么意思?和protected internal区别
  6. tips [终端]
  7. 启发式算法、寻路算法A*算法
  8. AFNetworking 3.0的GET和POST的使用
  9. 武汉科技大学ACM:1008: 零起点学算法64——回型矩阵
  10. 安装beautifulsoup的奇怪问题