CF993E Nikita and Order Statistics

给你一个长度为 $n$ 个序列 $a$ 和一个数 $x$,对于 $0\leq k\leq n$ 求出有多少个 $a$ 的子区间满足恰好有 $k$ 个数小于 $x$。

预处理前缀和,对前缀和$NTT$没了。

代码
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


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pii pair<int, int>
#define mk make_pair
const int N = 1e6 + 10;
const int mod = 1e9 + 7;
int read()
{
int x = 0, f = 1;
char c = getchar();
while (c < '0' || c > '9')
{
if (c == '-')
f = -1;
c = getchar();
}
while (c >= '0' && c <= '9')
x = (x << 1) + (x << 3) + c - '0', c = getchar();
return x * f;
}
const double pi = acos(-1.0);
struct Complex
{
double x, y;
Complex(double _x = 0.0, double _y = 0.0)
{
x = _x;
y = _y;
}

Complex operator-(const Complex &b) const
{
return Complex(x - b.x, y - b.y);
}

Complex operator+(const Complex &b) const
{
return Complex(x + b.x, y + b.y);
}

Complex operator*(const Complex &b) const
{
return Complex(x * b.x - y * b.y, x * b.y + y * b.x);
}
};
int rev[N];
void FFT(Complex *A, int n, int inv)
{
for (int i = 0; i < n; i++)
if (i < rev[i])
swap(A[i], A[rev[i]]);
for (int l = 1; l < n; l <<= 1)
{
Complex temp(cos(pi / l), inv * sin(pi / l));
for (int i = 0; i < n; i += (l << 1))
{
Complex omega(1, 0);
for (int j = 0; j < l; j++, omega = omega * temp)
{
Complex x = A[i + j], y = omega * A[i + j + l];
A[i + j] = x + y;
A[i + j + l] = x - y;
}
}
}

if (inv == -1)
for (int i = 0; i < n; i++)
A[i].x = ll(A[i].x / n + 0.5);
}

void FFTX(Complex *a, int n, Complex *b, int m, Complex *ans)
{
int ML = 1, bit = 0;
while (ML < n + m)
ML <<= 1, bit++;
for (int i = 0; i < ML; i++)
rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << (bit - 1));
FFT(a, ML, 1);
FFT(b, ML, 1);
for (int i = 0; i < ML; i++)
ans[i] = a[i] * b[i];
FFT(ans, ML, -1);
}
int a[N];
ll ans[N];
Complex f[N], g[N], c[N];
int main()
{
int n = read(), x = read();
for (int i = 1; i <= n; i++)
{
int u = read();
a[i] = a[i - 1] + (u < x);
}
for (int i = 0; i <= n; i++)
{
if (i != n)
f[a[i]].x++;
if (i)
g[a[i]].x++;
}

reverse(g, g + n + 1);
FFTX(f, n, g, n, c);
for (int k = 0; k <= n; k++)
ans[k] = ll(c[n - k].x);
ans[0] = 1ll * n * (n + 1) / 2;
for (int k = 1; k <= n; k++)
ans[0] -= ans[k];
for (int k = 0; k <= n; k++)
printf("%lld ", ans[k]);
}