/* Author : Mychecksdead */
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define MOD (1000000000+7)
#define MOD1 (998244353)
#define pb push_back
#define all(x) x.begin(), x.end()
#define en cout << '\n'
#define ff first
#define ss second
#define pii pair<int,int>
#define vi vector<int>
const int N = 1e6+100, M = 1e5+10, K = 52, MX = 30;
int n, q, a[N];
ll ans[N], cur;
vector<array<int, 4>> Q;
vector<ll> freq(N), co(N);
set<int> S;
void add(int i){
co[freq[a[i]]]--;
if(co[freq[a[i]]] == 0) S.erase(freq[a[i]]);
freq[a[i]]++;
co[freq[a[i]]]++;
if(co[freq[a[i]]] == 1) S.insert(freq[a[i]]);
}
void rem(int i){
co[freq[a[i]]]--;
if(co[freq[a[i]]] == 0) S.erase(freq[a[i]]);
freq[a[i]]--;
co[freq[a[i]]]++;
if(co[freq[a[i]]] == 1 && freq[a[i]] > 0) S.insert(freq[a[i]]);
}
ll get_sq(ll x){
return (x+1)*(2*x+1)*x/6;
}
ll get_li(ll x){
return (x*(x+1))>>1;
}
void solve(){
cin >> n >> q;
for(int i = 1; i <= n; ++i){
cin >> a[i];
}
const int SS = sqrt(n);
for(int i = 1; i <= q; ++i){
int l, r; cin >> l >> r;
Q.pb({l / SS, r, l, i});
}
sort(all(Q));
cur = 0;
int L = 1, R = 0;
for(auto p: Q){
while(R < p[1]){
++R;
add(R);
}
while(L > p[2]){
--L;
add(L);
}
while(R > p[1]){
rem(R);
--R;
}
while(L < p[2]){
rem(L);
++L;
}
cur = 0;
ll odd = 0, even = 0, tot = 0, d = 0;
for(ll pp: S){
ll k = co[pp];
ll x = (k + 1) >> 1;
ll y = k >> 1;
if((d & 1)) swap(x, y);
// cout << x << ' ' << y << '\n';
cur += x * odd * odd;
cur += 2 * get_li(x - 1) * odd * pp;
cur += get_sq(x - 1) * pp * pp;
cur += y * even * even;
cur += 2 * get_li(y - 1) * even * pp;
cur += get_sq(y - 1) * pp * pp;
odd += x * pp, even += y * pp;
// else odd += y * pp, even += x * pp;
tot += k * pp;
d += k;
}
// cout << odd << ' ' << even << ' ' << cur << '\n';
odd = 0, even = 0;
ll dd = 0;
for(ll pp: S){
ll k = co[pp];
ll x = (k + 1) >> 1;
ll y = k >> 1;
if((dd & 1)) swap(x, y);
ll P = (tot - odd - pp);
ll T = (tot - even - pp);
cur += x * P * P;
cur -= 2 * get_li(x - 1) * P * pp;
cur += get_sq(x - 1) * pp * pp;
cur += y * T * T;
cur -= 2 * get_li(y - 1) * T * pp;
cur += get_sq(y - 1) * pp * pp;
odd += x * pp, even += y * pp;
// else odd += y * pp, even += x * pp;
dd += k;
}
// cout << d << ' ' << tot << ' ' << cur << ' ' ;
ans[p[3]] = (d * tot * (tot+1) - tot * (d-1) - cur) >> 1;
}
for(int i = 1; i <= q; ++i){
cout << ans[i] << '\n';
}
}
int main(){
cin.tie(0); ios::sync_with_stdio(0);
int tt = 1, aa;
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
while(tt--){
solve();
en;
}
cerr<<"time taken : "<<(float)clock()/CLOCKS_PER_SEC<<" seconds\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... |