#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int NM = 2e5, SZ = 632;
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 |
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 |
1 ms |
1112 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 |
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 |
1 ms |
1112 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 |
10 ms |
1148 KB |
Output is correct |
10 |
Correct |
12 ms |
1148 KB |
Output is correct |
11 |
Correct |
11 ms |
1108 KB |
Output is correct |
12 |
Correct |
11 ms |
1140 KB |
Output is correct |
13 |
Correct |
11 ms |
1144 KB |
Output is correct |
14 |
Correct |
13 ms |
1072 KB |
Output is correct |
15 |
Correct |
11 ms |
1144 KB |
Output is correct |
16 |
Correct |
11 ms |
1144 KB |
Output is correct |
17 |
Correct |
5 ms |
2772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
351 ms |
4408 KB |
Output is correct |
2 |
Correct |
499 ms |
4784 KB |
Output is correct |
3 |
Correct |
274 ms |
4044 KB |
Output is correct |
4 |
Correct |
522 ms |
4852 KB |
Output is correct |
5 |
Correct |
516 ms |
5020 KB |
Output is correct |
6 |
Correct |
539 ms |
5112 KB |
Output is correct |
7 |
Correct |
556 ms |
5116 KB |
Output is correct |
8 |
Correct |
551 ms |
5116 KB |
Output is correct |
9 |
Correct |
549 ms |
5112 KB |
Output is correct |
10 |
Correct |
598 ms |
5112 KB |
Output is correct |
11 |
Correct |
531 ms |
5108 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1108 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 |
1 ms |
1112 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 |
10 ms |
1148 KB |
Output is correct |
10 |
Correct |
12 ms |
1148 KB |
Output is correct |
11 |
Correct |
11 ms |
1108 KB |
Output is correct |
12 |
Correct |
11 ms |
1140 KB |
Output is correct |
13 |
Correct |
11 ms |
1144 KB |
Output is correct |
14 |
Correct |
13 ms |
1072 KB |
Output is correct |
15 |
Correct |
11 ms |
1144 KB |
Output is correct |
16 |
Correct |
11 ms |
1144 KB |
Output is correct |
17 |
Correct |
5 ms |
2772 KB |
Output is correct |
18 |
Correct |
351 ms |
4408 KB |
Output is correct |
19 |
Correct |
499 ms |
4784 KB |
Output is correct |
20 |
Correct |
274 ms |
4044 KB |
Output is correct |
21 |
Correct |
522 ms |
4852 KB |
Output is correct |
22 |
Correct |
516 ms |
5020 KB |
Output is correct |
23 |
Correct |
539 ms |
5112 KB |
Output is correct |
24 |
Correct |
556 ms |
5116 KB |
Output is correct |
25 |
Correct |
551 ms |
5116 KB |
Output is correct |
26 |
Correct |
549 ms |
5112 KB |
Output is correct |
27 |
Correct |
598 ms |
5112 KB |
Output is correct |
28 |
Correct |
531 ms |
5108 KB |
Output is correct |
29 |
Correct |
560 ms |
5108 KB |
Output is correct |
30 |
Correct |
333 ms |
1496 KB |
Output is correct |
31 |
Correct |
675 ms |
1712 KB |
Output is correct |
32 |
Correct |
1651 ms |
2644 KB |
Output is correct |
33 |
Correct |
728 ms |
1892 KB |
Output is correct |
34 |
Correct |
747 ms |
1884 KB |
Output is correct |
35 |
Correct |
488 ms |
1624 KB |
Output is correct |
36 |
Correct |
280 ms |
1496 KB |
Output is correct |
37 |
Correct |
359 ms |
1564 KB |
Output is correct |
38 |
Correct |
609 ms |
5128 KB |
Output is correct |
39 |
Correct |
566 ms |
5192 KB |
Output is correct |
40 |
Correct |
566 ms |
5124 KB |
Output is correct |
41 |
Correct |
608 ms |
5124 KB |
Output is correct |
42 |
Correct |
590 ms |
5132 KB |
Output is correct |
43 |
Correct |
609 ms |
5148 KB |
Output is correct |
44 |
Correct |
611 ms |
5148 KB |
Output is correct |
45 |
Correct |
617 ms |
5192 KB |
Output is correct |
46 |
Correct |
620 ms |
5148 KB |
Output is correct |
47 |
Correct |
601 ms |
5144 KB |
Output is correct |
48 |
Correct |
1828 ms |
5340 KB |
Output is correct |
49 |
Correct |
1777 ms |
5300 KB |
Output is correct |
50 |
Correct |
1034 ms |
5436 KB |
Output is correct |
51 |
Correct |
1040 ms |
5300 KB |
Output is correct |
52 |
Correct |
1627 ms |
2768 KB |
Output is correct |
53 |
Correct |
1615 ms |
2768 KB |
Output is correct |
54 |
Correct |
1782 ms |
5300 KB |
Output is correct |