#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
const int M = 2 * N;
const int B = sqrt(N / 2) / 2;
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 |
1 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 |
1 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 |
1 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 |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
2 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
6 ms |
340 KB |
Output is correct |
10 |
Correct |
5 ms |
340 KB |
Output is correct |
11 |
Correct |
5 ms |
340 KB |
Output is correct |
12 |
Correct |
5 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 |
5 ms |
340 KB |
Output is correct |
16 |
Correct |
5 ms |
340 KB |
Output is correct |
17 |
Correct |
4 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
283 ms |
1976 KB |
Output is correct |
2 |
Correct |
354 ms |
2124 KB |
Output is correct |
3 |
Correct |
215 ms |
1360 KB |
Output is correct |
4 |
Correct |
426 ms |
2124 KB |
Output is correct |
5 |
Correct |
409 ms |
2184 KB |
Output is correct |
6 |
Correct |
453 ms |
2252 KB |
Output is correct |
7 |
Correct |
453 ms |
2252 KB |
Output is correct |
8 |
Correct |
441 ms |
2260 KB |
Output is correct |
9 |
Correct |
445 ms |
2252 KB |
Output is correct |
10 |
Correct |
452 ms |
2244 KB |
Output is correct |
11 |
Correct |
400 ms |
2252 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 |
1 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 |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
2 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
6 ms |
340 KB |
Output is correct |
10 |
Correct |
5 ms |
340 KB |
Output is correct |
11 |
Correct |
5 ms |
340 KB |
Output is correct |
12 |
Correct |
5 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 |
5 ms |
340 KB |
Output is correct |
16 |
Correct |
5 ms |
340 KB |
Output is correct |
17 |
Correct |
4 ms |
340 KB |
Output is correct |
18 |
Correct |
283 ms |
1976 KB |
Output is correct |
19 |
Correct |
354 ms |
2124 KB |
Output is correct |
20 |
Correct |
215 ms |
1360 KB |
Output is correct |
21 |
Correct |
426 ms |
2124 KB |
Output is correct |
22 |
Correct |
409 ms |
2184 KB |
Output is correct |
23 |
Correct |
453 ms |
2252 KB |
Output is correct |
24 |
Correct |
453 ms |
2252 KB |
Output is correct |
25 |
Correct |
441 ms |
2260 KB |
Output is correct |
26 |
Correct |
445 ms |
2252 KB |
Output is correct |
27 |
Correct |
452 ms |
2244 KB |
Output is correct |
28 |
Correct |
400 ms |
2252 KB |
Output is correct |
29 |
Correct |
425 ms |
2148 KB |
Output is correct |
30 |
Correct |
79 ms |
852 KB |
Output is correct |
31 |
Correct |
167 ms |
1228 KB |
Output is correct |
32 |
Correct |
407 ms |
2152 KB |
Output is correct |
33 |
Correct |
166 ms |
1280 KB |
Output is correct |
34 |
Correct |
179 ms |
1304 KB |
Output is correct |
35 |
Correct |
108 ms |
1012 KB |
Output is correct |
36 |
Correct |
67 ms |
796 KB |
Output is correct |
37 |
Correct |
87 ms |
876 KB |
Output is correct |
38 |
Correct |
426 ms |
2168 KB |
Output is correct |
39 |
Correct |
441 ms |
2252 KB |
Output is correct |
40 |
Correct |
418 ms |
2212 KB |
Output is correct |
41 |
Correct |
424 ms |
2252 KB |
Output is correct |
42 |
Correct |
432 ms |
2168 KB |
Output is correct |
43 |
Correct |
482 ms |
2252 KB |
Output is correct |
44 |
Correct |
525 ms |
2228 KB |
Output is correct |
45 |
Correct |
510 ms |
2116 KB |
Output is correct |
46 |
Correct |
488 ms |
2240 KB |
Output is correct |
47 |
Correct |
487 ms |
2252 KB |
Output is correct |
48 |
Correct |
2421 ms |
2160 KB |
Output is correct |
49 |
Correct |
2358 ms |
2252 KB |
Output is correct |
50 |
Correct |
1160 ms |
2252 KB |
Output is correct |
51 |
Correct |
1173 ms |
2252 KB |
Output is correct |
52 |
Execution timed out |
3050 ms |
2220 KB |
Time limit exceeded |
53 |
Halted |
0 ms |
0 KB |
- |