#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int NM = 1e5, SZ = 320;
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 (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 |
724 KB |
Output is correct |
2 |
Correct |
1 ms |
724 KB |
Output is correct |
3 |
Correct |
1 ms |
724 KB |
Output is correct |
4 |
Correct |
1 ms |
724 KB |
Output is correct |
5 |
Correct |
1 ms |
716 KB |
Output is correct |
6 |
Correct |
1 ms |
724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
724 KB |
Output is correct |
2 |
Correct |
1 ms |
724 KB |
Output is correct |
3 |
Correct |
1 ms |
724 KB |
Output is correct |
4 |
Correct |
1 ms |
724 KB |
Output is correct |
5 |
Correct |
1 ms |
716 KB |
Output is correct |
6 |
Correct |
1 ms |
724 KB |
Output is correct |
7 |
Correct |
3 ms |
756 KB |
Output is correct |
8 |
Correct |
1 ms |
724 KB |
Output is correct |
9 |
Correct |
8 ms |
772 KB |
Output is correct |
10 |
Correct |
8 ms |
772 KB |
Output is correct |
11 |
Correct |
8 ms |
724 KB |
Output is correct |
12 |
Correct |
8 ms |
724 KB |
Output is correct |
13 |
Correct |
8 ms |
776 KB |
Output is correct |
14 |
Correct |
8 ms |
724 KB |
Output is correct |
15 |
Correct |
8 ms |
736 KB |
Output is correct |
16 |
Correct |
8 ms |
724 KB |
Output is correct |
17 |
Correct |
5 ms |
3924 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
5 ms |
1028 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
724 KB |
Output is correct |
2 |
Correct |
1 ms |
724 KB |
Output is correct |
3 |
Correct |
1 ms |
724 KB |
Output is correct |
4 |
Correct |
1 ms |
724 KB |
Output is correct |
5 |
Correct |
1 ms |
716 KB |
Output is correct |
6 |
Correct |
1 ms |
724 KB |
Output is correct |
7 |
Correct |
3 ms |
756 KB |
Output is correct |
8 |
Correct |
1 ms |
724 KB |
Output is correct |
9 |
Correct |
8 ms |
772 KB |
Output is correct |
10 |
Correct |
8 ms |
772 KB |
Output is correct |
11 |
Correct |
8 ms |
724 KB |
Output is correct |
12 |
Correct |
8 ms |
724 KB |
Output is correct |
13 |
Correct |
8 ms |
776 KB |
Output is correct |
14 |
Correct |
8 ms |
724 KB |
Output is correct |
15 |
Correct |
8 ms |
736 KB |
Output is correct |
16 |
Correct |
8 ms |
724 KB |
Output is correct |
17 |
Correct |
5 ms |
3924 KB |
Output is correct |
18 |
Incorrect |
5 ms |
1028 KB |
Output isn't correct |
19 |
Halted |
0 ms |
0 KB |
- |