#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
const int M = 2 * N;
const int B = sqrt(N * 3);
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 |
212 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 |
212 KB |
Output is correct |
7 |
Correct |
4 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
13 ms |
340 KB |
Output is correct |
10 |
Correct |
12 ms |
340 KB |
Output is correct |
11 |
Correct |
13 ms |
340 KB |
Output is correct |
12 |
Correct |
12 ms |
344 KB |
Output is correct |
13 |
Correct |
12 ms |
344 KB |
Output is correct |
14 |
Correct |
12 ms |
340 KB |
Output is correct |
15 |
Correct |
12 ms |
344 KB |
Output is correct |
16 |
Correct |
12 ms |
340 KB |
Output is correct |
17 |
Correct |
12 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1221 ms |
1996 KB |
Output is correct |
2 |
Correct |
1624 ms |
2124 KB |
Output is correct |
3 |
Correct |
947 ms |
1360 KB |
Output is correct |
4 |
Correct |
1731 ms |
2124 KB |
Output is correct |
5 |
Correct |
1787 ms |
2124 KB |
Output is correct |
6 |
Correct |
1890 ms |
2252 KB |
Output is correct |
7 |
Correct |
1902 ms |
2252 KB |
Output is correct |
8 |
Correct |
1919 ms |
2212 KB |
Output is correct |
9 |
Correct |
1878 ms |
2140 KB |
Output is correct |
10 |
Correct |
1889 ms |
2252 KB |
Output is correct |
11 |
Correct |
1827 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 |
212 KB |
Output is correct |
7 |
Correct |
4 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
13 ms |
340 KB |
Output is correct |
10 |
Correct |
12 ms |
340 KB |
Output is correct |
11 |
Correct |
13 ms |
340 KB |
Output is correct |
12 |
Correct |
12 ms |
344 KB |
Output is correct |
13 |
Correct |
12 ms |
344 KB |
Output is correct |
14 |
Correct |
12 ms |
340 KB |
Output is correct |
15 |
Correct |
12 ms |
344 KB |
Output is correct |
16 |
Correct |
12 ms |
340 KB |
Output is correct |
17 |
Correct |
12 ms |
340 KB |
Output is correct |
18 |
Correct |
1221 ms |
1996 KB |
Output is correct |
19 |
Correct |
1624 ms |
2124 KB |
Output is correct |
20 |
Correct |
947 ms |
1360 KB |
Output is correct |
21 |
Correct |
1731 ms |
2124 KB |
Output is correct |
22 |
Correct |
1787 ms |
2124 KB |
Output is correct |
23 |
Correct |
1890 ms |
2252 KB |
Output is correct |
24 |
Correct |
1902 ms |
2252 KB |
Output is correct |
25 |
Correct |
1919 ms |
2212 KB |
Output is correct |
26 |
Correct |
1878 ms |
2140 KB |
Output is correct |
27 |
Correct |
1889 ms |
2252 KB |
Output is correct |
28 |
Correct |
1827 ms |
2252 KB |
Output is correct |
29 |
Correct |
1960 ms |
2184 KB |
Output is correct |
30 |
Correct |
350 ms |
852 KB |
Output is correct |
31 |
Correct |
723 ms |
1252 KB |
Output is correct |
32 |
Correct |
1755 ms |
2124 KB |
Output is correct |
33 |
Correct |
741 ms |
1304 KB |
Output is correct |
34 |
Correct |
767 ms |
1308 KB |
Output is correct |
35 |
Correct |
492 ms |
1020 KB |
Output is correct |
36 |
Correct |
291 ms |
724 KB |
Output is correct |
37 |
Correct |
361 ms |
876 KB |
Output is correct |
38 |
Correct |
1769 ms |
2252 KB |
Output is correct |
39 |
Correct |
1808 ms |
2252 KB |
Output is correct |
40 |
Correct |
1806 ms |
2252 KB |
Output is correct |
41 |
Correct |
1808 ms |
2252 KB |
Output is correct |
42 |
Correct |
1791 ms |
2252 KB |
Output is correct |
43 |
Correct |
1831 ms |
2252 KB |
Output is correct |
44 |
Correct |
1840 ms |
2252 KB |
Output is correct |
45 |
Correct |
1835 ms |
2116 KB |
Output is correct |
46 |
Correct |
1836 ms |
2252 KB |
Output is correct |
47 |
Correct |
1829 ms |
2252 KB |
Output is correct |
48 |
Execution timed out |
3061 ms |
2252 KB |
Time limit exceeded |
49 |
Halted |
0 ms |
0 KB |
- |