#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
const int M = 2 * N;
const int B = sqrt(N) / 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 readint() {
char c = getchar();
while (c < '0' || c > '9')
c = getchar();
int x = 0;
while ('0' <= c && c <= '9')
x = x * 10 + c - '0', c = getchar();
return x;
}
int main() {
n = readint();
for (int i = 1; i <= n; i++) {
a[i] = readint();
}
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);
for (int i = 1; i < M; i++)
c[i] = 0;
}
}
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;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 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 |
1876 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 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 |
1876 KB |
Output is correct |
7 |
Correct |
2 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
5 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 |
6 ms |
344 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 |
6 ms |
1908 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
383 ms |
2600 KB |
Output is correct |
2 |
Correct |
498 ms |
2796 KB |
Output is correct |
3 |
Correct |
286 ms |
2488 KB |
Output is correct |
4 |
Correct |
529 ms |
2732 KB |
Output is correct |
5 |
Correct |
538 ms |
2724 KB |
Output is correct |
6 |
Correct |
563 ms |
2796 KB |
Output is correct |
7 |
Correct |
569 ms |
2764 KB |
Output is correct |
8 |
Correct |
579 ms |
2764 KB |
Output is correct |
9 |
Correct |
572 ms |
2852 KB |
Output is correct |
10 |
Correct |
572 ms |
2772 KB |
Output is correct |
11 |
Correct |
524 ms |
2776 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 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 |
1876 KB |
Output is correct |
7 |
Correct |
2 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
5 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 |
6 ms |
344 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 |
6 ms |
1908 KB |
Output is correct |
18 |
Correct |
383 ms |
2600 KB |
Output is correct |
19 |
Correct |
498 ms |
2796 KB |
Output is correct |
20 |
Correct |
286 ms |
2488 KB |
Output is correct |
21 |
Correct |
529 ms |
2732 KB |
Output is correct |
22 |
Correct |
538 ms |
2724 KB |
Output is correct |
23 |
Correct |
563 ms |
2796 KB |
Output is correct |
24 |
Correct |
569 ms |
2764 KB |
Output is correct |
25 |
Correct |
579 ms |
2764 KB |
Output is correct |
26 |
Correct |
572 ms |
2852 KB |
Output is correct |
27 |
Correct |
572 ms |
2772 KB |
Output is correct |
28 |
Correct |
524 ms |
2776 KB |
Output is correct |
29 |
Correct |
530 ms |
2764 KB |
Output is correct |
30 |
Correct |
110 ms |
852 KB |
Output is correct |
31 |
Correct |
208 ms |
1232 KB |
Output is correct |
32 |
Correct |
533 ms |
2124 KB |
Output is correct |
33 |
Correct |
219 ms |
1284 KB |
Output is correct |
34 |
Correct |
231 ms |
1412 KB |
Output is correct |
35 |
Correct |
158 ms |
1020 KB |
Output is correct |
36 |
Correct |
91 ms |
800 KB |
Output is correct |
37 |
Correct |
109 ms |
876 KB |
Output is correct |
38 |
Correct |
539 ms |
2760 KB |
Output is correct |
39 |
Correct |
541 ms |
2768 KB |
Output is correct |
40 |
Correct |
552 ms |
2760 KB |
Output is correct |
41 |
Correct |
539 ms |
2868 KB |
Output is correct |
42 |
Correct |
541 ms |
2768 KB |
Output is correct |
43 |
Correct |
580 ms |
2764 KB |
Output is correct |
44 |
Correct |
584 ms |
2764 KB |
Output is correct |
45 |
Correct |
578 ms |
2768 KB |
Output is correct |
46 |
Correct |
575 ms |
2760 KB |
Output is correct |
47 |
Correct |
607 ms |
2816 KB |
Output is correct |
48 |
Correct |
1608 ms |
2764 KB |
Output is correct |
49 |
Correct |
1624 ms |
2848 KB |
Output is correct |
50 |
Correct |
948 ms |
2884 KB |
Output is correct |
51 |
Correct |
969 ms |
3004 KB |
Output is correct |
52 |
Correct |
2655 ms |
2888 KB |
Output is correct |
53 |
Correct |
2463 ms |
3504 KB |
Output is correct |
54 |
Correct |
1625 ms |
3444 KB |
Output is correct |