#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int NM = 2e5, SZ = 450;
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 |
2 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 |
2 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 |
2 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 |
2 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 |
10 ms |
1144 KB |
Output is correct |
10 |
Correct |
10 ms |
1144 KB |
Output is correct |
11 |
Correct |
10 ms |
1140 KB |
Output is correct |
12 |
Correct |
11 ms |
1108 KB |
Output is correct |
13 |
Correct |
10 ms |
1144 KB |
Output is correct |
14 |
Correct |
9 ms |
1108 KB |
Output is correct |
15 |
Correct |
8 ms |
1108 KB |
Output is correct |
16 |
Correct |
9 ms |
1108 KB |
Output is correct |
17 |
Correct |
4 ms |
2644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
301 ms |
4336 KB |
Output is correct |
2 |
Correct |
355 ms |
4800 KB |
Output is correct |
3 |
Correct |
229 ms |
3968 KB |
Output is correct |
4 |
Correct |
412 ms |
4848 KB |
Output is correct |
5 |
Correct |
395 ms |
4996 KB |
Output is correct |
6 |
Correct |
458 ms |
5120 KB |
Output is correct |
7 |
Correct |
436 ms |
5120 KB |
Output is correct |
8 |
Correct |
442 ms |
5108 KB |
Output is correct |
9 |
Correct |
463 ms |
5112 KB |
Output is correct |
10 |
Correct |
421 ms |
5240 KB |
Output is correct |
11 |
Correct |
460 ms |
5244 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 |
2 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 |
2 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 |
10 ms |
1144 KB |
Output is correct |
10 |
Correct |
10 ms |
1144 KB |
Output is correct |
11 |
Correct |
10 ms |
1140 KB |
Output is correct |
12 |
Correct |
11 ms |
1108 KB |
Output is correct |
13 |
Correct |
10 ms |
1144 KB |
Output is correct |
14 |
Correct |
9 ms |
1108 KB |
Output is correct |
15 |
Correct |
8 ms |
1108 KB |
Output is correct |
16 |
Correct |
9 ms |
1108 KB |
Output is correct |
17 |
Correct |
4 ms |
2644 KB |
Output is correct |
18 |
Correct |
301 ms |
4336 KB |
Output is correct |
19 |
Correct |
355 ms |
4800 KB |
Output is correct |
20 |
Correct |
229 ms |
3968 KB |
Output is correct |
21 |
Correct |
412 ms |
4848 KB |
Output is correct |
22 |
Correct |
395 ms |
4996 KB |
Output is correct |
23 |
Correct |
458 ms |
5120 KB |
Output is correct |
24 |
Correct |
436 ms |
5120 KB |
Output is correct |
25 |
Correct |
442 ms |
5108 KB |
Output is correct |
26 |
Correct |
463 ms |
5112 KB |
Output is correct |
27 |
Correct |
421 ms |
5240 KB |
Output is correct |
28 |
Correct |
460 ms |
5244 KB |
Output is correct |
29 |
Correct |
477 ms |
5364 KB |
Output is correct |
30 |
Correct |
241 ms |
1624 KB |
Output is correct |
31 |
Correct |
484 ms |
1884 KB |
Output is correct |
32 |
Correct |
1231 ms |
2864 KB |
Output is correct |
33 |
Correct |
501 ms |
2004 KB |
Output is correct |
34 |
Correct |
523 ms |
1916 KB |
Output is correct |
35 |
Correct |
349 ms |
1744 KB |
Output is correct |
36 |
Correct |
209 ms |
1584 KB |
Output is correct |
37 |
Correct |
255 ms |
1560 KB |
Output is correct |
38 |
Correct |
418 ms |
5372 KB |
Output is correct |
39 |
Correct |
438 ms |
5240 KB |
Output is correct |
40 |
Correct |
411 ms |
5192 KB |
Output is correct |
41 |
Correct |
438 ms |
5252 KB |
Output is correct |
42 |
Correct |
426 ms |
5376 KB |
Output is correct |
43 |
Correct |
461 ms |
5596 KB |
Output is correct |
44 |
Correct |
456 ms |
5656 KB |
Output is correct |
45 |
Correct |
468 ms |
5720 KB |
Output is correct |
46 |
Correct |
468 ms |
5660 KB |
Output is correct |
47 |
Correct |
468 ms |
5576 KB |
Output is correct |
48 |
Correct |
1580 ms |
5604 KB |
Output is correct |
49 |
Correct |
1593 ms |
5988 KB |
Output is correct |
50 |
Correct |
867 ms |
5868 KB |
Output is correct |
51 |
Correct |
868 ms |
5872 KB |
Output is correct |
52 |
Correct |
2775 ms |
5956 KB |
Output is correct |
53 |
Correct |
2551 ms |
6084 KB |
Output is correct |
54 |
Correct |
1598 ms |
5988 KB |
Output is correct |