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>
#define pb push_back
#define f first
#define s second
using namespace std;
typedef long long ll;
typedef long double ld;
const ll oo = 1e18 + 7;
const ll N = 1e6 + 10;
const ll N1 = 2e5 + 10;
const ll M = 1e3 + 10;
vector <ll> ps[N];
ll b[N];
map <ll, ll> mp;
ll ans = 0;
int val[N];
int key[N];
int t;
void upd(int x, int y)
{
x += N1;
if (key[x] != t)
{
key[x] = t;
val[x] = 0;
}
val[x] += y;
}
int value(int x)
{
x += N1;
if (key[x] != t)
{
key[x] = t;
val[x] = 0;
}
return val[x];
}
int main()
{
ios_base::sync_with_stdio(0);
iostream::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
ll n;
cin >> n;
ll a[n];
vector <ll> cmp;
for (ll i = 0; i < n; i++) cin >> a[i], cmp.pb(a[i]);
sort(cmp.begin(), cmp.end());
cmp.resize(unique(cmp.begin(), cmp.end()) - cmp.begin());
for (ll i = 0; i < n; i++) a[i] = lower_bound(cmp.begin(), cmp.end(), a[i]) - cmp.begin();
for (ll i = 0; i < n; i++) ps[a[i]].pb(i);
for (ll i = 0; i < n; i++)
{
t++;
int nom_mx = N1;
for (int j = i; j < min(n, i + M); j++)
{
upd(a[j], 1);
if (value(a[j]) > value(nom_mx)) nom_mx = a[j];
if (value(nom_mx) * 2 > (j - i + 1))
{
if (ps[nom_mx].size() <= M / 2) ans++;
}
}
}
for (ll i = 0; i < n; i++)
if (ps[i].size() > M / 2)
{
for (ll j = 0; j < n; j++) b[j] = 0;
for (auto j : ps[i]) b[j] = 1;
t++;
ll sum1 = 0;
ll kol = 0;
for (ll j = 0; j < n; j++)
{
sum1 += b[j];
upd(sum1 * 2 - (j + 1), 1);
if (sum1 * 2 - (j + 1) > 0) kol++;
}
ans += kol;
ll c = 1;
sum1 = 0;
for (ll j = 0; j < n; j++)
{
sum1 += b[j];
upd(sum1 * 2 - (j + 1), -1);
if (sum1 * 2 - (j + 1) >= c) kol--;
if (b[j] == 0)
{
c--;
kol += value(c);
}
else
{
kol -= value(c);
c++;
}
ans += kol;
}
cout << endl;
}
cout << ans;
}
/*
5 3
2 1 1 2 2 3
2 3 3 4 4 5
4 1 1 2 2 3 3 4 4 5
*/
# | 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... |