#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() <= 900) {
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);
| ~~~~~~^~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4952 KB |
Output is correct |
2 |
Correct |
1 ms |
4956 KB |
Output is correct |
3 |
Correct |
1 ms |
4956 KB |
Output is correct |
4 |
Correct |
1 ms |
4952 KB |
Output is correct |
5 |
Correct |
1 ms |
4964 KB |
Output is correct |
6 |
Correct |
1 ms |
4952 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4952 KB |
Output is correct |
2 |
Correct |
1 ms |
4956 KB |
Output is correct |
3 |
Correct |
1 ms |
4956 KB |
Output is correct |
4 |
Correct |
1 ms |
4952 KB |
Output is correct |
5 |
Correct |
1 ms |
4964 KB |
Output is correct |
6 |
Correct |
1 ms |
4952 KB |
Output is correct |
7 |
Correct |
1 ms |
4952 KB |
Output is correct |
8 |
Correct |
1 ms |
4956 KB |
Output is correct |
9 |
Correct |
1 ms |
5040 KB |
Output is correct |
10 |
Correct |
2 ms |
5208 KB |
Output is correct |
11 |
Correct |
1 ms |
5212 KB |
Output is correct |
12 |
Correct |
2 ms |
5208 KB |
Output is correct |
13 |
Correct |
1 ms |
5212 KB |
Output is correct |
14 |
Correct |
1 ms |
5212 KB |
Output is correct |
15 |
Correct |
2 ms |
5212 KB |
Output is correct |
16 |
Correct |
2 ms |
5220 KB |
Output is correct |
17 |
Correct |
2 ms |
5208 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
84 ms |
18124 KB |
Output is correct |
2 |
Correct |
112 ms |
21448 KB |
Output is correct |
3 |
Correct |
62 ms |
14576 KB |
Output is correct |
4 |
Correct |
115 ms |
22428 KB |
Output is correct |
5 |
Correct |
118 ms |
23048 KB |
Output is correct |
6 |
Correct |
132 ms |
23780 KB |
Output is correct |
7 |
Correct |
128 ms |
23752 KB |
Output is correct |
8 |
Correct |
140 ms |
23956 KB |
Output is correct |
9 |
Correct |
123 ms |
24108 KB |
Output is correct |
10 |
Correct |
134 ms |
24136 KB |
Output is correct |
11 |
Correct |
163 ms |
23816 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4952 KB |
Output is correct |
2 |
Correct |
1 ms |
4956 KB |
Output is correct |
3 |
Correct |
1 ms |
4956 KB |
Output is correct |
4 |
Correct |
1 ms |
4952 KB |
Output is correct |
5 |
Correct |
1 ms |
4964 KB |
Output is correct |
6 |
Correct |
1 ms |
4952 KB |
Output is correct |
7 |
Correct |
1 ms |
4952 KB |
Output is correct |
8 |
Correct |
1 ms |
4956 KB |
Output is correct |
9 |
Correct |
1 ms |
5040 KB |
Output is correct |
10 |
Correct |
2 ms |
5208 KB |
Output is correct |
11 |
Correct |
1 ms |
5212 KB |
Output is correct |
12 |
Correct |
2 ms |
5208 KB |
Output is correct |
13 |
Correct |
1 ms |
5212 KB |
Output is correct |
14 |
Correct |
1 ms |
5212 KB |
Output is correct |
15 |
Correct |
2 ms |
5212 KB |
Output is correct |
16 |
Correct |
2 ms |
5220 KB |
Output is correct |
17 |
Correct |
2 ms |
5208 KB |
Output is correct |
18 |
Correct |
84 ms |
18124 KB |
Output is correct |
19 |
Correct |
112 ms |
21448 KB |
Output is correct |
20 |
Correct |
62 ms |
14576 KB |
Output is correct |
21 |
Correct |
115 ms |
22428 KB |
Output is correct |
22 |
Correct |
118 ms |
23048 KB |
Output is correct |
23 |
Correct |
132 ms |
23780 KB |
Output is correct |
24 |
Correct |
128 ms |
23752 KB |
Output is correct |
25 |
Correct |
140 ms |
23956 KB |
Output is correct |
26 |
Correct |
123 ms |
24108 KB |
Output is correct |
27 |
Correct |
134 ms |
24136 KB |
Output is correct |
28 |
Correct |
163 ms |
23816 KB |
Output is correct |
29 |
Correct |
171 ms |
23892 KB |
Output is correct |
30 |
Correct |
13 ms |
6668 KB |
Output is correct |
31 |
Correct |
22 ms |
8240 KB |
Output is correct |
32 |
Correct |
61 ms |
11976 KB |
Output is correct |
33 |
Correct |
26 ms |
8348 KB |
Output is correct |
34 |
Correct |
26 ms |
8656 KB |
Output is correct |
35 |
Correct |
20 ms |
7388 KB |
Output is correct |
36 |
Correct |
10 ms |
6620 KB |
Output is correct |
37 |
Correct |
12 ms |
6704 KB |
Output is correct |
38 |
Correct |
275 ms |
24168 KB |
Output is correct |
39 |
Correct |
264 ms |
24196 KB |
Output is correct |
40 |
Correct |
263 ms |
24008 KB |
Output is correct |
41 |
Correct |
253 ms |
24268 KB |
Output is correct |
42 |
Correct |
262 ms |
24264 KB |
Output is correct |
43 |
Correct |
500 ms |
24524 KB |
Output is correct |
44 |
Correct |
510 ms |
24560 KB |
Output is correct |
45 |
Correct |
528 ms |
24380 KB |
Output is correct |
46 |
Correct |
509 ms |
24560 KB |
Output is correct |
47 |
Correct |
519 ms |
24516 KB |
Output is correct |
48 |
Correct |
136 ms |
23944 KB |
Output is correct |
49 |
Correct |
138 ms |
23944 KB |
Output is correct |
50 |
Execution timed out |
3038 ms |
24528 KB |
Time limit exceeded |
51 |
Halted |
0 ms |
0 KB |
- |