「BJ模拟」「CodeChef」「BZOJ-3509」COUNTARI 等差数列-暴力/FFT

给定 $n$ 个整数 $A_1,A_2,A_n$ ,求有多少个三元组 $(i,j,k)$ 满足 $1 \leq i < j < k \leq n$ 且 $A_j-A_i=A_k-A_j$ 。

链接

CodeChef-COUNTARI
BZOJ-3509

题解

此题可以暴力踩标程,用前后两个桶来统计,复杂度 $O(n max(a))$,这个暴力相信大家都会,这时我们只需要循环展开 $15$ 层,刺激 $CPU$ 并发,就拿下了 $BZOJ$ 和 $CC$ rank1….

然而被模拟赛的垃圾CPU坑T了….

暴力

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
/*
* created by xehoth on 18-03-2017
* 不能并行?
* CPU 并发!(好虚啊)
* 假设不卡 CPU 并发...
*/
#include <bits/stdc++.h>

inline char read() {
static const int IN_LEN = 1000000;
static char buf[IN_LEN], *s, *t;
if (s == t) {
t = (s = buf) + fread(buf, 1, IN_LEN, stdin);
if (s == t) return -1;
}
return *s++;
}

template<class T>
inline bool read(T &x) {
static bool iosig;
static char c;
for (iosig = false, c = read(); !isdigit(c); c = read()) {
if (c == '-') iosig = true;
if (c == -1) return false;
}
for (x = 0; isdigit(c); c = read())
x = (x + (x << 2) << 1) + (c ^ '0');
if (iosig) x = -x;
return true;
}

const int OUT_LEN = 10000000;
char obuf[OUT_LEN], *oh = obuf;

inline void print(char c) {
if (oh == obuf + OUT_LEN) fwrite(obuf, 1, OUT_LEN, stdout), oh = obuf;
*oh++ = c;
}

template<class T>
inline void print(T x) {
static int buf[30], cnt;
if (x == 0) {
print('0');
} else {
if (x < 0) print('-'), x = -x;
for (cnt = 0; x; x /= 10) buf[++cnt] = x % 10 + 48;
while (cnt) print((char)buf[cnt--]);
}
}

inline void flush() {
fwrite(obuf, 1, oh - obuf, stdout);
}

typedef unsigned long long ull;
typedef unsigned int uint;
#define long long long

const int MAXN = 100000;
const int MAX_VAL = 30000;
uint v[MAXN], bucL[MAX_VAL + 1], bucR[MAX_VAL + 1];

namespace Concurrent {

/*concurrent*/
inline void concurrentSolve() {
/* real register %esp %ebp %eax */
register int i, n, tmp;
read(n);
for (i = 0; i < n; i++)
read(tmp), bucR[MAX_VAL - (v[i] = tmp)]++;

bucR[MAX_VAL - v[0]]--;
register int minL = v[0], maxL = v[0];

register long ans = 0;

n--;
for (i = 1; i < n; i++) {
register int last = v[i - 1], cur = v[i];
if (last < minL) minL = last;
else if (last > maxL) maxL = last;

bucL[last]++;
bucR[MAX_VAL - cur]--;

register int bufx = cur << 1, low = std::max(minL, bufx - MAX_VAL),
high = std::min(maxL, bufx - 1);
/*CPU 并发优化*/
register uint tmp = 0, *p1 = bucL + low, *pr = bucL + high - 14,
*p2 = bucR + MAX_VAL - bufx + low;
/*循环展开 + 刺激并发*/
while (p1 <= pr) {
tmp += (*p1) * (*p2) + (*(p1 + 1)) * (*(p2 + 1)) + (*(p1 + 2)) *
(*(p2 + 2)) + (*(p1 + 3)) * (*(p2 + 3)) + (*(p1 + 4)) * (*(p2 + 4))
+ (*(p1 + 5)) * (*(p2 + 5)) + (*(p1 + 6)) * (*(p2 + 6)) + (*(p1 + 7))
* (*(p2 + 7)) + (*(p1 + 8)) * (*(p2 + 8)) + (*(p1 + 9)) * (*(p2 + 9))
+ (*(p1 + 10)) * (*(p2 + 10)) + (*(p1 + 11)) * (*(p2 + 11))
+ (*(p1 + 12)) * (*(p2 + 12)) + (*(p1 + 13)) * (*(p2 + 13))
+ (*(p1 + 14)) * (*(p2 + 14));

p1 += 15, p2 += 15;
}
while (p1 <= bucL + high) tmp += (*(p1++)) * (*(p2++));
ans += tmp;
}

print(ans);
}

}

int main() {
#ifndef ONLINE_JUDGE
freopen("in.in", "r", stdin);
#endif
Concurrent::concurrentSolve();
flush();
return 0;
}

FFT

这份是分块的,部分暴力部分正解

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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*
* created by xehoth on 18-03-2017
* 不能并行?
* CPU 并发!(好虚啊)
* 假设不卡 CPU 并发...
******************************
* 垃圾CPU
* 就这 OJ 被卡
*/
#include <bits/stdc++.h>

inline char read() {
static const int IN_LEN = 1000000;
static char buf[IN_LEN], *s, *t;
if (s == t) {
t = (s = buf) + fread(buf, 1, IN_LEN, stdin);
if (s == t) return -1;
}
return *s++;
}

template<class T>
inline bool read(T &x) {
static bool iosig;
static char c;
for (iosig = false, c = read(); !isdigit(c); c = read()) {
if (c == '-') iosig = true;
if (c == -1) return false;
}
for (x = 0; isdigit(c); c = read())
x = (x + (x << 2) << 1) + (c ^ '0');
if (iosig) x = -x;
return true;
}

const int OUT_LEN = 10000000;
char obuf[OUT_LEN], *oh = obuf;

inline void print(char c) {
if (oh == obuf + OUT_LEN) fwrite(obuf, 1, OUT_LEN, stdout), oh = obuf;
*oh++ = c;
}

template<class T>
inline void print(T x) {
static int buf[30], cnt;
if (x == 0) {
print('0');
} else {
if (x < 0) print('-'), x = -x;
for (cnt = 0; x; x /= 10) buf[++cnt] = x % 10 + 48;
while (cnt) print((char)buf[cnt--]);
}
}

inline void flush() {
fwrite(obuf, 1, oh - obuf, stdout);
}

typedef unsigned long long ull;
typedef unsigned int uint;
#define long long long

const int MAXN = 100000;
const int MAX_VAL = 30000;
uint v[MAXN], bucL[MAX_VAL + 1], bucR[MAX_VAL + 1];

namespace Concurrent {

/*concurrent*/
inline void concurrentSolve(int n, int num1) {
/* real register %esp %ebp %eax */
register int i, tmp;
bucR[MAX_VAL - (v[0] = num1)]++;
for (i = 1; i < n; i++)
read(tmp), bucR[MAX_VAL - (v[i] = tmp)]++;

bucR[MAX_VAL - v[0]]--;
register int minL = v[0], maxL = v[0];

register long ans = 0;

n--;
for (i = 1; i < n; i++) {
register int last = v[i - 1], cur = v[i];
if (last < minL) minL = last;
else if (last > maxL) maxL = last;

bucL[last]++;
bucR[MAX_VAL - cur]--;

register int bufx = cur << 1, low = std::max(minL, bufx - MAX_VAL),
high = std::min(maxL, bufx - 1);
/*CPU 并发优化*/
register uint tmp = 0, *p1 = bucL + low, *pr = bucL + high - 14,
*p2 = bucR + MAX_VAL - bufx + low;
/*循环展开 + 刺激并发*/
while (p1 <= pr) {
tmp += (*p1) * (*p2) + (*(p1 + 1)) * (*(p2 + 1)) + (*(p1 + 2)) *
(*(p2 + 2)) + (*(p1 + 3)) * (*(p2 + 3)) + (*(p1 + 4)) * (*(p2 + 4))
+ (*(p1 + 5)) * (*(p2 + 5)) + (*(p1 + 6)) * (*(p2 + 6)) + (*(p1 + 7))
* (*(p2 + 7)) + (*(p1 + 8)) * (*(p2 + 8)) + (*(p1 + 9)) * (*(p2 + 9))
+ (*(p1 + 10)) * (*(p2 + 10)) + (*(p1 + 11)) * (*(p2 + 11))
+ (*(p1 + 12)) * (*(p2 + 12)) + (*(p1 + 13)) * (*(p2 + 13))
+ (*(p1 + 14)) * (*(p2 + 14));

p1 += 15, p2 += 15;
}
while (p1 <= bucL + high) tmp += (*(p1++)) * (*(p2++));
ans += tmp;
}

print(ans);
}

}

namespace FastFourierTransform {

struct Complex {
double r, i;

Complex(double r = 0, double i = 0) : r(r), i(i) {}

inline Complex operator+(const Complex &x) const {
return Complex(r + x.r, i + x.i);
}

inline Complex operator-(const Complex &x) const {
return Complex(r - x.r, i - x.i);
}

inline Complex operator*(const Complex &x) const {
return Complex(r * x.r - i * x.i, r * x.i + i * x.r);
}

inline Complex conj() {
return Complex(r, -i);
}
};

const double PI = acos(-1);

inline void fft(Complex *a, const int n, const int f) {
for (register int i = 0, j = 0; i < n; i++) {
if (i > j) std::swap(a[i], a[j]);
for (register int k = n >> 1; (j ^= k) < k; k >>= 1);
}
for (register int i = 1; i < n; i <<= 1) {
Complex wn(cos(PI / i), f * sin(PI / i));
for (register int j = 0; j < n; j += i << 1) {
Complex w(1, 0);
for (register int k = 0; k < i; k++, w = w * wn) {
Complex x = a[j + k], y = w * a[i + j + k];
a[j + k] = x + y, a[i + j + k] = x - y;
}
}
}
if (f == -1) for (register int i = 0; i < n; i++) a[i].r /= n;
}

const int MAXN = 700010;
Complex a[MAXN], b[MAXN];
int num[MAXN], l[MAXN], r[MAXN], st[MAXN], ed[MAXN];
int n;

inline void solve(int n, int num1) {
num[1] = num1;
register int max = num1;
r[num1]++;
for (register int i = 2; i <= n; i++)
read(num[i]), max = std::max(max, num[i]), r[num[i]]++;
max++;
max = (max << 1) - 1;
for (FastFourierTransform::n = 1; FastFourierTransform::n <= max;
FastFourierTransform::n <<= 1);
register int size = 2000;
register int m = (n - 1) / size + 1;
for (register int i = 1; i <= m; i++) {
st[i] = ed[i - 1] + 1;
ed[i] = i * size;
}
ed[m] = n;
register long ans = 0;
for (register int i = 1; i <= m; i++) {
for (register int j = st[i]; j <= ed[i]; j++)
r[num[j]]--;
for (register int j = 0; j < FastFourierTransform::n; j++)
b[j] = Complex(l[j], r[j]);
fft(b, FastFourierTransform::n, 1);
for (register int i = 0, j; i < FastFourierTransform::n; i++) {
j = (FastFourierTransform::n - i) &
(FastFourierTransform::n - 1),
a[i] = (b[i] * b[i] - (b[j] * b[j]).conj())
* Complex(0, -0.25);
}
fft(a, FastFourierTransform::n, -1);
for (register int j = st[i]; j <= ed[i]; j++)
ans += ((long)(a[2 * num[j]].r + 0.5));
for (register int j = st[i]; j <= ed[i]; j++) {
for (register int k = st[i]; k < j; k++)
if (2 * num[j] - num[k] >= 0)
ans += r[2 * num[j] - num[k]];
for (register int k = j + 1; k <= ed[i]; k++)
if (2 * num[j] - num[k] >= 0)
ans += l[2 * num[j] - num[k]];
l[num[j]]++;
}
}
print(ans);
}
}

int main() {
#ifndef ONLINE_JUDGE
freopen("in.in", "r", stdin);
#endif
register int n, num1;
read(n), read(num1);
if ((n <= 100000 && num1 <= 50 && num1 != 1) || (n <= 5000)) {
Concurrent::concurrentSolve(n, num1);
} else {
FastFourierTransform::solve(n, num1);
}
flush();
return 0;
}
#

Comments

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×