This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/**
____ ____ ____ ____ ____ ____
||l |||e |||i |||n |||a |||d ||
||__|||__|||__|||__|||__|||__||
|/__\|/__\|/__\|/__\|/__\|/__\|
**/
#include<bits/stdc++.h>
#define endl '\n'
using namespace std;
typedef long long ll;
void speed()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
const int maxn = 3e5 + 10, maxq = 5e4 + 10;
const int block_size = sqrt(maxn);
struct query
{
int l, r, idx;
} task[maxq];
bool cmp_mo(query t1, query t2)
{
if (t1.l / block_size == t2.l / block_size)
return t1.r < t2.r;
return t1.l < t2.l;
}
int q, a[maxn], freq[maxn], comp[maxn];
ll n;
unordered_map < int, int > act;
void update(int val, int c)
{
///cout << "update " << val << " " << c << endl;
if (freq[val] != 0)
{
act[freq[val]] --;
if (act[freq[val]] == 0)
act.erase(freq[val]);
}
///comp[freq[val]] --;
freq[val] += c;
///comp[freq[val]] ++;
if (freq[val] != 0)
{
act[freq[val]] ++;
}
}
int lf = 1, rf = 0;
ll ans[maxn];
void move_borders(int left, int right)
{
while(rf < right)
{
rf ++;
update(a[rf], 1);
}
while(lf > left)
{
lf --;
update(a[lf], 1);
}
while(rf > right)
{
update(a[rf], -1);
rf --;
}
while(lf < left)
{
update(a[lf], -1);
lf ++;
}
}
ll arr[maxn], bef[maxn], aft[maxn];
ll calc_function(ll len)
{
vector < pair < ll, ll > > values;
for (pair < int, int > cur : act)
{
values.push_back(cur);
}
sort(values.begin(), values.end());
ll sz = values.size(), cp = 0;
deque < pair < ll, ll > > dq;
ll left_add = 0, right_add = 0;
values.back().second --;
cp ++;
dq.push_back({values.back().first, 1});
for (int i = sz - 1; i >= 0; i --)
{
ll p1 = (values[i].second + 1) / 2;
ll p2 = (values[i].second) / 2;
if (right_add > left_add)
swap(p1, p2);
if(p1 != 0)
dq.push_back({values[i].first, p1});
if (p2 != 0)
dq.push_front({values[i].first, p2});
cp += values[i].second;
left_add += p2;
right_add += p1;
}
int pt = 0;
for (int i = 0; i < dq.size(); i ++)
{
for (int j = 0; j < dq[i].second; i ++)
arr[++ pt] = dq[i].first;
}
/**for (int i = 0; i < sz; i += 2)
{
arr[++ pt] = values[i];
}
int f = sz - 1;
if (sz % 2 == 1)
f --;
for (int i = f; i >= 0; i -= 2)
arr[++ pt] = values[i];*/
/**for (int i = 1; i <= cp; i ++)
cout << arr[i] << " ";
cout << endl;*/
/**for (int i = 1; i <= n; i ++)
cout << comp[i] << " ";
cout << endl;*/
ll tot = 0, pref = 0;
for (int i = 1; i < cp; i ++)
{
pref += arr[i];
tot = tot + pref * pref;
}
ll suff = 0;
for (int i = cp; i > 1; i --)
{
suff = suff + arr[i];
tot = tot + suff * suff;
}
///cout << tot << endl;
tot = (cp * len * (len + 1) - len * (cp - 1) - tot) / 2;
return tot;
}
void compress_data()
{
int cur = 0;
map < int, int > mp;
for (int i = 1; i <= n; i ++)
{
if (mp[a[i]] == 0)
mp[a[i]] = ++ cur;
a[i] = mp[a[i]];
}
}
void solve()
{
cin >> n >> q;
for (int i = 1; i <= n; i ++)
cin >> a[i];
compress_data();
for (int i = 1; i <= q; i ++)
{
cin >> task[i].l >> task[i].r;
task[i].idx = i;
}
sort(task + 1, task + q + 1, cmp_mo);
for (int i = 1; i <= q; i ++)
{
move_borders(task[i].l, task[i].r);
ans[task[i].idx] = calc_function(task[i].r - task[i].l + 1);
}
for (int i = 1; i <= q; i ++)
cout << ans[i] << endl;
}
int main()
{
speed();
solve();
return 0;
}
Compilation message (stderr)
diversity.cpp: In function 'll calc_function(ll)':
diversity.cpp:123:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::deque<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
123 | for (int i = 0; i < dq.size(); i ++)
| ~~^~~~~~~~~~~
# | 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... |