#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int NM = 2e5, SZ = 700;
int n, a[NM+5], cnt[NM+5];
bool type[NM+5];
vector <int> v;
ll ans = 0;
int pref[NM+5];
int bit[2*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 p){
while (p <= 2*n+1){
bit[p]++;
p += p & (-p);
}
}
int get(int p){
int res = 0;
while (p > 0){
res += bit[p];
p -= p & (-p);
}
return res;
}
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(bit, 0, sizeof(bit));
update(n+1);
for (int i = 1; i <= n; i++){
ans += get(2*pref[i]-i+n);
update(2*pref[i]-i+n+1);
}
}
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 |
1108 KB |
Output is correct |
2 |
Correct |
1 ms |
1048 KB |
Output is correct |
3 |
Correct |
1 ms |
1108 KB |
Output is correct |
4 |
Correct |
1 ms |
1108 KB |
Output is correct |
5 |
Correct |
1 ms |
1108 KB |
Output is correct |
6 |
Correct |
1 ms |
1108 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1108 KB |
Output is correct |
2 |
Correct |
1 ms |
1048 KB |
Output is correct |
3 |
Correct |
1 ms |
1108 KB |
Output is correct |
4 |
Correct |
1 ms |
1108 KB |
Output is correct |
5 |
Correct |
1 ms |
1108 KB |
Output is correct |
6 |
Correct |
1 ms |
1108 KB |
Output is correct |
7 |
Correct |
3 ms |
1108 KB |
Output is correct |
8 |
Correct |
1 ms |
1108 KB |
Output is correct |
9 |
Correct |
11 ms |
1144 KB |
Output is correct |
10 |
Correct |
14 ms |
1108 KB |
Output is correct |
11 |
Correct |
13 ms |
1144 KB |
Output is correct |
12 |
Correct |
12 ms |
1108 KB |
Output is correct |
13 |
Correct |
13 ms |
1144 KB |
Output is correct |
14 |
Correct |
13 ms |
1108 KB |
Output is correct |
15 |
Correct |
12 ms |
1140 KB |
Output is correct |
16 |
Correct |
12 ms |
1108 KB |
Output is correct |
17 |
Correct |
5 ms |
2644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
460 ms |
4336 KB |
Output is correct |
2 |
Correct |
544 ms |
4916 KB |
Output is correct |
3 |
Correct |
306 ms |
3960 KB |
Output is correct |
4 |
Correct |
567 ms |
4848 KB |
Output is correct |
5 |
Correct |
635 ms |
4912 KB |
Output is correct |
6 |
Correct |
644 ms |
5188 KB |
Output is correct |
7 |
Correct |
677 ms |
5116 KB |
Output is correct |
8 |
Correct |
734 ms |
5116 KB |
Output is correct |
9 |
Correct |
626 ms |
5108 KB |
Output is correct |
10 |
Correct |
630 ms |
5112 KB |
Output is correct |
11 |
Correct |
607 ms |
5108 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1108 KB |
Output is correct |
2 |
Correct |
1 ms |
1048 KB |
Output is correct |
3 |
Correct |
1 ms |
1108 KB |
Output is correct |
4 |
Correct |
1 ms |
1108 KB |
Output is correct |
5 |
Correct |
1 ms |
1108 KB |
Output is correct |
6 |
Correct |
1 ms |
1108 KB |
Output is correct |
7 |
Correct |
3 ms |
1108 KB |
Output is correct |
8 |
Correct |
1 ms |
1108 KB |
Output is correct |
9 |
Correct |
11 ms |
1144 KB |
Output is correct |
10 |
Correct |
14 ms |
1108 KB |
Output is correct |
11 |
Correct |
13 ms |
1144 KB |
Output is correct |
12 |
Correct |
12 ms |
1108 KB |
Output is correct |
13 |
Correct |
13 ms |
1144 KB |
Output is correct |
14 |
Correct |
13 ms |
1108 KB |
Output is correct |
15 |
Correct |
12 ms |
1140 KB |
Output is correct |
16 |
Correct |
12 ms |
1108 KB |
Output is correct |
17 |
Correct |
5 ms |
2644 KB |
Output is correct |
18 |
Correct |
460 ms |
4336 KB |
Output is correct |
19 |
Correct |
544 ms |
4916 KB |
Output is correct |
20 |
Correct |
306 ms |
3960 KB |
Output is correct |
21 |
Correct |
567 ms |
4848 KB |
Output is correct |
22 |
Correct |
635 ms |
4912 KB |
Output is correct |
23 |
Correct |
644 ms |
5188 KB |
Output is correct |
24 |
Correct |
677 ms |
5116 KB |
Output is correct |
25 |
Correct |
734 ms |
5116 KB |
Output is correct |
26 |
Correct |
626 ms |
5108 KB |
Output is correct |
27 |
Correct |
630 ms |
5112 KB |
Output is correct |
28 |
Correct |
607 ms |
5108 KB |
Output is correct |
29 |
Correct |
636 ms |
5108 KB |
Output is correct |
30 |
Correct |
357 ms |
1496 KB |
Output is correct |
31 |
Correct |
721 ms |
1748 KB |
Output is correct |
32 |
Correct |
1788 ms |
2716 KB |
Output is correct |
33 |
Correct |
785 ms |
1876 KB |
Output is correct |
34 |
Correct |
850 ms |
1868 KB |
Output is correct |
35 |
Correct |
540 ms |
1624 KB |
Output is correct |
36 |
Correct |
312 ms |
1496 KB |
Output is correct |
37 |
Correct |
390 ms |
1496 KB |
Output is correct |
38 |
Correct |
649 ms |
5124 KB |
Output is correct |
39 |
Correct |
624 ms |
5120 KB |
Output is correct |
40 |
Correct |
639 ms |
5128 KB |
Output is correct |
41 |
Correct |
611 ms |
5128 KB |
Output is correct |
42 |
Correct |
646 ms |
5128 KB |
Output is correct |
43 |
Correct |
668 ms |
5152 KB |
Output is correct |
44 |
Correct |
714 ms |
5148 KB |
Output is correct |
45 |
Correct |
722 ms |
5144 KB |
Output is correct |
46 |
Correct |
696 ms |
5192 KB |
Output is correct |
47 |
Correct |
676 ms |
5152 KB |
Output is correct |
48 |
Correct |
1869 ms |
5304 KB |
Output is correct |
49 |
Correct |
1878 ms |
5300 KB |
Output is correct |
50 |
Correct |
1095 ms |
5300 KB |
Output is correct |
51 |
Correct |
1105 ms |
5308 KB |
Output is correct |
52 |
Correct |
1685 ms |
2680 KB |
Output is correct |
53 |
Correct |
1744 ms |
2748 KB |
Output is correct |
54 |
Correct |
1830 ms |
5308 KB |
Output is correct |