CodeForces 538F A Heap of Heaps

令人卡常的操作

简单的主席树,当然树状数组也行

CodeForces 538F A Heap of Heaps

题目描述

根据 $k$ 叉树(堆)的理论,我们构造一个 $k$ 叉小顶堆,请问你里面有多少个元素的位置在 $k$ 叉树下的条件放置的不合法(不满足 $A_{father_k} < A_k$)。

前置技能

根据 $k$ 叉树的标号原则:

  • $x$ 的父亲是 $\lfloor \frac x k \rfloor + 1$
  • $x$ 的拥有的儿子是 $[kx + 1, (k + 1)x]$

我们需要判断 $A_x$ 小于$[kx + 1, (k + 1)x]$ 里面多少个数,求出总和即可。

题解

主席树离散化,求出 $A_x$ 比 $[kx + 1, (k + 1)x]$ 的多少数小即可。复杂度 $O(k \log n)$ ,每一次访问各个点一次。

下面的写法是求出 $(kx, (k + 1)x]$ 里面的数量。

AC Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
//#define NOSTDCPP
//#define Cpp11
//#define Linux_System
#ifndef NOSTDCPP
#include <bits/stdc++.h>
#else
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <complex>
#include <cstring>
#include <cstdio>
#include <deque>
#include <exception>
#include <functional>
#include <iomanip>
#include <iostream>
#include <istream>
#include <iterator>
#include <list>
#include <map>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#endif
# ifdef Linux_System
# define getchar getchar_unlocked
# define putchar putchar_unlocked
# endif
# define RESET(_) memset(_, 0, sizeof(_))
# define RESET_(_, val) memset(_, val, sizeof(_))
# define fi first
# define se second
# define pb push_back
# define midf(x, y) ((x + y) >> 1)
# define DXA(_) (((_) << 1))
# define DXB(_) (((_) << 1) | 1)
# define next __Chtholly__
# define x1 __Mercury__
# define y1 __bbtl04__
# define index __ikooo__
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector <int> vi;
typedef set <int> si;
typedef pair <int, int> pii;
typedef long double ld;
const int MOD = 1e9 + 7;
const int maxn = 200009;
const int maxm = 300009;
const ll inf = 1e18;
const double pi = acos(-1.0);
const double eps = 1e-6;
ll myrand(ll mod){return ((ll)rand() << 32 ^ (ll)rand() << 16 ^ rand()) % mod;}
template <class T>
inline bool scan_d(T & ret)
{
char c;
int sgn;
if(c = getchar(), c == EOF)return false;
while(c != '-' && (c < '0' || c > '9'))c = getchar();
sgn = (c == '-') ? -1 : 1;
ret = (c == '-') ? 0 : (c - '0');
while(c = getchar(), c >= '0' && c <= '9')
ret = ret * 10 + (c - '0');
ret *= sgn;
return true;
}
#ifdef Cpp11
template <class T, class ... Args>
inline bool scan_d(T & ret, Args & ... args)
{
scan_d(ret);
scan_d(args...);
}
#define cin.tie(0); cin.tie(nullptr);
#define cout.tie(0); cout.tie(nullptr);
#endif
inline bool scan_ch(char &ch)
{
if(ch = getchar(), ch == EOF)return false;
while(ch == ' ' || ch == '\n')ch = getchar();
return true;
}
template <class T>
inline void out_number(T x)
{
if(x < 0)
{
putchar('-');
out_number(- x);
return ;
}
if(x > 9)out_number(x / 10);
putchar(x % 10 + '0');
}
int n, m;
int A[maxn], root[maxn], cnt;
struct node
{
int l, r, val;
}tree[maxn * 50];
#define lson(_) (tree[_].l)
#define rson(_) (tree[_].r)
int l, r, val;
void update(int pre, int &now, int nl, int nr)
{
now = ++ cnt;
tree[now] = tree[pre];
++ tree[now].val;
if(nl == nr) return ;
int mid = midf(nl, nr);
if(val <= mid) update(lson(pre), lson(now), nl, mid);
else update(rson(pre), rson(now), mid + 1, nr);
}
int query(int nl, int nr, int x, int y)
{
if(nl == nr) return 0;
int mid = midf(nl, nr), pp = tree[lson(y)].val - tree[lson(x)].val;
if(val <= mid) return query(nl, mid, lson(x), lson(y));
else return pp + query(mid + 1, nr, rson(x), rson(y));
}
#undef lson
#undef rson
vi v;
int main()
{
v.clear();
scanf("%d", &n);
for(register int i = 1; i <= n; ++ i) scanf("%d", A + i), v.push_back(A[i]);
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
for(register int i = 1; i <= n; ++ i)
A[i] = lower_bound(v.begin(), v.end(), A[i]) - v.begin() + 1;
for(register int i = 1; i <= n; ++ i)
{
val = A[i];
update(root[i - 1], root[i], 1, n);
}
ll ans = 0;
for(register int k = 1; k < n; ++ k)
{
ans = 0;
for(int i = 1, j = 1; j <= n; ++ i, j += k)
{
l = j, r = min(j + k, n), val = A[i];
ans += query(1, n, root[l], root[r]);
}
printf("%I64d ", ans);
}
puts("");
return 0;
}
文章目录
  1. 1. CodeForces 538F A Heap of Heaps
    1. 1.1. 题目描述
    2. 1.2. 前置技能
    3. 1.3. 题解
    4. 1.4. AC Code
{{ live2d() }}