[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.

输入示例

5
1 5 4 3 2

输出示例

3 2 1 0

数据规模及约定

见“输入

题解

从 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 4000010int 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;
}

转载于:https://www.cnblogs.com/xiao-ju-ruo-xjr/p/6359793.html

[codeforces538F]A Heap of Heaps相关推荐

  1. CodeForces - 538F--A Heap of Heaps(树状数组+离线)

    题目链接https://codeforces.com/problemset/problem/538/F Time limit 3000 ms Memory limit 524288 kB Andrew ...

  2. 【CF538F】 A Heap of Heaps

    题目 展开 题目描述 Andrew skipped lessons on the subject 'Algorithms and Data Structures' for the entire ter ...

  3. 【题解】CF538F:A Heap of Heaps

    原题传送门 按照权值从小到大排序 找的是儿子中小于自己的有几个,这样我们就顺着枚举,满足权值递增,用树状数组维护个数,对于一个xxx,kkk叉树,儿子区间为[xk+1−k,xk+1][xk+1-k,x ...

  4. CodeForce 538-F A Heap of Heaps(主席树)

    题目: 传送门 思路:        咱用 离线+树状数组 逃了好多次课 ,终究还是 被抓回来补课了:        对于每个k,每个点我们都去查询 [k*(I-1)+2,min(n,k*I+1)] ...

  5. [CF538F]A Heap of Heaps(主席树)

    题面 题意:给你一个数组a[n],对于数组每次建立一个完全k叉树,对于每个节点,如果父节点的值比这个节点的值大,那么就是一个违规点,统计出1~n-1完全叉树下的违规点的各自的个数. 分析 注意到完全k ...

  6. [CF538F]A Heap of Heaps

    题目大意 懒得写 做法 从0开始标号. 注意i是父亲是(i-1)/k 因此可以分块 对一段产生的影响可以在数组上打tag #include<cstdio> #include<algo ...

  7. [CF538F]A Heap of Heaps 持久化线段树

    直接枚举k 每个有儿子的节点和他的儿子区间的起点 容易发现这样枚举是调和级数 然后需要一个东西查询某段区间比x小的数 上主席树即可 #include<cstdio> #include< ...

  8. 为上次渲染的三角形添加颜色

    让我们在场景中添加一些颜色. 在本教程中,我们将为顶点添加颜色以为三角形着色. 这涉及更新顶点着色器以将颜色传递给像素着色器,像素着色器以输出传递给它的颜色,顶点结构添加颜色属性,输入布局包含颜色输入 ...

  9. ION基本概念介绍和原理分析[转]

    转载前的话: ION将内核态形形色色的内存分配纳入统一的管理接口之中,更重要的设计意图是为内存在不同用户态进程之间传递和访问提供了支持. 每个ion_buffer与一个struct file关联,其h ...

  10. ION基本概念介绍和原理分析

    转载前的话: ION将内核态形形色色的内存分配纳入统一的管理接口之中,更重要的设计意图是为内存在不同用户态进程之间传递和访问提供了支持. 每个ion_buffer与一个struct file关联,其h ...

最新文章

  1. js 使用filter过滤多重数组
  2. php 年的第几天,收藏-php中某年第几天计算出日期年月日的代码
  3. 深入理解JavaScript系列(32):设计模式之观察者模式
  4. 无线策略服务器,无线网络中的分布式资源管理策略研究
  5. android studio创建构造方法,使用Android studio创建你的第一个项目
  6. 简单-三层-存储过程-增删改《一》
  7. 小米王育军:小爱背后的小米语音技术
  8. kvm初体验之八:调整vm的vcpu, memory, disk大小
  9. python常用中文分词方法_中文分词原理及常用Python中文分词库介绍
  10. 勒索病毒处置经验分享
  11. 万字攻略全面了解selenium_selenium教程
  12. 解决 kindle 书籍字体颜色偏淡问题的方法
  13. 在更改计算机的设置路由器,怎么改路由器wifi密码 怎么修改路由器wifi密码
  14. 上山能养鸡,下海能养鲍鱼,他如何依靠养殖成为富翁
  15. 什么是握手信号? 什么是握手协议?
  16. [新版新概念英语1-4册全部视频和课本]
  17. 孙陶然:创业是从零开始的一次全新旅程
  18. 教你挑选适合自己的蜂蜜
  19. cadence 查看器件丝印_一种Allegro软件中自动检查丝印摆放方向的方法与流程
  20. java计算机毕业设计服装定制管理系统源码+mysql数据库+系统+lw文档+部署

热门文章

  1. eclipse 环境安装
  2. 基于物联网的工业分析将席卷制造业
  3. (转载)程序员文史综合题目一(附答案)
  4. php 字符串加,php字符串如何增加
  5. json html双引号,当gethtml方法返回json时,json中的字符串如果存在双引号,会破坏json的格式, 如:quot;...
  6. nginx源码阅读 ---- Event模块和配置的初始化
  7. C++(多态实现原理)函数重写,重载,重定义
  8. linux 快捷键回复禁用,Linux 禁用Ctrl+Alt+Delete重启服务器操作
  9. Windows搭建SVN实现访问远程SVN库
  10. Redis Cluster集群的配置