#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
#define int ll
#define all(x) x.begin(), x.end()
#define rep(i, l, n) for (int i = l; i < (n); ++i)
#define sz(x) (int)x.size()
const char nl = '\n';
const int mod = 1e9+7;
struct segtree {
vector<P> tree;
int size;
void init(int n) {
size = 1;
while (size < n)size <<= 1;
tree.assign(2*size-1, {0, 0});
}
void build(vector<int> &a, int x, int lx, int rx) {
if (rx-lx==1) {
if (lx < sz(a)) {
//cout << "!" << x << nl;
tree[x].first = a[lx]*(lx+1), tree[x].second = a[lx];
}
return;
}
int mid = lx+rx>>1;
build(a, 2*x+1, lx, mid);
build(a, 2*x+2, mid, rx);
tree[x].first = tree[2*x+1].first +tree[2*x+2].first;
tree[x].second = tree[2*x+1].second +tree[2*x+2].second;
}
void build(vector<int> &a) {
init(sz(a)+3);
build(a, 0, 0, size);
}
P get(int l, int r, int x, int lx, int rx) {
if (l <= lx && rx <= r)return tree[x];
if (rx <= l || r <= lx)return {0, 0};
int mid = lx+rx>>1;
P s1 = get(l, r, 2*x+1, lx, mid),
s2 = get(l, r, 2*x+2, mid, rx);
return {s1.first+s2.first, s2.second+s1.second};
}
P get(int l, int r) {
return get(l, r, 0, 0, size);
}
};
void solve() {
int n, q; cin >> n >> q;
//rep(i, 0, n)
map<int, int> mp;
rep(i, 0, n) {
int x; cin >> x;
mp[x] += 1;
}
vector<int> a;
for (auto i: mp)a.push_back(i.second);
sort(all(a));
vector<int> res;
rep(i, 0, sz(a))
if (i%2==0)res.push_back(a[i]);
for (int i = sz(a)-1; i >= 0; --i)
if (i&1)res.push_back(a[i]);
//segtree st;
//st.build(res);
int ans = 0;
for (auto i: res)
ans += i*(i+1)/2;
int l, r; cin >> l >> r;
int m = sz(res);
vector<int> p(m+3), p2(m+3);
rep(i, 0, m)
p[i] = i*res[i]+(i>0?p[i-1]:0),
p2[i] = res[i]+(i>0?p2[i-1]:0);
p[m] = p[m-1], p2[m] = p2[m-1];
//cout << st.get(2, 3).second << nl;
//for (auto i: res)cout << i << " ";
//cout << nl;
rep(i, 0, sz(res)) {
P g = P(p[m]-p[i]+p2[m]-p2[i], p2[m]-p2[i]);
int x = (g.first-g.second*i)*res[i];
//assert(ans <= (ll)1e18);
int y = 0;
rep(j, i+1, sz(res))y += res[j]*j;
ans += x;
assert(p[m]-p[i]==y);
//if (x != y) {
//cout << g.second << " " << nl;
//cout << i << " " << x << " " << y << nl;
//}
}
cout << ans << nl;
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
solve();
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... |