#include <bits/stdc++.h>
using namespace std ;
const int MAX = 1e6 + 10 ;
const int B = 448 ;
int arr[MAX] ;
int n ;
vector<int>occ[MAX] ;
void compress()
{
vector<int>v ;
for(int i = 1 ; i <= n ; ++i)
v.push_back(arr[i]) ;
sort(v.begin() , v.end()) ;
v.erase(unique(v.begin() , v.end()) , v.end()) ;
for(int i = 1 ; i <= n ; ++i)
{
arr[i] = lower_bound(v.begin() , v.end() , arr[i]) - v.begin() ;
arr[i]++ ;
}
}
int freq[MAX] ;
long long solve_big(int x)
{
for(int i = 0 ; i <= 3*n ; ++i)
freq[i] = 0 ;
int idx = n-1 , cur = n ;
long long ans = 0 , cnt = 0 ;
freq[n]++ , cnt = 0 ;
for(int i = 1 ; i <= n ; ++i)
{
if(arr[i] == x)
idx++ , cnt += freq[idx] , ++cur ;
else
cnt -= freq[idx] , --idx , --cur ;
ans += cnt ;
freq[cur]++ ;
if(cur <= idx)
cnt++ ;
}
return ans ;
}
long long calc(int l , int r)
{
return (1ll * r * (r+1ll) / 2ll - 1ll * (l-1) * (l) / 2ll) ;
}
long long solve_small(int x)
{
long long ans = 0 ;
int sz = occ[x].size() ;
for(int i = 0 ; i < sz ; ++i)
{
for(int j = 0 ; j <= i ; ++j)
{
int cnt = i-j+1 - (occ[x][i] - occ[x][j] + 1 - (i-j+1)) - 1 ;
int a = occ[x][j] , b = n+1 - occ[x][i] ;
if(j > 0)
a -= occ[x][j-1] ;
if(i+1 < sz)
b = occ[x][i+1] - occ[x][i] ;
if(cnt < 0)
continue ;
// start at occ[x][j]
ans += min(cnt+1 , b) , --a ;
// end at occ[x][i]
ans += min(cnt , a) , --b ;
// start < oxx[x][j] and end > oxx[j][i]
int y = max(0 , min(cnt-a , b)) ;
ans += 1ll * a * y ;
b -= y , cnt -= y ;
ans += calc(cnt - min(cnt , b) , cnt-1) ;
}
}
return ans ;
}
int main()
{
ios_base::sync_with_stdio(0) ;
cin.tie(0) ;
cin>>n ;
for(int i = 1 ; i <= n ; ++i)
cin>>arr[i] ;
compress() ;
for(int i = 1 ; i <= n ; ++i)
occ[arr[i]].push_back(i) ;
long long ans = 0 ;
for(int i = 1 ; i <= n ; ++i)
{
if(occ[i].size() >= B)
ans += solve_big(i) ;
else
ans += solve_small(i) ;
}
return cout<<ans<<"\n" , 0 ;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23764 KB |
Output is correct |
2 |
Correct |
11 ms |
23756 KB |
Output is correct |
3 |
Correct |
11 ms |
23764 KB |
Output is correct |
4 |
Correct |
13 ms |
23764 KB |
Output is correct |
5 |
Correct |
11 ms |
23792 KB |
Output is correct |
6 |
Correct |
11 ms |
23756 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23764 KB |
Output is correct |
2 |
Correct |
11 ms |
23756 KB |
Output is correct |
3 |
Correct |
11 ms |
23764 KB |
Output is correct |
4 |
Correct |
13 ms |
23764 KB |
Output is correct |
5 |
Correct |
11 ms |
23792 KB |
Output is correct |
6 |
Correct |
11 ms |
23756 KB |
Output is correct |
7 |
Correct |
11 ms |
23864 KB |
Output is correct |
8 |
Correct |
11 ms |
23824 KB |
Output is correct |
9 |
Correct |
12 ms |
23764 KB |
Output is correct |
10 |
Correct |
12 ms |
23812 KB |
Output is correct |
11 |
Correct |
12 ms |
23764 KB |
Output is correct |
12 |
Correct |
11 ms |
23764 KB |
Output is correct |
13 |
Correct |
12 ms |
23732 KB |
Output is correct |
14 |
Correct |
12 ms |
23792 KB |
Output is correct |
15 |
Correct |
12 ms |
23764 KB |
Output is correct |
16 |
Correct |
12 ms |
23764 KB |
Output is correct |
17 |
Correct |
11 ms |
23764 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
27040 KB |
Output is correct |
2 |
Correct |
26 ms |
27944 KB |
Output is correct |
3 |
Correct |
20 ms |
26212 KB |
Output is correct |
4 |
Correct |
27 ms |
27888 KB |
Output is correct |
5 |
Correct |
28 ms |
28012 KB |
Output is correct |
6 |
Correct |
28 ms |
28204 KB |
Output is correct |
7 |
Correct |
31 ms |
28184 KB |
Output is correct |
8 |
Correct |
29 ms |
28456 KB |
Output is correct |
9 |
Correct |
29 ms |
28224 KB |
Output is correct |
10 |
Correct |
29 ms |
28224 KB |
Output is correct |
11 |
Correct |
25 ms |
28760 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23764 KB |
Output is correct |
2 |
Correct |
11 ms |
23756 KB |
Output is correct |
3 |
Correct |
11 ms |
23764 KB |
Output is correct |
4 |
Correct |
13 ms |
23764 KB |
Output is correct |
5 |
Correct |
11 ms |
23792 KB |
Output is correct |
6 |
Correct |
11 ms |
23756 KB |
Output is correct |
7 |
Correct |
11 ms |
23864 KB |
Output is correct |
8 |
Correct |
11 ms |
23824 KB |
Output is correct |
9 |
Correct |
12 ms |
23764 KB |
Output is correct |
10 |
Correct |
12 ms |
23812 KB |
Output is correct |
11 |
Correct |
12 ms |
23764 KB |
Output is correct |
12 |
Correct |
11 ms |
23764 KB |
Output is correct |
13 |
Correct |
12 ms |
23732 KB |
Output is correct |
14 |
Correct |
12 ms |
23792 KB |
Output is correct |
15 |
Correct |
12 ms |
23764 KB |
Output is correct |
16 |
Correct |
12 ms |
23764 KB |
Output is correct |
17 |
Correct |
11 ms |
23764 KB |
Output is correct |
18 |
Correct |
23 ms |
27040 KB |
Output is correct |
19 |
Correct |
26 ms |
27944 KB |
Output is correct |
20 |
Correct |
20 ms |
26212 KB |
Output is correct |
21 |
Correct |
27 ms |
27888 KB |
Output is correct |
22 |
Correct |
28 ms |
28012 KB |
Output is correct |
23 |
Correct |
28 ms |
28204 KB |
Output is correct |
24 |
Correct |
31 ms |
28184 KB |
Output is correct |
25 |
Correct |
29 ms |
28456 KB |
Output is correct |
26 |
Correct |
29 ms |
28224 KB |
Output is correct |
27 |
Correct |
29 ms |
28224 KB |
Output is correct |
28 |
Correct |
25 ms |
28760 KB |
Output is correct |
29 |
Correct |
33 ms |
28736 KB |
Output is correct |
30 |
Correct |
23 ms |
25024 KB |
Output is correct |
31 |
Correct |
35 ms |
26068 KB |
Output is correct |
32 |
Correct |
83 ms |
29528 KB |
Output is correct |
33 |
Correct |
40 ms |
26668 KB |
Output is correct |
34 |
Correct |
38 ms |
26764 KB |
Output is correct |
35 |
Correct |
34 ms |
25844 KB |
Output is correct |
36 |
Correct |
22 ms |
25036 KB |
Output is correct |
37 |
Correct |
23 ms |
25324 KB |
Output is correct |
38 |
Correct |
32 ms |
28592 KB |
Output is correct |
39 |
Correct |
32 ms |
28604 KB |
Output is correct |
40 |
Correct |
32 ms |
28584 KB |
Output is correct |
41 |
Correct |
31 ms |
28724 KB |
Output is correct |
42 |
Correct |
33 ms |
28468 KB |
Output is correct |
43 |
Correct |
52 ms |
30264 KB |
Output is correct |
44 |
Correct |
43 ms |
30144 KB |
Output is correct |
45 |
Correct |
43 ms |
30176 KB |
Output is correct |
46 |
Correct |
43 ms |
30168 KB |
Output is correct |
47 |
Correct |
43 ms |
30180 KB |
Output is correct |
48 |
Correct |
109 ms |
28676 KB |
Output is correct |
49 |
Correct |
108 ms |
28696 KB |
Output is correct |
50 |
Correct |
64 ms |
28916 KB |
Output is correct |
51 |
Correct |
65 ms |
28972 KB |
Output is correct |
52 |
Correct |
166 ms |
28584 KB |
Output is correct |
53 |
Correct |
152 ms |
28488 KB |
Output is correct |
54 |
Correct |
103 ms |
28520 KB |
Output is correct |