[$>Codeforces \space 538 F. A Heap of Heaps

题目大意 :给出 \(n\) 个点,编号为 \(1 - n\) ,每个点有点权,将这些点构建成 \(k\) 叉树的形式 \((k \in [1, n - 1])\) 。

对于编号为 \(i\) 的点,它的儿子区间是 \([\ k(i-1)+2, \ \min(ki + 1, n)\ ]\) 如果说一个点违反堆性质,当且仅当它的点权小于它父亲的点权,对于所有 \((k \in [1, n - 1])\) 叉树,求出按照给定规则构建后违反堆性质的点的数量

\(1≤ n ≤ 2 \times 10^5\) , \(-10^9 \leq a_i \leq 10^9\)

解题思路 :

观察发现,按照题目规则构建的 \(k\) 叉树,有儿子的点最多只有 \(\frac{n}{k}\) 个

不妨暴力枚举 \(k\) ,对于每一个 \(k\) 枚举树中有儿子的点,统计其对应的儿子区间里权值比他小的点的数量

考虑本质上是一个二维数点,那么离散化 \(+\) 主席树就可以在 \(O(logn)\) 的时间内完成单次查询

考虑枚举部分的复杂度是一个类似于 \(\frac{n}{1} + \frac{n}{2} +..+ \frac{n}{n-1}\) 的调和级数状物,复杂度是 \(O(nlogn)\)

所以算法的总复杂度是 \(O(nlog^2n)\)

/*program by mangoyang*/
#include<bits/stdc++.h>
#define inf (0x7f7f7f7f)
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define Min(a, b) ((a) < (b) ? (a) : (b))
typedef long long ll;
using namespace std;
template <class T>
inline void read(T &x){
int f = 0, ch = 0; x = 0;
for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = 1;
for(; isdigit(ch); ch = getchar()) x = x * 10 + ch - 48;
if(f) x = -x;
}
#define N (1000005)
int s[N], a[N], rt[N], n;
struct SegmentTree{
int rc[N*25], lc[N*25], sz[N*25], cnt;
inline void ins(int &u, int pr, int l, int r, int pos){
u = ++cnt, sz[u] = sz[pr] + 1;
lc[u] = lc[pr], rc[u] = rc[pr];
if(l == r) return; int mid = l + r >> 1;
if(pos <= mid) ins(lc[u], lc[pr], l, mid, pos);
else ins(rc[u], rc[pr], mid + 1, r, pos);
}
inline int query(int x, int y, int l, int r, int L, int R){
if(l >= L && r <= R) return sz[y] - sz[x];
int mid = l + r >> 1, res = 0;
if(L <= mid) res += query(lc[x], lc[y], l, mid, L, R);
if(mid < R) res += query(rc[x], rc[y], mid + 1, r, L, R);
return res;
}
}van;
inline int solve(int k){
int ans = 0;
for(int i = 1; i <= n; i++){
int l = k * (i - 1) + 2, r = Min(n, k * i + 1);
if(l > n) return ans;
if(a[i] == 1) continue;
ans += van.query(rt[l-1], rt[r], 1, n, 1, a[i] - 1);
}
return ans;
}
int main(){
read(n);
for(int i = 1; i <= n; i++)
read(a[i]), s[i] = a[i];
sort(s + 1, s + n + 1);
int sz = unique(s + 1, s + n + 1) - s - 1;
for(int i = 1; i <= n; i++){
a[i] = lower_bound(s + 1, s + sz + 1, a[i]) - s;
van.ins(rt[i], rt[i-1], 1, n, a[i]);
}
for(int k = 1; k < n; k++) printf("%d ", solve(k));
return 0;
}

Codeforces 538 F. A Heap of Heaps的更多相关文章

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

  2. Codeforces300 F. A Heap of Heaps

    Codeforces题号:#300F 出处: Codeforces 主要算法:树状数组/线段树 难度:4.6 思路分析: 在没看到数据范围之前真是喜出望外,直到发现O(n^2)会被卡…… 其实也不是特 ...

  3. L - A Heap of Heaps CodeForces - 538F 主席树

    L - A Heap of Heaps CodeForces - 538F 这个是一个还比较裸的静态主席树. 这个题目的意思是把这个数组变成k叉树,然后问构成的树的子树小于等于它的父节点的对数有多少. ...

  4. [codeforces538F]A Heap of Heaps

    [codeforces538F]A Heap of Heaps 试题描述 Andrew skipped lessons on the subject 'Algorithms and Data Stru ...

  5. Codeforces 959 F. Mahmoud and Ehab and yet another xor task

    \(>Codeforces\space959 F. Mahmoud\ and\ Ehab\ and\ yet\ another\ xor\ task<\) 题目大意 : 给出一个长度为 \ ...

  6. Codeforces 835 F. Roads in the Kingdom

    \(>Codeforces\space835 F. Roads in the Kingdom<\) 题目大意 : 给你一棵 \(n\) 个点构成的树基环树,你需要删掉一条环边,使其变成一颗 ...

  7. Codeforces 731 F. Video Cards(前缀和)

    Codeforces 731 F. Video Cards 题目大意:给一组数,从中选一个数作lead,要求其他所有数减少为其倍数,再求和.问所求和的最大值. 思路:统计每个数字出现的个数,再做前缀和 ...

  8. CodeForces 538F A Heap of Heaps

    题意 给定一个长度为n的数组A,将它变为一颗k叉树(1 <= k <= n - 1)(堆的形式编号). 问对于每一个k,有多少个节点小于它的父节点. 解题 显然,最初的想法是暴力.因为树的 ...

  9. Codeforces 797 F Mice and Holes

    http://codeforces.com/problemset/problem/797/F F. Mice and Holes time limit per test             1.5 ...

随机推荐

  1. 【BZOJ】1419 Red is good

    [算法]期望DP [题解]其实把状态表示出来就是很简单的期望DP. f[i][j]表示i张红牌,j张黑牌的期望. i=0时,f[0][j]=0. j=0时,f[i][0]=i. f[i][j]=max ...

  2. Piggy-Bank(多重背包+一维和二维通过方式)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1114 题面: Problem Description Before ACM can do anythi ...

  3. 大聊Python----进程和线程

    什么是线程? 线程是操作系统能够进行运算调度的最小单位.它被包含在进程之中,是进程中的实际运作单位.一条线程指的是进程中一个单一顺序的控制流,一个进程中可以并发多个线程,每条线程并行执行不同的任务. ...

  4. sql server 在作业中 远程连接 oracle mysql sqlserver 数据库

    在作业中执行远程连接时,需要对本次作业执行的步骤指定特定用户 并且该用户必须拥有所需操作数据库的db_owner角色,和服务器sysadmin角色 在作业中执行远程连接时,需要做登录映射 下面是我在作 ...

  5. Angular2.0 基础: User Input

    1.Angular 2.0 中的变量 对输入值的获取,我们可以通过$event 来获取,也可以通过变量来获取. template: ` <input (keyup)="onKey($e ...

  6. JVM在遇到OOM(OutOfMemoryError)时生成Dump文件

    方法一: 命令:jmap -dump:format=b,file=heap.bin file:保存路径及文件名pid:进程编号(windows通过任务管理器查看,linux通过ps aux查看) du ...

  7. Python脚本 - 常用单位转换

    测试系统为:Centos 6.7 Python版本为: 3.6.4 脚本功能:常用单位的转换,这里用内存来模拟 import pstuil def bytes2human(n): symbols = ...

  8. linux编程之信号

    信号(signal)机制是UNIX系统中最为古老的进程之间的通信机制,它用在一个或多个进程之间传递异步信号,信号可以由各种异步事件产生,如: 键盘中断等等,在Linux 的shell 中,也可以使用信 ...

  9. linux加载指定目录的so文件

    linux加载指定目录的so文件 http://blog.csdn.net/win_lin/article/details/8286125 download urlhttp://download.ch ...

  10. vue做购物车

    写一点废话,昨天敲代码找bug,找了好久都没找到,后来一哥们找到他说,找代码的bug就像男女朋友吵架,女问男你错了没,男说错啦,女再问错哪了,男傻眼了不知道错哪.在找代码的过程中一直知道我错啦就是找不 ...