#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
#define fast ios::sync_with_stdio(0);cin.tie(0)
typedef long long ll;
#define f first
#define s second
#define LOGN 21
const ll MOD = 1e9 + 7;
const ll MAXN = 2e5 + 100;
#define int long long
vector<int> A, cc, occ[MAXN];
signed main() {
fast;
int n;
cin >> n;
A = vector<int>(n+1);
for (int i = 1; i <= n; i++) {
cin >> A[i];
cc.push_back(A[i]);
}
sort(cc.begin(), cc.end());
cc.erase(unique(cc.begin(), cc.end()), cc.end());
int N = cc.size();
for (int i = 1; i <= n; i++) {
A[i] = upper_bound(cc.begin(), cc.end(), A[i]) - cc.begin();
occ[A[i]].push_back(i);
}
int cnt = 0;
for (int i = 1; i <= N; i++) {
if (occ[i].size() <= 3000) {
for (int a = 0; a < occ[i].size(); a++) {
for (int b = a; b < occ[i].size(); b++) {
int positive = b - a + 1;
int now = - (occ[i][b] - occ[i][a] + 1) + 2 * positive - 1;
if (now < 0)
continue;
int left = (a == 0 ? occ[i][a] - 1 : occ[i][a] - occ[i][a-1] - 1);
int right = (b + 1 == occ[i].size() ? n - occ[i][b] : occ[i][b+1] - occ[i][b] - 1);
left = min(left, now);
right = min(right, now);
if (left > right)
swap(left, right);
if (left + right <= now)
cnt += (left + 1) * (right + 1);
else {
cnt += (right + 1) * (right + 2) / 2 + (now - right) * (right + 1);
cnt -= (now - left) * (now - left + 1) / 2;
}
}
}
} else {
vector<int> B(n+1);
for (int j = 1; j <= n; j++)
B[j] = B[j-1] + (A[j] == i ? 1 : -1);
tree<pair<int,int>, null_type, less<pair<int,int>>, rb_tree_tag, tree_order_statistics_node_update> os;
os.insert({0, 0});
for (int j = 1; j <= n; j++) {
cnt += os.order_of_key({B[j], 0});
os.insert({B[j], j});
}
}
}
cout << cnt << "\n";
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:40:31: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
40 | for (int a = 0; a < occ[i].size(); a++) {
| ~~^~~~~~~~~~~~~~~
Main.cpp:41:35: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
41 | for (int b = a; b < occ[i].size(); b++) {
| ~~^~~~~~~~~~~~~~~
Main.cpp:49:40: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
49 | int right = (b + 1 == occ[i].size() ? n - occ[i][b] : occ[i][b+1] - occ[i][b] - 1);
| ~~~~~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4952 KB |
Output is correct |
2 |
Correct |
1 ms |
4952 KB |
Output is correct |
3 |
Correct |
1 ms |
4956 KB |
Output is correct |
4 |
Correct |
1 ms |
4956 KB |
Output is correct |
5 |
Correct |
1 ms |
5164 KB |
Output is correct |
6 |
Correct |
1 ms |
4956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4952 KB |
Output is correct |
2 |
Correct |
1 ms |
4952 KB |
Output is correct |
3 |
Correct |
1 ms |
4956 KB |
Output is correct |
4 |
Correct |
1 ms |
4956 KB |
Output is correct |
5 |
Correct |
1 ms |
5164 KB |
Output is correct |
6 |
Correct |
1 ms |
4956 KB |
Output is correct |
7 |
Correct |
2 ms |
4956 KB |
Output is correct |
8 |
Correct |
1 ms |
4956 KB |
Output is correct |
9 |
Correct |
1 ms |
5212 KB |
Output is correct |
10 |
Correct |
2 ms |
5212 KB |
Output is correct |
11 |
Correct |
2 ms |
5212 KB |
Output is correct |
12 |
Correct |
2 ms |
5212 KB |
Output is correct |
13 |
Correct |
2 ms |
5212 KB |
Output is correct |
14 |
Correct |
2 ms |
5212 KB |
Output is correct |
15 |
Correct |
2 ms |
5212 KB |
Output is correct |
16 |
Correct |
2 ms |
5212 KB |
Output is correct |
17 |
Correct |
8 ms |
5212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
81 ms |
18084 KB |
Output is correct |
2 |
Correct |
111 ms |
21448 KB |
Output is correct |
3 |
Correct |
67 ms |
14576 KB |
Output is correct |
4 |
Correct |
116 ms |
22472 KB |
Output is correct |
5 |
Correct |
134 ms |
23168 KB |
Output is correct |
6 |
Correct |
131 ms |
23944 KB |
Output is correct |
7 |
Correct |
126 ms |
23756 KB |
Output is correct |
8 |
Correct |
136 ms |
23756 KB |
Output is correct |
9 |
Correct |
124 ms |
24012 KB |
Output is correct |
10 |
Correct |
142 ms |
24008 KB |
Output is correct |
11 |
Correct |
164 ms |
23884 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4952 KB |
Output is correct |
2 |
Correct |
1 ms |
4952 KB |
Output is correct |
3 |
Correct |
1 ms |
4956 KB |
Output is correct |
4 |
Correct |
1 ms |
4956 KB |
Output is correct |
5 |
Correct |
1 ms |
5164 KB |
Output is correct |
6 |
Correct |
1 ms |
4956 KB |
Output is correct |
7 |
Correct |
2 ms |
4956 KB |
Output is correct |
8 |
Correct |
1 ms |
4956 KB |
Output is correct |
9 |
Correct |
1 ms |
5212 KB |
Output is correct |
10 |
Correct |
2 ms |
5212 KB |
Output is correct |
11 |
Correct |
2 ms |
5212 KB |
Output is correct |
12 |
Correct |
2 ms |
5212 KB |
Output is correct |
13 |
Correct |
2 ms |
5212 KB |
Output is correct |
14 |
Correct |
2 ms |
5212 KB |
Output is correct |
15 |
Correct |
2 ms |
5212 KB |
Output is correct |
16 |
Correct |
2 ms |
5212 KB |
Output is correct |
17 |
Correct |
8 ms |
5212 KB |
Output is correct |
18 |
Correct |
81 ms |
18084 KB |
Output is correct |
19 |
Correct |
111 ms |
21448 KB |
Output is correct |
20 |
Correct |
67 ms |
14576 KB |
Output is correct |
21 |
Correct |
116 ms |
22472 KB |
Output is correct |
22 |
Correct |
134 ms |
23168 KB |
Output is correct |
23 |
Correct |
131 ms |
23944 KB |
Output is correct |
24 |
Correct |
126 ms |
23756 KB |
Output is correct |
25 |
Correct |
136 ms |
23756 KB |
Output is correct |
26 |
Correct |
124 ms |
24012 KB |
Output is correct |
27 |
Correct |
142 ms |
24008 KB |
Output is correct |
28 |
Correct |
164 ms |
23884 KB |
Output is correct |
29 |
Correct |
167 ms |
23880 KB |
Output is correct |
30 |
Correct |
11 ms |
6616 KB |
Output is correct |
31 |
Correct |
23 ms |
8184 KB |
Output is correct |
32 |
Correct |
62 ms |
12096 KB |
Output is correct |
33 |
Correct |
25 ms |
8404 KB |
Output is correct |
34 |
Correct |
25 ms |
8668 KB |
Output is correct |
35 |
Correct |
16 ms |
7388 KB |
Output is correct |
36 |
Correct |
9 ms |
6620 KB |
Output is correct |
37 |
Correct |
11 ms |
6876 KB |
Output is correct |
38 |
Correct |
253 ms |
24108 KB |
Output is correct |
39 |
Correct |
263 ms |
24360 KB |
Output is correct |
40 |
Correct |
251 ms |
24008 KB |
Output is correct |
41 |
Correct |
252 ms |
24264 KB |
Output is correct |
42 |
Correct |
257 ms |
24268 KB |
Output is correct |
43 |
Correct |
505 ms |
24520 KB |
Output is correct |
44 |
Correct |
512 ms |
24520 KB |
Output is correct |
45 |
Correct |
510 ms |
24524 KB |
Output is correct |
46 |
Correct |
510 ms |
24640 KB |
Output is correct |
47 |
Correct |
514 ms |
24520 KB |
Output is correct |
48 |
Correct |
89 ms |
10196 KB |
Output is correct |
49 |
Correct |
97 ms |
10180 KB |
Output is correct |
50 |
Correct |
217 ms |
24528 KB |
Output is correct |
51 |
Correct |
227 ms |
25036 KB |
Output is correct |
52 |
Correct |
50 ms |
10696 KB |
Output is correct |
53 |
Correct |
64 ms |
10452 KB |
Output is correct |
54 |
Correct |
80 ms |
10704 KB |
Output is correct |