Codeforces Round #300 F - A Heap of Heaps (树状数组 OR 差分)
3 seconds
512 megabytes
standard input
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!
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
6
2 2 2 2 2 2
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 差分)的更多相关文章
- 【Codeforces Round #433 (Div. 1) C】Boredom(树状数组)
[链接]h在这里写链接 [题意] 给你一个n*n的矩阵. 其中每一列都有一个点. 任意两个点构成了矩形的两个对角点 ->即任意两个点确定了一个矩形. ->总共能确定n*(n-1)/2个矩形 ...
- Codeforces Round #301 (Div. 2) E . Infinite Inversions 树状数组求逆序数
E. Infinite Inversions ...
- 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 ...
- 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]]+ ...
- 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 ...
- 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 ...
- 【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 ...
- 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 ...
- 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 ...
随机推荐
- Android 自定义CheckBox样式
1.首先在drawable文件夹中添加drawable文件checkbox_style.xml. <selector xmlns:android="http://schemas.and ...
- iOS8 Core Image In Swift:视频实时滤镜
iOS8 Core Image In Swift:自己主动改善图像以及内置滤镜的使用 iOS8 Core Image In Swift:更复杂的滤镜 iOS8 Core Image In Swift: ...
- 项目总结——深入浅出socket网络编程
前言: 为什么会有如题的概念呢,我想对于没有主动听说过socket网络编程的人来说读到题目可能就已经蒙头了,为了很好的让大家进入场景,首先说一下一个需要用到这点东西的业务需求. 首先大家应该明确的是s ...
- 基于 Java 2 运行时安全模型的线程协作--转
在 Java 2 之前的版本,运行时的安全模型使用非常严格受限的沙箱模型(Sandbox).读者应该熟悉,Java 不受信的 Applet 代码就是基于这个严格受限的沙箱模型来提供运行时的安全检查.沙 ...
- TCP总结
TCP协议 <计算机网络>谢希仁 及笔记 TCP 的那些事儿(上):http://coolshell.cn/articles/11564.html TCP 的那些事儿(下):htt ...
- Activity启动机制
以下资料摘录整理自老罗的Android之旅博客,是对老罗的博客关于Android底层原理的一个抽象的知识概括总结(如有错误欢迎指出)(侵删):http://blog.csdn.net/luosheng ...
- 作为iOS开发者不得不follow的52人
对于每位iOS开发者来说,Twitter是个获得最新iOS开发技术和相关信息的好地方.如果你刚好有Twitter账户,可以关注以下为你推荐的该领域内的52个优秀人物. 1.Tim Cook 这位无需多 ...
- SpringMVC06以对象的方式获取前台的数据
========创建需要的两个实体类================ public class School { private String sName; private String addres ...
- Cannot modify header information - headers already sent by
有时候你在使用 header("Location: http://localhost/myblog/index.php/manager/listview");的时候会出现这个问题, ...
- 匹配不含有某个信息的sql语句写法
SELECT id,order_id,flight_info FROM order_flights WHERE mark=0 AND flight_info REGEXP '[^() DAY)]' O ...