// #ifdef __AVX2__
// #pragma GCC target "avx2"
// #endif
// #pragma GCC optimize "O3"
// #pragma GCC optimize "unroll-loops"
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
using namespace std;
#define int long long
#define elif else if
#define all(l) begin(l),end(l)
#define rall(l) rbegin(l),rend(l)
#define append push_back
#define print(l) for(auto i:l) cout<<i<<' '; cout<<endl;
#define pprint(a,b) cout<<a<<' '<<b<<endl;
#define inp(l) for(auto &i:l) cin>>i;
// #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define pai make_pair
#define endl "\n"
#define pii pair<int,int>
#define fi first
#define se second
#define vec vector
#define ins(a,b,c) a.insert(begin(a)+b,c);
// const int mod=998244353;
const int mod1=998244353;
const int mod=1e9+7;
const int N=2e5+5;
int iter=1,itera=1;
void solve(){
int n;
cin>>n;
vec<int>a(n);
inp(a)
ins(a,0,0);
if(n<=2000){
int ans=0;
for(int l=1;l<=n;l++){
int ma=0;
map<int,int>d;
for(int r=l;r<=n;r++){
d[a[r]]++;
ma=max(ma,d[a[r]]);
if(ma>(r-l+1)/2) ans++;
// cout<<l<<' '<<r<<' '<<ma<<endl;
}
}
cout<<ans<<endl;
return;
}
map<int,int>d;
d[0]=1;
int ans=(n*(n+1))/2;
int c=0;
for(int i=1;i<=n;i++){
if(a[i]==a[1]) c++;
else c--;
ans-=d[c];
// cout<<d[c]<<' ';
// cout<<c<<' ';
d[c]++;
}
cout<<ans<<endl;
}
signed main(){
// freopen("","r",stdin);
// freopen("","w",stdout);
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cout<<fixed<<setprecision(20);
// cin>>itera;
for(iter=1;iter<=itera;iter++) solve();
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |