#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef tree<
pair<int, int>,
null_type,
less<pair<int, int>>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;
typedef long long ll;
const int MAX = 2e5 + 2;
const int s = 447;
int a[MAX], p[MAX], cnt[MAX], cnt1[MAX], n;
ll res;
map<int, int> mp;
void solve() {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
mp[a[i]]++;
}
int m = 0;
for (auto i : mp) {
mp[i.first] = ++m;
}
for (int i = 1; i <= n; i++) {
a[i] = mp[a[i]];
cnt1[a[i]]++;
}
for (int i = 1; i <= n; i++) {
int mx = 0;
for (int j = i; j <= min(n, i + 2 * s - 1); j++) {
if (cnt1[a[j]] <= s) {
cnt[a[j]]++;
mx = max(mx, cnt[a[j]]);
if (mx > (j - i + 1 - mx)) res++;
}
}
for (int j = i; j <= min(n, i + 2 * s - 1); j++) {
if (cnt1[a[j]] <= s) cnt[a[j]]--;
}
}
for (int i = 1; i <= m; i++) {
if (cnt1[i] <= s) continue;
for (int j = 1; j <= n; j++) p[j] = p[j - 1] + (a[j] == i ? 1 : -1);
ordered_set st;
st.insert({0, 0});
for (int j = 1; j <= n; j++) {
res += st.order_of_key({p[j], 0});
st.insert({p[j], j});
}
}
cout << res;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
4 ms |
496 KB |
Output is correct |
10 |
Correct |
4 ms |
348 KB |
Output is correct |
11 |
Correct |
4 ms |
348 KB |
Output is correct |
12 |
Correct |
4 ms |
504 KB |
Output is correct |
13 |
Correct |
4 ms |
472 KB |
Output is correct |
14 |
Correct |
4 ms |
348 KB |
Output is correct |
15 |
Correct |
4 ms |
348 KB |
Output is correct |
16 |
Correct |
4 ms |
348 KB |
Output is correct |
17 |
Correct |
2 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
197 ms |
9940 KB |
Output is correct |
2 |
Correct |
264 ms |
12996 KB |
Output is correct |
3 |
Correct |
151 ms |
7764 KB |
Output is correct |
4 |
Correct |
270 ms |
13628 KB |
Output is correct |
5 |
Correct |
273 ms |
13908 KB |
Output is correct |
6 |
Correct |
299 ms |
14688 KB |
Output is correct |
7 |
Correct |
308 ms |
14928 KB |
Output is correct |
8 |
Correct |
299 ms |
14684 KB |
Output is correct |
9 |
Correct |
300 ms |
14924 KB |
Output is correct |
10 |
Correct |
296 ms |
14928 KB |
Output is correct |
11 |
Correct |
336 ms |
14684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
4 ms |
496 KB |
Output is correct |
10 |
Correct |
4 ms |
348 KB |
Output is correct |
11 |
Correct |
4 ms |
348 KB |
Output is correct |
12 |
Correct |
4 ms |
504 KB |
Output is correct |
13 |
Correct |
4 ms |
472 KB |
Output is correct |
14 |
Correct |
4 ms |
348 KB |
Output is correct |
15 |
Correct |
4 ms |
348 KB |
Output is correct |
16 |
Correct |
4 ms |
348 KB |
Output is correct |
17 |
Correct |
2 ms |
604 KB |
Output is correct |
18 |
Correct |
197 ms |
9940 KB |
Output is correct |
19 |
Correct |
264 ms |
12996 KB |
Output is correct |
20 |
Correct |
151 ms |
7764 KB |
Output is correct |
21 |
Correct |
270 ms |
13628 KB |
Output is correct |
22 |
Correct |
273 ms |
13908 KB |
Output is correct |
23 |
Correct |
299 ms |
14688 KB |
Output is correct |
24 |
Correct |
308 ms |
14928 KB |
Output is correct |
25 |
Correct |
299 ms |
14684 KB |
Output is correct |
26 |
Correct |
300 ms |
14924 KB |
Output is correct |
27 |
Correct |
296 ms |
14928 KB |
Output is correct |
28 |
Correct |
336 ms |
14684 KB |
Output is correct |
29 |
Correct |
337 ms |
16464 KB |
Output is correct |
30 |
Correct |
122 ms |
2732 KB |
Output is correct |
31 |
Correct |
255 ms |
6228 KB |
Output is correct |
32 |
Correct |
624 ms |
10320 KB |
Output is correct |
33 |
Correct |
266 ms |
6648 KB |
Output is correct |
34 |
Correct |
288 ms |
6736 KB |
Output is correct |
35 |
Correct |
176 ms |
5716 KB |
Output is correct |
36 |
Correct |
101 ms |
2392 KB |
Output is correct |
37 |
Correct |
132 ms |
2652 KB |
Output is correct |
38 |
Correct |
414 ms |
14928 KB |
Output is correct |
39 |
Correct |
400 ms |
14928 KB |
Output is correct |
40 |
Correct |
433 ms |
14928 KB |
Output is correct |
41 |
Correct |
422 ms |
14680 KB |
Output is correct |
42 |
Correct |
424 ms |
14788 KB |
Output is correct |
43 |
Correct |
621 ms |
16508 KB |
Output is correct |
44 |
Correct |
626 ms |
16468 KB |
Output is correct |
45 |
Correct |
631 ms |
16504 KB |
Output is correct |
46 |
Correct |
640 ms |
16500 KB |
Output is correct |
47 |
Correct |
670 ms |
16468 KB |
Output is correct |
48 |
Execution timed out |
3036 ms |
14976 KB |
Time limit exceeded |
49 |
Halted |
0 ms |
0 KB |
- |