#pragma GCC optimize("Ofast")
#pragma GCC target("sse4")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int NM = 2e5, SZ = 500;
int n, a[NM+5], cnt[NM+5];
bool type[NM+5];
vector <int> v;
ll ans = 0;
int pref[NM+5];
int st[8*NM+5];
void compress(){
for (int i = 1; i <= n; i++)
v.push_back(a[i]);
sort(v.begin(), v.end());
for (int i = 1; i <= n; i++)
a[i] = lower_bound(v.begin(), v.end(), a[i])-v.begin();
}
void solve1(){
memset(cnt, 0, sizeof(cnt));
for (int i = 1; i <= n; i++){
int best = -1;
for (int j = i; j < min(i+2*SZ, n+1); j++){
if (type[a[j]] == 0) cnt[a[j]]++;
if (best == -1 || cnt[a[j]] > cnt[best]) best = a[j];
if (cnt[best]*2 > j-i+1){
ans++;
}
}
for (int j = i; j < min(i+2*SZ, n+1); j++)
if (type[a[j]] == 0) cnt[a[j]]--;
}
}
void update(int id, int l, int r, int i){
if (i < l || i > r) return;
if (l == r){
st[id]++;
return;
}
int mid = (l+r)/2;
update(2*id, l, mid, i);
update(2*id+1, mid+1, r, i);
st[id] = st[2*id]+st[2*id+1];
}
int get(int id, int l, int r, int u, int v){
if (u > v || v < l || u > r) return 0;
if (l >= u && r <= v) return st[id];
int mid = (l+r)/2;
return get(2*id, l, mid, u, v)+get(2*id+1, mid+1, r, u, v);
}
void solve2(int s){
pref[0] = 0;
for (int i = 1; i <= n; i++){
pref[i] = pref[i-1];
if (a[i] == s) pref[i]++;
}
memset(st, 0, sizeof(st));
update(1, 0, 2*n, n);
for (int i = 1; i <= n; i++){
ans += get(1, 0, 2*n, 0, 2*pref[i]-i-1+n);
update(1, 0, 2*n, 2*pref[i]-i+n);
}
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 1; i <= n; i++)
cin >> a[i];
compress();
for (int i = 1; i <= n; i++)
cnt[a[i]]++;
for (int i = 0; i < n; i++)
if (cnt[i] > SZ) type[i] = 1;
solve1();
for (int i = 0; i < n; i++)
if (type[i]) solve2(i);
cout << ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1128 KB |
Output is correct |
2 |
Correct |
1 ms |
1108 KB |
Output is correct |
3 |
Correct |
1 ms |
1108 KB |
Output is correct |
4 |
Correct |
1 ms |
1108 KB |
Output is correct |
5 |
Correct |
2 ms |
1108 KB |
Output is correct |
6 |
Correct |
1 ms |
1108 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1128 KB |
Output is correct |
2 |
Correct |
1 ms |
1108 KB |
Output is correct |
3 |
Correct |
1 ms |
1108 KB |
Output is correct |
4 |
Correct |
1 ms |
1108 KB |
Output is correct |
5 |
Correct |
2 ms |
1108 KB |
Output is correct |
6 |
Correct |
1 ms |
1108 KB |
Output is correct |
7 |
Correct |
4 ms |
1108 KB |
Output is correct |
8 |
Correct |
1 ms |
1108 KB |
Output is correct |
9 |
Correct |
8 ms |
1108 KB |
Output is correct |
10 |
Correct |
8 ms |
1144 KB |
Output is correct |
11 |
Correct |
8 ms |
1108 KB |
Output is correct |
12 |
Correct |
12 ms |
1108 KB |
Output is correct |
13 |
Correct |
12 ms |
1148 KB |
Output is correct |
14 |
Correct |
8 ms |
1108 KB |
Output is correct |
15 |
Correct |
12 ms |
1152 KB |
Output is correct |
16 |
Correct |
11 ms |
1108 KB |
Output is correct |
17 |
Correct |
9 ms |
7420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
369 ms |
9068 KB |
Output is correct |
2 |
Correct |
472 ms |
9688 KB |
Output is correct |
3 |
Correct |
280 ms |
8664 KB |
Output is correct |
4 |
Correct |
557 ms |
9764 KB |
Output is correct |
5 |
Correct |
552 ms |
9820 KB |
Output is correct |
6 |
Correct |
570 ms |
9912 KB |
Output is correct |
7 |
Correct |
536 ms |
9912 KB |
Output is correct |
8 |
Correct |
528 ms |
9916 KB |
Output is correct |
9 |
Correct |
559 ms |
9928 KB |
Output is correct |
10 |
Correct |
554 ms |
9928 KB |
Output is correct |
11 |
Correct |
495 ms |
9912 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1128 KB |
Output is correct |
2 |
Correct |
1 ms |
1108 KB |
Output is correct |
3 |
Correct |
1 ms |
1108 KB |
Output is correct |
4 |
Correct |
1 ms |
1108 KB |
Output is correct |
5 |
Correct |
2 ms |
1108 KB |
Output is correct |
6 |
Correct |
1 ms |
1108 KB |
Output is correct |
7 |
Correct |
4 ms |
1108 KB |
Output is correct |
8 |
Correct |
1 ms |
1108 KB |
Output is correct |
9 |
Correct |
8 ms |
1108 KB |
Output is correct |
10 |
Correct |
8 ms |
1144 KB |
Output is correct |
11 |
Correct |
8 ms |
1108 KB |
Output is correct |
12 |
Correct |
12 ms |
1108 KB |
Output is correct |
13 |
Correct |
12 ms |
1148 KB |
Output is correct |
14 |
Correct |
8 ms |
1108 KB |
Output is correct |
15 |
Correct |
12 ms |
1152 KB |
Output is correct |
16 |
Correct |
11 ms |
1108 KB |
Output is correct |
17 |
Correct |
9 ms |
7420 KB |
Output is correct |
18 |
Correct |
369 ms |
9068 KB |
Output is correct |
19 |
Correct |
472 ms |
9688 KB |
Output is correct |
20 |
Correct |
280 ms |
8664 KB |
Output is correct |
21 |
Correct |
557 ms |
9764 KB |
Output is correct |
22 |
Correct |
552 ms |
9820 KB |
Output is correct |
23 |
Correct |
570 ms |
9912 KB |
Output is correct |
24 |
Correct |
536 ms |
9912 KB |
Output is correct |
25 |
Correct |
528 ms |
9916 KB |
Output is correct |
26 |
Correct |
559 ms |
9928 KB |
Output is correct |
27 |
Correct |
554 ms |
9928 KB |
Output is correct |
28 |
Correct |
495 ms |
9912 KB |
Output is correct |
29 |
Correct |
504 ms |
9924 KB |
Output is correct |
30 |
Correct |
254 ms |
1496 KB |
Output is correct |
31 |
Correct |
557 ms |
1864 KB |
Output is correct |
32 |
Correct |
1306 ms |
2844 KB |
Output is correct |
33 |
Correct |
553 ms |
1900 KB |
Output is correct |
34 |
Correct |
587 ms |
2004 KB |
Output is correct |
35 |
Correct |
380 ms |
1624 KB |
Output is correct |
36 |
Correct |
224 ms |
1496 KB |
Output is correct |
37 |
Correct |
288 ms |
1496 KB |
Output is correct |
38 |
Correct |
661 ms |
9916 KB |
Output is correct |
39 |
Correct |
645 ms |
9920 KB |
Output is correct |
40 |
Correct |
659 ms |
9924 KB |
Output is correct |
41 |
Correct |
695 ms |
9924 KB |
Output is correct |
42 |
Correct |
728 ms |
9912 KB |
Output is correct |
43 |
Correct |
932 ms |
9912 KB |
Output is correct |
44 |
Correct |
933 ms |
9992 KB |
Output is correct |
45 |
Correct |
908 ms |
9920 KB |
Output is correct |
46 |
Correct |
911 ms |
9916 KB |
Output is correct |
47 |
Correct |
903 ms |
9928 KB |
Output is correct |
48 |
Execution timed out |
3061 ms |
9908 KB |
Time limit exceeded |
49 |
Halted |
0 ms |
0 KB |
- |