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 ff first
# define ss second
# define nl "\n"
# define sz(x) ((int)(x).size())
# define deb(x) cerr << #x << " = " << x << endl;
# define pll pair <ll, ll>
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const ll maxn = 2e5 + 25;
const ll inf = 2e9 + 0;
const ll mod = 1e9 + 123;
const ll dx[] = {-1, 1, 0, 0};
const ll dy[] = {0, 0, -1, 1};
using namespace std;
ll n, a[maxn];
ll b[maxn];
vector <ll> pos[maxn];
struct node
{
ll sum1, sum2, upd;
} t[maxn * 4];
ll cnt (ll x)
{
return x * (x + 1) / 2;
}
ll f (ll tl, ll tr)
{
tl = n + n - tl + 1;
tr = n + n - tr + 1;
if (tl > tr) swap(tl, tr);
return cnt(tr) - cnt(tl - 1);
}
void push (ll v, ll tl, ll tr)
{
if (t[v].upd && tl != tr)
{
ll tm = (tl + tr) >> 1;
t[v * 2].sum1 += t[v].upd * (tm - tl + 1);
t[v * 2 + 1].sum1 += t[v].upd * (tr - tm);
t[v * 2].sum2 += t[v].upd * f(tl, tm);
t[v * 2 + 1].sum2 += t[v].upd * f(tm + 1, tr);
t[v * 2].upd += t[v].upd;
t[v * 2 + 1].upd += t[v].upd;
t[v].upd = 0;
}
}
void update (ll l, ll r, ll x, ll v = 1, ll tl = 0, ll tr = n + n)
{
push(v, tl, tr);
if (tr < l || r < tl) return;
if (l <= tl && tr <= r)
{
t[v].sum1 += x * (tr - tl + 1);
t[v].sum2 += x * f(tl, tr);
t[v].upd += x;
return;
}
ll tm = (tl + tr) >> 1;
update(l, r, x, v * 2, tl, tm);
update(l, r, x, v * 2 + 1, tm + 1, tr);
t[v].sum1 = t[v * 2].sum1 + t[v * 2 + 1].sum1;
t[v].sum2 = t[v * 2].sum1 * (tr - tm) + t[v * 2].sum2 + t[v * 2 + 1].sum2;
}
ll get1 (ll l, ll r, ll v = 1, ll tl = 0, ll tr = n + n)
{
push(v, tl, tr);
if (tr < l || r < tl) return 0;
if (l <= tl && tr <= r) return t[v].sum1;
ll tm = (tl + tr) >> 1;
return get1(l, r, v * 2, tl, tm) + get1(l, r, v * 2 + 1, tm + 1, tr);
}
ll get2 (ll l, ll r, ll v = 1, ll tl = 0, ll tr = n + n)
{
push(v, tl, tr);
if (tr < l || r < tl) return 0;
if (l <= tl && tr <= r) return t[v].sum2;
ll tm = (tl + tr) >> 1;
return get2(l, r, v * 2, tl, tm) * (tr - tm) + get2(l, r, v * 2 + 1, tm + 1, tr);
}
void out (ll v = 1, ll tl = 0, ll tr = n + n)
{
push(v, tl, tr);
cout << tl << ' ' << tr << ' ' << t[v].sum2 << nl;
if (tl == tr)
{
return;
}
ll tm = (tl + tr) >> 1;
out(v * 2, tl, tm);
out(v * 2 + 1, tm + 1, tr);
}
void ma1n (/* SABR */)
{
cin >> n;
for (ll i = 1; i <= n; ++i)
{
cin >> a[i];
b[i] = a[i];
}
sort(b + 1, b + 1 + n);
set <ll> st;
for (ll i = 1; i <= n; ++i)
{
a[i] = lower_bound(b + 1, b + 1 + n, a[i]) - b;
st.insert(a[i]);
pos[a[i]].pb(i);
}
ll ans = n;
for (ll x : st)
{
update(n, n, 1);
ll cur = n;
//1 .. pos[x][0]
cur -= pos[x][0] - 1;
ans += get2(0, cur - 1);
for (ll i = 1; i < sz(pos[x]); ++i)
{
ll sz = pos[x][i] - pos[x][i - 1] - 1;
update(cur - sz, cur - 1, 1);
update(cur - sz + 1, cur - sz + 1, 1);
ans += get2(0, cur - sz - 1);
ans += get1(0, cur - sz);
cur = cur - sz + 1;
}
}
cout << ans;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
// freopen("spainting.in", "r", stdin);
// freopen("spainting.out", "w", stdout);
int ttt = 1;
// cin >> ttt;
for (int test = 1; test <= ttt; ++test)
{
// cout << "Case " << test << ":" << ' ';
ma1n();
}
return 0;
}
# | 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... |