#include <bits/stdc++.h>
using namespace std ;
const int MAX = 1e6 + 10 ;
const int B = 1 ;
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(x < 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 ;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
23836 KB |
Output is correct |
2 |
Correct |
12 ms |
23832 KB |
Output is correct |
3 |
Correct |
12 ms |
23804 KB |
Output is correct |
4 |
Correct |
15 ms |
23764 KB |
Output is correct |
5 |
Correct |
11 ms |
23808 KB |
Output is correct |
6 |
Correct |
12 ms |
23764 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
23836 KB |
Output is correct |
2 |
Correct |
12 ms |
23832 KB |
Output is correct |
3 |
Correct |
12 ms |
23804 KB |
Output is correct |
4 |
Correct |
15 ms |
23764 KB |
Output is correct |
5 |
Correct |
11 ms |
23808 KB |
Output is correct |
6 |
Correct |
12 ms |
23764 KB |
Output is correct |
7 |
Correct |
12 ms |
23764 KB |
Output is correct |
8 |
Correct |
11 ms |
23764 KB |
Output is correct |
9 |
Correct |
11 ms |
23764 KB |
Output is correct |
10 |
Correct |
11 ms |
23768 KB |
Output is correct |
11 |
Correct |
12 ms |
23764 KB |
Output is correct |
12 |
Correct |
12 ms |
23824 KB |
Output is correct |
13 |
Correct |
11 ms |
23764 KB |
Output is correct |
14 |
Correct |
12 ms |
23796 KB |
Output is correct |
15 |
Correct |
11 ms |
23764 KB |
Output is correct |
16 |
Correct |
13 ms |
23860 KB |
Output is correct |
17 |
Correct |
11 ms |
23764 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
26984 KB |
Output is correct |
2 |
Correct |
26 ms |
28200 KB |
Output is correct |
3 |
Correct |
21 ms |
26388 KB |
Output is correct |
4 |
Correct |
30 ms |
28244 KB |
Output is correct |
5 |
Correct |
29 ms |
28396 KB |
Output is correct |
6 |
Correct |
29 ms |
28608 KB |
Output is correct |
7 |
Correct |
28 ms |
28472 KB |
Output is correct |
8 |
Correct |
29 ms |
28736 KB |
Output is correct |
9 |
Correct |
29 ms |
28612 KB |
Output is correct |
10 |
Correct |
30 ms |
28524 KB |
Output is correct |
11 |
Correct |
25 ms |
29068 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
23836 KB |
Output is correct |
2 |
Correct |
12 ms |
23832 KB |
Output is correct |
3 |
Correct |
12 ms |
23804 KB |
Output is correct |
4 |
Correct |
15 ms |
23764 KB |
Output is correct |
5 |
Correct |
11 ms |
23808 KB |
Output is correct |
6 |
Correct |
12 ms |
23764 KB |
Output is correct |
7 |
Correct |
12 ms |
23764 KB |
Output is correct |
8 |
Correct |
11 ms |
23764 KB |
Output is correct |
9 |
Correct |
11 ms |
23764 KB |
Output is correct |
10 |
Correct |
11 ms |
23768 KB |
Output is correct |
11 |
Correct |
12 ms |
23764 KB |
Output is correct |
12 |
Correct |
12 ms |
23824 KB |
Output is correct |
13 |
Correct |
11 ms |
23764 KB |
Output is correct |
14 |
Correct |
12 ms |
23796 KB |
Output is correct |
15 |
Correct |
11 ms |
23764 KB |
Output is correct |
16 |
Correct |
13 ms |
23860 KB |
Output is correct |
17 |
Correct |
11 ms |
23764 KB |
Output is correct |
18 |
Correct |
23 ms |
26984 KB |
Output is correct |
19 |
Correct |
26 ms |
28200 KB |
Output is correct |
20 |
Correct |
21 ms |
26388 KB |
Output is correct |
21 |
Correct |
30 ms |
28244 KB |
Output is correct |
22 |
Correct |
29 ms |
28396 KB |
Output is correct |
23 |
Correct |
29 ms |
28608 KB |
Output is correct |
24 |
Correct |
28 ms |
28472 KB |
Output is correct |
25 |
Correct |
29 ms |
28736 KB |
Output is correct |
26 |
Correct |
29 ms |
28612 KB |
Output is correct |
27 |
Correct |
30 ms |
28524 KB |
Output is correct |
28 |
Correct |
25 ms |
29068 KB |
Output is correct |
29 |
Correct |
33 ms |
30652 KB |
Output is correct |
30 |
Correct |
1968 ms |
25736 KB |
Output is correct |
31 |
Execution timed out |
3069 ms |
27424 KB |
Time limit exceeded |
32 |
Halted |
0 ms |
0 KB |
- |