#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() <= 2000) {
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 |
4956 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 |
4956 KB |
Output is correct |
5 |
Correct |
1 ms |
4956 KB |
Output is correct |
6 |
Correct |
1 ms |
4956 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4956 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 |
4956 KB |
Output is correct |
5 |
Correct |
1 ms |
4956 KB |
Output is correct |
6 |
Correct |
1 ms |
4956 KB |
Output is correct |
7 |
Correct |
1 ms |
4956 KB |
Output is correct |
8 |
Correct |
1 ms |
4952 KB |
Output is correct |
9 |
Correct |
1 ms |
5212 KB |
Output is correct |
10 |
Correct |
1 ms |
5212 KB |
Output is correct |
11 |
Correct |
1 ms |
5212 KB |
Output is correct |
12 |
Correct |
1 ms |
5212 KB |
Output is correct |
13 |
Correct |
1 ms |
5212 KB |
Output is correct |
14 |
Correct |
2 ms |
5208 KB |
Output is correct |
15 |
Correct |
2 ms |
5212 KB |
Output is correct |
16 |
Correct |
1 ms |
5212 KB |
Output is correct |
17 |
Correct |
6 ms |
5208 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
89 ms |
18124 KB |
Output is correct |
2 |
Correct |
108 ms |
21704 KB |
Output is correct |
3 |
Correct |
63 ms |
14828 KB |
Output is correct |
4 |
Correct |
112 ms |
22840 KB |
Output is correct |
5 |
Correct |
121 ms |
23496 KB |
Output is correct |
6 |
Correct |
131 ms |
24328 KB |
Output is correct |
7 |
Correct |
124 ms |
24264 KB |
Output is correct |
8 |
Correct |
129 ms |
24268 KB |
Output is correct |
9 |
Correct |
127 ms |
24440 KB |
Output is correct |
10 |
Correct |
124 ms |
24520 KB |
Output is correct |
11 |
Correct |
168 ms |
24140 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4956 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 |
4956 KB |
Output is correct |
5 |
Correct |
1 ms |
4956 KB |
Output is correct |
6 |
Correct |
1 ms |
4956 KB |
Output is correct |
7 |
Correct |
1 ms |
4956 KB |
Output is correct |
8 |
Correct |
1 ms |
4952 KB |
Output is correct |
9 |
Correct |
1 ms |
5212 KB |
Output is correct |
10 |
Correct |
1 ms |
5212 KB |
Output is correct |
11 |
Correct |
1 ms |
5212 KB |
Output is correct |
12 |
Correct |
1 ms |
5212 KB |
Output is correct |
13 |
Correct |
1 ms |
5212 KB |
Output is correct |
14 |
Correct |
2 ms |
5208 KB |
Output is correct |
15 |
Correct |
2 ms |
5212 KB |
Output is correct |
16 |
Correct |
1 ms |
5212 KB |
Output is correct |
17 |
Correct |
6 ms |
5208 KB |
Output is correct |
18 |
Correct |
89 ms |
18124 KB |
Output is correct |
19 |
Correct |
108 ms |
21704 KB |
Output is correct |
20 |
Correct |
63 ms |
14828 KB |
Output is correct |
21 |
Correct |
112 ms |
22840 KB |
Output is correct |
22 |
Correct |
121 ms |
23496 KB |
Output is correct |
23 |
Correct |
131 ms |
24328 KB |
Output is correct |
24 |
Correct |
124 ms |
24264 KB |
Output is correct |
25 |
Correct |
129 ms |
24268 KB |
Output is correct |
26 |
Correct |
127 ms |
24440 KB |
Output is correct |
27 |
Correct |
124 ms |
24520 KB |
Output is correct |
28 |
Correct |
168 ms |
24140 KB |
Output is correct |
29 |
Correct |
167 ms |
25904 KB |
Output is correct |
30 |
Correct |
12 ms |
6872 KB |
Output is correct |
31 |
Correct |
23 ms |
8664 KB |
Output is correct |
32 |
Correct |
58 ms |
13248 KB |
Output is correct |
33 |
Correct |
26 ms |
8876 KB |
Output is correct |
34 |
Correct |
26 ms |
9232 KB |
Output is correct |
35 |
Correct |
17 ms |
7900 KB |
Output is correct |
36 |
Correct |
13 ms |
6872 KB |
Output is correct |
37 |
Correct |
12 ms |
7200 KB |
Output is correct |
38 |
Correct |
252 ms |
24520 KB |
Output is correct |
39 |
Correct |
255 ms |
24544 KB |
Output is correct |
40 |
Correct |
264 ms |
24520 KB |
Output is correct |
41 |
Correct |
251 ms |
24752 KB |
Output is correct |
42 |
Correct |
252 ms |
24532 KB |
Output is correct |
43 |
Correct |
508 ms |
26380 KB |
Output is correct |
44 |
Correct |
521 ms |
26640 KB |
Output is correct |
45 |
Correct |
506 ms |
26328 KB |
Output is correct |
46 |
Correct |
506 ms |
26568 KB |
Output is correct |
47 |
Correct |
503 ms |
26436 KB |
Output is correct |
48 |
Correct |
87 ms |
10964 KB |
Output is correct |
49 |
Correct |
88 ms |
10952 KB |
Output is correct |
50 |
Execution timed out |
3069 ms |
25036 KB |
Time limit exceeded |
51 |
Halted |
0 ms |
0 KB |
- |