#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
const int M = 2 * N;
const int B = 2 * sqrt(N);
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]);
| ~~~~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
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 |
# |
결과 |
실행 시간 |
메모리 |
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 |
3 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
13 ms |
340 KB |
Output is correct |
10 |
Correct |
12 ms |
344 KB |
Output is correct |
11 |
Correct |
9 ms |
340 KB |
Output is correct |
12 |
Correct |
12 ms |
340 KB |
Output is correct |
13 |
Correct |
12 ms |
344 KB |
Output is correct |
14 |
Correct |
12 ms |
340 KB |
Output is correct |
15 |
Correct |
13 ms |
348 KB |
Output is correct |
16 |
Correct |
13 ms |
340 KB |
Output is correct |
17 |
Correct |
13 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1439 ms |
1996 KB |
Output is correct |
2 |
Correct |
1868 ms |
2108 KB |
Output is correct |
3 |
Correct |
1120 ms |
1360 KB |
Output is correct |
4 |
Correct |
1961 ms |
2124 KB |
Output is correct |
5 |
Correct |
2043 ms |
2124 KB |
Output is correct |
6 |
Correct |
2218 ms |
2252 KB |
Output is correct |
7 |
Correct |
2183 ms |
2188 KB |
Output is correct |
8 |
Correct |
2198 ms |
2252 KB |
Output is correct |
9 |
Correct |
2154 ms |
2252 KB |
Output is correct |
10 |
Correct |
2201 ms |
2252 KB |
Output is correct |
11 |
Correct |
2077 ms |
2232 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
3 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
13 ms |
340 KB |
Output is correct |
10 |
Correct |
12 ms |
344 KB |
Output is correct |
11 |
Correct |
9 ms |
340 KB |
Output is correct |
12 |
Correct |
12 ms |
340 KB |
Output is correct |
13 |
Correct |
12 ms |
344 KB |
Output is correct |
14 |
Correct |
12 ms |
340 KB |
Output is correct |
15 |
Correct |
13 ms |
348 KB |
Output is correct |
16 |
Correct |
13 ms |
340 KB |
Output is correct |
17 |
Correct |
13 ms |
376 KB |
Output is correct |
18 |
Correct |
1439 ms |
1996 KB |
Output is correct |
19 |
Correct |
1868 ms |
2108 KB |
Output is correct |
20 |
Correct |
1120 ms |
1360 KB |
Output is correct |
21 |
Correct |
1961 ms |
2124 KB |
Output is correct |
22 |
Correct |
2043 ms |
2124 KB |
Output is correct |
23 |
Correct |
2218 ms |
2252 KB |
Output is correct |
24 |
Correct |
2183 ms |
2188 KB |
Output is correct |
25 |
Correct |
2198 ms |
2252 KB |
Output is correct |
26 |
Correct |
2154 ms |
2252 KB |
Output is correct |
27 |
Correct |
2201 ms |
2252 KB |
Output is correct |
28 |
Correct |
2077 ms |
2232 KB |
Output is correct |
29 |
Correct |
2130 ms |
2124 KB |
Output is correct |
30 |
Correct |
390 ms |
860 KB |
Output is correct |
31 |
Correct |
794 ms |
1224 KB |
Output is correct |
32 |
Correct |
1954 ms |
2160 KB |
Output is correct |
33 |
Correct |
835 ms |
1272 KB |
Output is correct |
34 |
Correct |
853 ms |
1380 KB |
Output is correct |
35 |
Correct |
560 ms |
1024 KB |
Output is correct |
36 |
Correct |
330 ms |
852 KB |
Output is correct |
37 |
Correct |
404 ms |
876 KB |
Output is correct |
38 |
Correct |
2045 ms |
2252 KB |
Output is correct |
39 |
Correct |
2039 ms |
2252 KB |
Output is correct |
40 |
Correct |
2047 ms |
2228 KB |
Output is correct |
41 |
Correct |
2063 ms |
2252 KB |
Output is correct |
42 |
Correct |
2066 ms |
2252 KB |
Output is correct |
43 |
Correct |
2086 ms |
2236 KB |
Output is correct |
44 |
Correct |
2132 ms |
2252 KB |
Output is correct |
45 |
Correct |
2075 ms |
2216 KB |
Output is correct |
46 |
Correct |
2069 ms |
2172 KB |
Output is correct |
47 |
Correct |
2065 ms |
2264 KB |
Output is correct |
48 |
Execution timed out |
3058 ms |
2252 KB |
Time limit exceeded |
49 |
Halted |
0 ms |
0 KB |
- |