#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() <= 450) {
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 |
4956 KB |
Output is correct |
9 |
Correct |
2 ms |
5212 KB |
Output is correct |
10 |
Correct |
1 ms |
5220 KB |
Output is correct |
11 |
Correct |
1 ms |
5212 KB |
Output is correct |
12 |
Correct |
2 ms |
5212 KB |
Output is correct |
13 |
Correct |
1 ms |
5212 KB |
Output is correct |
14 |
Correct |
1 ms |
5212 KB |
Output is correct |
15 |
Correct |
3 ms |
5212 KB |
Output is correct |
16 |
Correct |
2 ms |
5212 KB |
Output is correct |
17 |
Correct |
2 ms |
5212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
84 ms |
18124 KB |
Output is correct |
2 |
Correct |
110 ms |
21496 KB |
Output is correct |
3 |
Correct |
62 ms |
14508 KB |
Output is correct |
4 |
Correct |
119 ms |
22484 KB |
Output is correct |
5 |
Correct |
119 ms |
23152 KB |
Output is correct |
6 |
Correct |
129 ms |
23756 KB |
Output is correct |
7 |
Correct |
127 ms |
23800 KB |
Output is correct |
8 |
Correct |
128 ms |
23756 KB |
Output is correct |
9 |
Correct |
124 ms |
24012 KB |
Output is correct |
10 |
Correct |
124 ms |
24104 KB |
Output is correct |
11 |
Correct |
165 ms |
23884 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 |
4956 KB |
Output is correct |
9 |
Correct |
2 ms |
5212 KB |
Output is correct |
10 |
Correct |
1 ms |
5220 KB |
Output is correct |
11 |
Correct |
1 ms |
5212 KB |
Output is correct |
12 |
Correct |
2 ms |
5212 KB |
Output is correct |
13 |
Correct |
1 ms |
5212 KB |
Output is correct |
14 |
Correct |
1 ms |
5212 KB |
Output is correct |
15 |
Correct |
3 ms |
5212 KB |
Output is correct |
16 |
Correct |
2 ms |
5212 KB |
Output is correct |
17 |
Correct |
2 ms |
5212 KB |
Output is correct |
18 |
Correct |
84 ms |
18124 KB |
Output is correct |
19 |
Correct |
110 ms |
21496 KB |
Output is correct |
20 |
Correct |
62 ms |
14508 KB |
Output is correct |
21 |
Correct |
119 ms |
22484 KB |
Output is correct |
22 |
Correct |
119 ms |
23152 KB |
Output is correct |
23 |
Correct |
129 ms |
23756 KB |
Output is correct |
24 |
Correct |
127 ms |
23800 KB |
Output is correct |
25 |
Correct |
128 ms |
23756 KB |
Output is correct |
26 |
Correct |
124 ms |
24012 KB |
Output is correct |
27 |
Correct |
124 ms |
24104 KB |
Output is correct |
28 |
Correct |
165 ms |
23884 KB |
Output is correct |
29 |
Correct |
179 ms |
23880 KB |
Output is correct |
30 |
Correct |
12 ms |
6616 KB |
Output is correct |
31 |
Correct |
22 ms |
8152 KB |
Output is correct |
32 |
Correct |
57 ms |
11980 KB |
Output is correct |
33 |
Correct |
24 ms |
8404 KB |
Output is correct |
34 |
Correct |
26 ms |
8660 KB |
Output is correct |
35 |
Correct |
16 ms |
7384 KB |
Output is correct |
36 |
Correct |
10 ms |
6592 KB |
Output is correct |
37 |
Correct |
12 ms |
6872 KB |
Output is correct |
38 |
Correct |
254 ms |
24092 KB |
Output is correct |
39 |
Correct |
262 ms |
24272 KB |
Output is correct |
40 |
Correct |
282 ms |
24000 KB |
Output is correct |
41 |
Correct |
254 ms |
24348 KB |
Output is correct |
42 |
Correct |
252 ms |
24312 KB |
Output is correct |
43 |
Correct |
504 ms |
24524 KB |
Output is correct |
44 |
Correct |
504 ms |
24520 KB |
Output is correct |
45 |
Correct |
512 ms |
24520 KB |
Output is correct |
46 |
Correct |
504 ms |
24524 KB |
Output is correct |
47 |
Correct |
533 ms |
24520 KB |
Output is correct |
48 |
Execution timed out |
3063 ms |
24168 KB |
Time limit exceeded |
49 |
Halted |
0 ms |
0 KB |
- |