#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
const int M = 2 * N;
const int B = sqrt(N) / 4;
int n, a[N], c[M], cnt[N], occ[N];
void modify(int p, int v) {
for (p += N; p < M; p += p & -p) c[p] += v;
}
int query(int p) {
int res = 0;
for (p += N; p > 0; p -= p & -p) res += c[p];
return res;
}
void compress() {
vector<int> xs;
for (int i = 1; i <= n; i++)
xs.push_back(a[i]);
sort(xs.begin(), xs.end());
xs.erase(unique(xs.begin(), xs.end()), xs.end());
for (int i = 1; i <= n; i++) {
a[i] = (int) (lower_bound(xs.begin(), xs.end(), a[i]) - xs.begin()) + 1;
}
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
compress();
for (int i = 1; i <= n; i++) {
occ[a[i]] += 1;
}
long long ans = 0;
for (int v = 1; v <= n; v++) {
if (occ[v] >= B) {
modify(0, +1);
int bal = 0;
for (int i = 1; i <= n; i++) {
bal += (a[i] == v ? 1 : -1);
ans += query(bal - 1);
modify(bal, +1);
}
modify(0, -1);
bal = 0;
for (int i = 1; i <= n; i++) {
bal += (a[i] == v ? 1 : -1);
modify(bal, -1);
}
}
}
for (int l = 1; l <= n; l++) {
int f = a[l];
for (int r = l; r <= min(l + 2 * B, n); r++) {
cnt[a[r]] += 1;
if (cnt[a[r]] > cnt[f])
f = a[r];
if (cnt[f] * 2 > r - l + 1 && occ[f] < B)
ans++;
}
for (int r = l; r <= min(l + 2 * B, n); r++) {
cnt[a[r]] = 0;
}
}
printf("%lld", ans);
return 0;
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:26:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
26 | scanf("%d", &n);
| ~~~~~^~~~~~~~~~
Main.cpp:28:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
28 | scanf("%d", &a[i]);
| ~~~~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
2 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
2 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
2 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
2 ms |
340 KB |
Output is correct |
7 |
Correct |
2 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
5 ms |
340 KB |
Output is correct |
10 |
Correct |
7 ms |
340 KB |
Output is correct |
11 |
Correct |
5 ms |
368 KB |
Output is correct |
12 |
Correct |
4 ms |
340 KB |
Output is correct |
13 |
Correct |
5 ms |
340 KB |
Output is correct |
14 |
Correct |
5 ms |
340 KB |
Output is correct |
15 |
Correct |
6 ms |
340 KB |
Output is correct |
16 |
Correct |
6 ms |
376 KB |
Output is correct |
17 |
Correct |
4 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
211 ms |
1996 KB |
Output is correct |
2 |
Correct |
304 ms |
2112 KB |
Output is correct |
3 |
Correct |
224 ms |
1240 KB |
Output is correct |
4 |
Correct |
300 ms |
2124 KB |
Output is correct |
5 |
Correct |
302 ms |
2168 KB |
Output is correct |
6 |
Correct |
316 ms |
2252 KB |
Output is correct |
7 |
Correct |
312 ms |
2176 KB |
Output is correct |
8 |
Correct |
334 ms |
2132 KB |
Output is correct |
9 |
Correct |
317 ms |
2252 KB |
Output is correct |
10 |
Correct |
345 ms |
2180 KB |
Output is correct |
11 |
Correct |
283 ms |
2192 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
2 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
2 ms |
340 KB |
Output is correct |
7 |
Correct |
2 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
5 ms |
340 KB |
Output is correct |
10 |
Correct |
7 ms |
340 KB |
Output is correct |
11 |
Correct |
5 ms |
368 KB |
Output is correct |
12 |
Correct |
4 ms |
340 KB |
Output is correct |
13 |
Correct |
5 ms |
340 KB |
Output is correct |
14 |
Correct |
5 ms |
340 KB |
Output is correct |
15 |
Correct |
6 ms |
340 KB |
Output is correct |
16 |
Correct |
6 ms |
376 KB |
Output is correct |
17 |
Correct |
4 ms |
340 KB |
Output is correct |
18 |
Correct |
211 ms |
1996 KB |
Output is correct |
19 |
Correct |
304 ms |
2112 KB |
Output is correct |
20 |
Correct |
224 ms |
1240 KB |
Output is correct |
21 |
Correct |
300 ms |
2124 KB |
Output is correct |
22 |
Correct |
302 ms |
2168 KB |
Output is correct |
23 |
Correct |
316 ms |
2252 KB |
Output is correct |
24 |
Correct |
312 ms |
2176 KB |
Output is correct |
25 |
Correct |
334 ms |
2132 KB |
Output is correct |
26 |
Correct |
317 ms |
2252 KB |
Output is correct |
27 |
Correct |
345 ms |
2180 KB |
Output is correct |
28 |
Correct |
283 ms |
2192 KB |
Output is correct |
29 |
Correct |
334 ms |
2252 KB |
Output is correct |
30 |
Correct |
60 ms |
964 KB |
Output is correct |
31 |
Correct |
121 ms |
1232 KB |
Output is correct |
32 |
Correct |
309 ms |
2132 KB |
Output is correct |
33 |
Correct |
141 ms |
1284 KB |
Output is correct |
34 |
Correct |
128 ms |
1324 KB |
Output is correct |
35 |
Correct |
89 ms |
1020 KB |
Output is correct |
36 |
Correct |
51 ms |
848 KB |
Output is correct |
37 |
Correct |
104 ms |
880 KB |
Output is correct |
38 |
Correct |
461 ms |
2156 KB |
Output is correct |
39 |
Correct |
346 ms |
2216 KB |
Output is correct |
40 |
Correct |
335 ms |
2188 KB |
Output is correct |
41 |
Correct |
304 ms |
2252 KB |
Output is correct |
42 |
Correct |
341 ms |
2212 KB |
Output is correct |
43 |
Correct |
386 ms |
2144 KB |
Output is correct |
44 |
Correct |
380 ms |
2252 KB |
Output is correct |
45 |
Correct |
448 ms |
2160 KB |
Output is correct |
46 |
Correct |
412 ms |
2260 KB |
Output is correct |
47 |
Correct |
431 ms |
2252 KB |
Output is correct |
48 |
Correct |
2670 ms |
2252 KB |
Output is correct |
49 |
Correct |
2495 ms |
2164 KB |
Output is correct |
50 |
Correct |
1160 ms |
2252 KB |
Output is correct |
51 |
Correct |
1083 ms |
2252 KB |
Output is correct |
52 |
Execution timed out |
3084 ms |
2236 KB |
Time limit exceeded |
53 |
Halted |
0 ms |
0 KB |
- |