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;
typedef long long ll;
typedef vector<ll> vi;
typedef pair<ll, ll> pii;
typedef vector<pii> vpii;
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
const ll INF = 1e18;
int N, Q;
vi A;
vi ans;
vi freq;
ll tmp, pref, suff, n;
struct query
{
int l, r, idx;
};
ll numberOfSubsequences(ll x)
{
return x * (x + 1) / 2;
}
ll cach3[300010];
ll totalLengthOfSubsequences(ll x)
{
if (cach3[x] == -1)
cach3[x] = x * (x + 1) * (x + 1) / 2 - (2 * x + 1) * (x + 1) * x / 6;
return cach3[x];
}
void doStuff(ll x, ll y)
{
ll len = x * y;
// if both are in set
tmp -= totalLengthOfSubsequences(len) - x * x * (totalLengthOfSubsequences(y) - y) - y * numberOfSubsequences(x);
// if one is inside the other isn't
tmp -= (n - len) * (len * (len + 1) / 2 - x * y * (y + 1) / 2);
// if both are outside
tmp -= (len - y) * pref * (n - pref - len);
pref += len;
swap(pref, suff);
}
struct segtree
{
vi arr;
int size;
void init(int _size)
{
size = 1;
while (size < _size)
size *= 2;
arr.assign(2 * size, 0);
}
void add(int i, int v)
{
i += size;
while (i > 0)
{
arr[i] += v;
i /= 2;
}
}
void dfs(int x)
{
if (arr[x] == 0)
return;
if (x < size)
{
dfs(2 * x);
dfs(2 * x + 1);
}
else
{
// arr[x] := how often
if (arr[x] & 1)
doStuff(x - size, 1);
if (arr[x] > 1)
{
doStuff(x - size, arr[x] / 2);
doStuff(x - size, arr[x] / 2);
}
}
}
};
segtree seg;
void add(int x)
{
if (freq[x] > 0)
seg.add(freq[x], -1);
++freq[x];
seg.add(freq[x], 1);
}
void sub(int x)
{
seg.add(freq[x], -1);
--freq[x];
if (freq[x])
seg.add(freq[x], 1);
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin >> N >> Q;
A.resize(N);
for (int i = 0; i < N; ++i)
cin >> A[i];
vector<query> Qu(Q);
ans.resize(Q);
for (int i = 0; i < Q; ++i)
cin >> Qu[i].l >> Qu[i].r, Qu[i].idx = i, --Qu[i].l;
int sq = ceil(sqrt(N));
auto cmp = [&](const query &a, const query &b)
{ if (a.l / sq == b.l / sq)
return a.r < b.r;
else
return a.l < b.l; };
sort(all(Qu), cmp);
fill(cach3, cach3 + N + 2, -1);
int lx = 0, rx = 0;
freq.assign(*max_element(all(A)) + 1, 0);
seg.init(N + 1);
for (query q : Qu)
{
while (rx < q.r)
add(A[rx++]);
while (lx > q.l)
add(A[--lx]);
while (rx > q.r)
sub(A[--rx]);
while (lx < q.l)
sub(A[lx++]);
n = q.r - q.l;
pref = 0, suff = 0;
tmp = totalLengthOfSubsequences(n);
seg.dfs(1);
ans[q.idx] = tmp;
}
for (ll x : ans)
cout << x << "\n";
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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |