This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define fi first
#define se second
#define pb push_back
#define foru(i, l, r) for(int i = l; i <= r; i++)
#define ford(i, r, l) for(int i = r; i >= l; i--)
#define __TIME (1.0 * clock() / CLOCKS_PER_SEC)
typedef pair<int, int> ii;
typedef pair<ii, int> iii;
typedef pair<ii, ii> iiii;
const int oo = 1e9, mod = 1e9 + 7;
const int S=450, N=2e5+1, M=N*2+10;
int n, a[N], s[N], t[M];
int cnt[N], mp[N];
void update(int pos, int val){
for (int i=pos; i<M; i+=i&(-i)) t[i]+=val;
}
int get(int pos){
if (pos<0) return 0;
int sum=0;
for (int i=pos; i; i-=i&(-i)) sum+=t[i];
return sum;
}
void process(){
cin >> n;
long long ans=0;
for (int i=1; i<=n; ++i) cin >> a[i];
vector<int> sus(a+1, a+n+1);
sort(sus.begin(), sus.end()); sus.resize(unique(sus.begin(), sus.end())-sus.begin());
for (int i=1; i<=n; ++i) a[i]=lower_bound(sus.begin(), sus.end(), a[i])-sus.begin()+1;
for (int i=1; i<=n; ++i) ++mp[a[i]];
for (int l=1; l<=n; ++l){
int mx=0;
for (int r=l; r<=n && r-l+1<=2*S; ++r){
if (mp[a[r]]<=S){
++cnt[a[r]];
mx=max(mx, cnt[a[r]]);
}
if (mx*2>r-l+1) ++ans;
}
for (int r=l; r<=n && r-l+1<=2*S; ++r){
if (mp[a[r]]<=S) --cnt[a[r]];
}
}
for (int i=1; i<=n; ++i){
if (mp[i]<=S) continue;
memset(s, 0, sizeof s);
memset(t, 0, sizeof t);
for (int j=1; j<=n; ++j) if (a[j]==i) ++s[j]; else --s[j];
partial_sum(s, s+n+1, s);
update(0+n+1, 1);
for (int j=1; j<=n; ++j){
ans+=get(s[j]-1+n+1);
update(s[j]+n+1, 1);
}
}
cout << ans;
}
signed main() {
cin.tie(0)->sync_with_stdio(false);
//freopen(".inp", "r", stdin);
//freopen(".out", "w", stdout);
process();
cerr << "Time elapsed: " << __TIME << " s.\n";
return 0;
}
/*
Xét các trường hợp đặc biệt
Kiểm tra lại input/output
Cố gắng trâu
Lật ngược bài toán
Keep calm and get VOI
Flow:
*/
# | 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... |