[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.
输入示例
输出示例
数据规模及约定
见“输入”
题解
从 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;
}
[codeforces538F]A Heap of Heaps的更多相关文章
- Codeforces538F A Heap of Heaps(函数式线段树)
题意:给你一个数组a[n],对于数组每次建立一个完全k叉树,对于每个节点,如果父节点的值比这个节点的值大,那么就是一个违规点,统计出1~n-1完全叉树下的违规点的各自的个数. 一个直觉的思想就是暴力, ...
- Codeforces Round #300 F - A Heap of Heaps (树状数组 OR 差分)
F. A Heap of Heaps time limit per test 3 seconds memory limit per test 512 megabytes input standard ...
- Codeforces 538 F. A Heap of Heaps
\(>Codeforces \space 538 F. A Heap of Heaps<\) 题目大意 :给出 \(n\) 个点,编号为 \(1 - n\) ,每个点有点权,将这些点构建成 ...
- L - A Heap of Heaps CodeForces - 538F 主席树
L - A Heap of Heaps CodeForces - 538F 这个是一个还比较裸的静态主席树. 这个题目的意思是把这个数组变成k叉树,然后问构成的树的子树小于等于它的父节点的对数有多少. ...
- Codeforces300 F. A Heap of Heaps
Codeforces题号:#300F 出处: Codeforces 主要算法:树状数组/线段树 难度:4.6 思路分析: 在没看到数据范围之前真是喜出望外,直到发现O(n^2)会被卡…… 其实也不是特 ...
- CodeForces 538F A Heap of Heaps
题意 给定一个长度为n的数组A,将它变为一颗k叉树(1 <= k <= n - 1)(堆的形式编号). 问对于每一个k,有多少个节点小于它的父节点. 解题 显然,最初的想法是暴力.因为树的 ...
- [CF538F]A Heap of Heaps(主席树)
题面 题意:给你一个数组a[n],对于数组每次建立一个完全k叉树,对于每个节点,如果父节点的值比这个节点的值大,那么就是一个违规点,统计出1~n-1完全叉树下的违规点的各自的个数. 分析 注意到完全k ...
- CF数据结构练习
1. CF 438D The Child and Sequence 大意: n元素序列, m个操作: 1,询问区间和. 2,区间对m取模. 3,单点修改 维护最大值, 取模时暴力对所有>m的数取 ...
- Codeforces Round #300 解题报告
呜呜周日的时候手感一直很好 代码一般都是一遍过编译一遍过样例 做CF的时候前三题也都是一遍过Pretest没想着去检查... 期间姐姐提醒说有Announcement也自信不去看 呜呜然后就FST了 ...
随机推荐
- 解决spring boot websocket
在网上找的demo写了一个小例子,本地开发测试都很正常,但是部署在tomcat就各种坑 1.MyWebSocket不要用spring 注解标注 2.main方法对应的类继承SpringBootServ ...
- REST风格笔记
这一篇主要是看了FB的覃超大大的文章,做了一些笔记和自己的思考. 定义: 用URL来定义资源,用HTTP(GET/POST/DELETE/DETC)来描述操作. 1. REST描述的是网络 ...
- vue中的事件监听之——v-on vs .$on
跟着视频中老师的教学视频学vue的时候,看很多时候都用@(v-on)来监听子级emit的自定义事件,但在bus总线那块,又用.$on来监听bus自身emit的事件,v-on之间似乎相似但又不同,今天对 ...
- netcdf源码在windows上的编译
作者:朱金灿 来源:http://blog.csdn.net/clever101 今天搞搞netcdf源码在windows上的编译,折腾了半天,算是搞成了,特地记录一下过程.我的目标是要生成netcd ...
- 搜索模板elasticsearch
搜索: like 对中文分词效率与支持都不太友好elasticsearch 实时的(效率高).分布式(可扩展)的搜索和分析引擎,基于Lucene全文搜索引擎工具包,算法基于倒排索引算法(eg:一篇文章 ...
- C# 图片打印杂谈
日常开头水一下,看了下上次博客,一年零八天了,啧啧,奢侈. 最近这个工作挺满意的,是我想要的发展方向,后续要做机器学习,现在得先把公司之前堆积的问题解决了. 谈人生到此结束,还是说正题吧.(感觉这标题 ...
- qt QTableView/QTableWidget样式设置
转载请注明出处:http://www.cnblogs.com/dachen408/p/7591409.html 选中设置: QTableView::item:selected { background ...
- 洛谷 P1163 银行贷款
题目描述 当一个人从银行贷款后,在一段时间内他(她)将不得不每月偿还固定的分期付款.这个问题要求计算出贷款者向银行支付的利率.假设利率按月累计. 输入输出格式 输入格式: 输入文件仅一行包含三个用空格 ...
- SQLite概述
SQLite概述 这个教程帮助您理解SQLite是什么,它如何不同于SQL,为什么它是必要的和它的方式处理应用程序数据库. SQLite是一个库,实现了一个独立的软件,serverless zer ...
- C-基础:关于预编译以及宏
这是没有引入任何头文件时,如果使用"NULL",编译器会报错:没有定义NULL.此时可用下面代码定义. #undef NULL //#undef 是在后面取消以前定义的宏定义#if ...