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

Codeforces Round #300 F - A Heap of Heaps (树状数组 OR 差分)的更多相关文章

  1. 【Codeforces Round #433 (Div. 1) C】Boredom(树状数组)

    [链接]h在这里写链接 [题意] 给你一个n*n的矩阵. 其中每一列都有一个点. 任意两个点构成了矩形的两个对角点 ->即任意两个点确定了一个矩形. ->总共能确定n*(n-1)/2个矩形 ...

  2. Codeforces Round #301 (Div. 2) E . Infinite Inversions 树状数组求逆序数

                                                                    E. Infinite Inversions               ...

  3. Codeforces Round #381 (Div. 2) D dfs序+树状数组

    D. Alyona and a tree time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  4. Educational Codeforces Round 87 (Rated for Div. 2) D树状数组加二分删除的值

    Sample Input 5 4 1 2 3 4 5 -5 -1 -3 -1 Sample Output 3 思路,首先发现a[i]的值的范围是在1~n之间,每次插入我们可以直接把cnt[a[i]]+ ...

  5. Codeforces Beta Round #79 (Div. 1 Only) B. Buses 树状数组

    http://codeforces.com/contest/101/problem/B 给定一个数n,起点是0  终点是n,有m两车,每辆车是从s开去t的,我们只能从[s,s+1,s+2....t-1 ...

  6. 8VC Venture Cup 2016 - Final Round (Div. 2 Edition) D. Factory Repairs 树状数组

    D. Factory Repairs 题目连接: http://www.codeforces.com/contest/635/problem/D Description A factory produ ...

  7. 【Codeforces】Gym 101156E Longest Increasing Subsequences LIS+树状数组

    题意 给定$n$个数,求最长上升子序列的方案数 根据数据范围要求是$O(n\log n)$ 朴素的dp方程式$f_i=max(f_j+1),a_i>a_j$,所以记方案数为$v_i$,则$v_i ...

  8. AtCoder Beginner Contest 253 F - Operations on a Matrix // 树状数组

    题目传送门:F - Operations on a Matrix (atcoder.jp) 题意: 给一个N*M大小的零矩阵,以及Q次操作.操作1(l,r,x):对于 [l,r] 区间内的每列都加上x ...

  9. Codeforces 1167 F Scalar Queries 计算贡献+树状数组

    题意 给一个数列\(a\),定义\(f(l,r)\)为\(b_1, b_2, \dots, b_{r - l + 1}\),\(b_i = a_{l - 1 + i}\),将\(b\)排序,\(f(l ...

随机推荐

  1. 实战:mysql版本号升级

    /***************************************************** mysql 5.6.19 升级到5.6.21 ********************** ...

  2. setTimeout()的返回值

    今天遇到一个问题,题目如下: var len=4; while(len--){ setTimeout(function(){ console.log(len); },0); console.log(l ...

  3. Jetty开发指导:框架

    Spring设置 你能嵌入Jetty到你的项目中,也能够使用差点儿全部的IoC类型框架,包含Spring.假设全部你想做的是在你的Spring中设置Jetty Server,那么以下的xml片段能够作 ...

  4. OD: ActiveX Vulnerabilities

    通过一个精心构造的页面 exploit 第三方软件中的 ActiveX 已经成为一种惯用攻击手段,众多知名软件公司都曾被发现其注册的 ActiveX 中存在严重的缓冲区溢出漏洞,一个被广泛使用的第三方 ...

  5. js动态新增组合Input标签

    var x = 1; function addlink() { var linkdiv = document.getElementById("add1_0"); if (linkd ...

  6. Android ScrollView 嵌套 ListView、 ListView 嵌套ScrollView Scroll事件冲突解决办法

    本人菜鸟一名,最近工作了,开始学习Android. 最近在做项目的时候,UX给了个design,大概就是下拉刷新的ListView中嵌套了ScrollView,而且还要在ScrollView中添加动画 ...

  7. mssql 2008 失败 需要重新启动计算机 的解决办法

    大致出错信息如下:RebootRequiredCheck 检查是否需要挂起计算机重新启动.挂起重新启动会导致安装程序失败. 失败 需要重新启动计算机.必须重新启动计算机才能安装 SQL Server. ...

  8. j2ee开发中的“java容器”和“web容器”有什么区别?

    http://blog.csdn.net/zi_jun/article/details/7387259

  9. Skin++ 皮肤库 CCheckListBox MFC 界面风格

    今天使用CCheckListBox,发现增加进去的字符串无法显示,但是当点击的时候,确有反应. 仔细检查代码,没有问题.之前也是这样用的,完全没有问题. 思前想后,觉得是因为使用了Skin++皮肤库, ...

  10. Java之简单的聊天工具

    今天整理资料的时候,找出自己几年前刚学Java时做过的一个简易的聊天工具,有服务器也有客户端,能发送文字消息和文件,但是用户上线并未存入数据库,而只是简单的缓存在服务器的一个数组中,所以,只要服务器一 ...