#include <bits/stdc++.h>
using namespace std;
const int maxn = 200010;
int n, a[maxn], sum[maxn], c[maxn * 2];
long long ans;
map<int, vector<int>> mp;
void add(int p) {
for (p += maxn; p < maxn * 2; p += p & -p) c[p]++;
}
int query(int p) {
int s = 0;
for (p += maxn; p; p -= p & -p) s += c[p];
return s;
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]), mp[a[i]].push_back(i);
}
auto calc = [&](int x, int y, int lim) {
int t = min(x, max(0, y - lim));
return (2LL * lim + t + 1) * t / 2 + 1LL * (x - t) * y;
};
for (auto &p : mp) {
auto &pos = p.second;
if (pos.size() <= 1000) {
for (int i = 0; i < pos.size(); i++) {
for (int j = i; j < pos.size(); j++) {
int l = (i ? pos[i - 1] : 0) + 1;
int r = (j + 1 < pos.size() ? pos[j + 1] : n + 1) - 1;
int lim = 2 * (j - i);
ans += calc(pos[i], r, lim) + calc(l - 1, pos[j] - 1, lim);
ans -= calc(l - 1, r, lim) + calc(pos[i], pos[j] - 1, lim);
}
}
} else {
memset(sum, -1, sizeof(sum));
for (int x : pos) {
sum[x] = 1;
}
for (int i = 1; i <= n; i++) {
sum[i] += sum[i - 1];
}
memset(c, 0, sizeof(c));
for (int i = 1; i <= n; i++) {
add(sum[i - 1]), ans += query(sum[i] - 1);
}
}
}
printf("%lld\n", ans);
return 0;
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:31:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
31 | for (int i = 0; i < pos.size(); i++) {
| ~~^~~~~~~~~~~~
Main.cpp:32:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
32 | for (int j = i; j < pos.size(); j++) {
| ~~^~~~~~~~~~~~
Main.cpp:34:36: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
34 | int r = (j + 1 < pos.size() ? pos[j + 1] : n + 1) - 1;
| ~~~~~~^~~~~~~~~~~~
Main.cpp:20:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
20 | scanf("%d", &n);
| ~~~~~^~~~~~~~~~
Main.cpp:22:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
22 | scanf("%d", &a[i]), mp[a[i]].push_back(i);
| ~~~~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 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 |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
2 ms |
340 KB |
Output is correct |
10 |
Correct |
2 ms |
340 KB |
Output is correct |
11 |
Correct |
3 ms |
340 KB |
Output is correct |
12 |
Correct |
3 ms |
340 KB |
Output is correct |
13 |
Correct |
2 ms |
340 KB |
Output is correct |
14 |
Correct |
3 ms |
336 KB |
Output is correct |
15 |
Correct |
2 ms |
340 KB |
Output is correct |
16 |
Correct |
2 ms |
340 KB |
Output is correct |
17 |
Correct |
2 ms |
2644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
3780 KB |
Output is correct |
2 |
Correct |
28 ms |
4012 KB |
Output is correct |
3 |
Correct |
17 ms |
3576 KB |
Output is correct |
4 |
Correct |
31 ms |
4132 KB |
Output is correct |
5 |
Correct |
32 ms |
4212 KB |
Output is correct |
6 |
Correct |
34 ms |
4352 KB |
Output is correct |
7 |
Correct |
33 ms |
4408 KB |
Output is correct |
8 |
Correct |
32 ms |
4404 KB |
Output is correct |
9 |
Correct |
33 ms |
4368 KB |
Output is correct |
10 |
Correct |
33 ms |
4272 KB |
Output is correct |
11 |
Correct |
27 ms |
4244 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
2 ms |
340 KB |
Output is correct |
10 |
Correct |
2 ms |
340 KB |
Output is correct |
11 |
Correct |
3 ms |
340 KB |
Output is correct |
12 |
Correct |
3 ms |
340 KB |
Output is correct |
13 |
Correct |
2 ms |
340 KB |
Output is correct |
14 |
Correct |
3 ms |
336 KB |
Output is correct |
15 |
Correct |
2 ms |
340 KB |
Output is correct |
16 |
Correct |
2 ms |
340 KB |
Output is correct |
17 |
Correct |
2 ms |
2644 KB |
Output is correct |
18 |
Correct |
24 ms |
3780 KB |
Output is correct |
19 |
Correct |
28 ms |
4012 KB |
Output is correct |
20 |
Correct |
17 ms |
3576 KB |
Output is correct |
21 |
Correct |
31 ms |
4132 KB |
Output is correct |
22 |
Correct |
32 ms |
4212 KB |
Output is correct |
23 |
Correct |
34 ms |
4352 KB |
Output is correct |
24 |
Correct |
33 ms |
4408 KB |
Output is correct |
25 |
Correct |
32 ms |
4404 KB |
Output is correct |
26 |
Correct |
33 ms |
4368 KB |
Output is correct |
27 |
Correct |
33 ms |
4272 KB |
Output is correct |
28 |
Correct |
27 ms |
4244 KB |
Output is correct |
29 |
Correct |
38 ms |
4312 KB |
Output is correct |
30 |
Correct |
18 ms |
4180 KB |
Output is correct |
31 |
Correct |
43 ms |
7568 KB |
Output is correct |
32 |
Correct |
169 ms |
14420 KB |
Output is correct |
33 |
Correct |
49 ms |
7896 KB |
Output is correct |
34 |
Correct |
59 ms |
8188 KB |
Output is correct |
35 |
Correct |
29 ms |
5732 KB |
Output is correct |
36 |
Correct |
17 ms |
3724 KB |
Output is correct |
37 |
Correct |
20 ms |
4408 KB |
Output is correct |
38 |
Correct |
57 ms |
4556 KB |
Output is correct |
39 |
Correct |
56 ms |
4332 KB |
Output is correct |
40 |
Correct |
53 ms |
4400 KB |
Output is correct |
41 |
Correct |
56 ms |
4332 KB |
Output is correct |
42 |
Correct |
53 ms |
4560 KB |
Output is correct |
43 |
Correct |
100 ms |
4644 KB |
Output is correct |
44 |
Correct |
103 ms |
4564 KB |
Output is correct |
45 |
Correct |
103 ms |
4652 KB |
Output is correct |
46 |
Correct |
101 ms |
4568 KB |
Output is correct |
47 |
Correct |
102 ms |
4720 KB |
Output is correct |
48 |
Correct |
848 ms |
4444 KB |
Output is correct |
49 |
Correct |
829 ms |
4452 KB |
Output is correct |
50 |
Correct |
600 ms |
5032 KB |
Output is correct |
51 |
Correct |
604 ms |
4892 KB |
Output is correct |
52 |
Correct |
446 ms |
2132 KB |
Output is correct |
53 |
Correct |
482 ms |
1996 KB |
Output is correct |
54 |
Correct |
822 ms |
2036 KB |
Output is correct |