#include<bits/stdc++.h>
using namespace std;
const long long inf = (long long) 1e18 + 10;
const int inf1 = (int) 1e9 + 10;
// #define int long long
#define ll long long
#define dbl long double
#define endl '\n'
#define sc second
#define fr first
#define mp make_pair
#define pb push_back
#define all(x) x.begin(), x.end()
const int maxn = 3e5+10;
int n, q, a[maxn], fq[maxn];
ll pre[maxn], ansq[maxn];
int cost(vector<int> v) {
int ans = 0;
int sum_all = 0;
for(auto x : v) sum_all+= x;
int sr = sum_all;
int sl = 0;
for(int i = 0; i < v.size(); i++) {
sr-= v[i];
ans+= sl*sr + v[i]*(sum_all-v[i]) + v[i]*(v[i]+1)/2;
sl+= v[i];
}
return ans;
}
vector<pair<int,int>> vtof(vector<int> v) {
vector<pair<int,int>> ans;
if(v.size() == 0) return ans;
ans.pb(mp(v[0],1));
for(int i = 1; i < v.size(); i++) {
if(v[i] == v[i-1]) {
ans.back().sc++;
}
else {
ans.pb(mp(v[i],1));
}
}
return ans;
}
// val, freq
ll costf(deque<pair<int,int>> v) {
ll ans = 0;
int sum_all = 0;
for(auto x : v) sum_all+= x.fr*x.sc;
int sr = sum_all;
int sl = 0;
for(int i = 0; i < v.size(); i++) {
int val = v[i].fr;
int frq = v[i].sc;
sr-= val*frq;
ans+= (ll) sl*sr*frq + (ll) (frq*(frq+1)/2)*val*(sl+sr) + (ll) frq*(val*(val+1)/2);
if(frq >= 2) {
ans+= (ll) val*val*(frq-1)*(frq);
ans+= (ll) val*val*frq*(frq-2)*(frq-1)/2;
ans-= (ll) pre[frq-2]*val*val;
// for(int j = 1; j <= frq-2; j++) {
// ans-= val*val*j*(j+1);
// }
}
sl+= val*frq;
}
return ans;
}
const int B = 1300;
const int B1 = 1000;
vector<pair<pair<int,int>,int>> bls[maxn];
void solve() {
// vector<pair<int,int>> wtf = {{2,3}};
// // vector<pair<int,int>> wtf = {{2,1},{2,1},{2,1}};
// cout << costf(wtf) << endl;
// exit(0);
cin >> n >> q;
vector<int> cc;
for(int i = 1; i <= n; i++) {
pre[i] = pre[i-1]+(ll) i*(i+1);
cin >> a[i];
cc.pb(a[i]);
}
sort(all(cc));
cc.erase(unique(all(cc)),cc.end());
for(int i = 1; i <= n; i++) a[i] = upper_bound(all(cc),a[i])-cc.begin();
for(int i = 1; i <= q; i++) {
int l,r; cin >> l >> r;
bls[l/B].pb(mp(mp(r,l),i));
}
for(int b = 0; b*B <= n; b++) {
sort(all(bls[b]));
int l = b*B;
int r = b*B-1;
map<int,int> fq1;
for(auto X : bls[b]) {
int l1 = X.fr.sc;
int r1 = X.fr.fr;
while(r < r1) {
fq1[fq[a[r+1]]]--;
if(fq1[fq[a[r+1]]] == 0) fq1.erase(fq[a[r+1]]);
fq[a[r+1]]++;
fq1[fq[a[r+1]]]++;
r++;
}
while(l < l1) {
fq1[fq[a[l]]]--;
if(fq1[fq[a[l]]] == 0) fq1.erase(fq[a[l]]);
fq[a[l]]--;
fq1[fq[a[l]]]++;
l++;
}
while(l > l1) {
fq1[fq[a[l-1]]]--;
if(fq1[fq[a[l-1]]] == 0) fq1.erase(fq[a[l-1]]);
fq[a[l-1]]++;
fq1[fq[a[l-1]]]++;
l--;
}
vector<pair<int,int>> v1;
deque<pair<int,int>> v;
for(auto x : fq1) {
if(x.fr != 0) v1.pb(x);
}
int cnt = 0;
for(int i = (int) v1.size()-1; i >= 0; i--) {
int x1 = v1[i].sc/2;
int x2 = v1[i].sc-x1;
if(x1 != x2) {
if(cnt == 0 && x1 > x2) swap(x1,x2);
else if(cnt == 1 && x1 < x2) swap(x1,x2);
cnt^=1;
}
v.pb(mp(v1[i].fr,x1));
v.push_front(mp(v1[i].fr,x2));
}
ansq[X.sc] = costf(v);
}
while(l <= r) {
fq[a[l]]--;
l++;
}
}
for(int i = 1; i <= q; i++) {
cout << ansq[i] << endl;
}
}
int32_t main() {
ios::sync_with_stdio(false); cin.tie(0);
// freopen("in.in", "r", stdin);
// freopen("out.out", "w", stdout);
int tt = 1;
// cin >> tt;
while(tt--) {
solve();
}
}
Compilation message
diversity.cpp: In function 'int cost(std::vector<int>)':
diversity.cpp:25:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
25 | for(int i = 0; i < v.size(); i++) {
| ~~^~~~~~~~~~
diversity.cpp: In function 'std::vector<std::pair<int, int> > vtof(std::vector<int>)':
diversity.cpp:37:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
37 | for(int i = 1; i < v.size(); i++) {
| ~~^~~~~~~~~~
diversity.cpp: In function 'long long int costf(std::deque<std::pair<int, int> >)':
diversity.cpp:55:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::deque<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
55 | for(int i = 0; i < v.size(); i++) {
| ~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7376 KB |
Output is correct |
2 |
Correct |
5 ms |
7372 KB |
Output is correct |
3 |
Correct |
3 ms |
7372 KB |
Output is correct |
4 |
Correct |
3 ms |
7380 KB |
Output is correct |
5 |
Correct |
3 ms |
7380 KB |
Output is correct |
6 |
Correct |
3 ms |
7368 KB |
Output is correct |
7 |
Correct |
4 ms |
7380 KB |
Output is correct |
8 |
Correct |
4 ms |
7380 KB |
Output is correct |
9 |
Correct |
4 ms |
7324 KB |
Output is correct |
10 |
Correct |
4 ms |
7372 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
7380 KB |
Output is correct |
2 |
Correct |
5 ms |
7380 KB |
Output is correct |
3 |
Correct |
5 ms |
7536 KB |
Output is correct |
4 |
Correct |
20 ms |
9172 KB |
Output is correct |
5 |
Incorrect |
36 ms |
10704 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
7380 KB |
Output is correct |
2 |
Correct |
5 ms |
7380 KB |
Output is correct |
3 |
Correct |
5 ms |
7536 KB |
Output is correct |
4 |
Correct |
20 ms |
9172 KB |
Output is correct |
5 |
Incorrect |
36 ms |
10704 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
7380 KB |
Output is correct |
2 |
Correct |
5 ms |
7380 KB |
Output is correct |
3 |
Correct |
5 ms |
7536 KB |
Output is correct |
4 |
Correct |
20 ms |
9172 KB |
Output is correct |
5 |
Incorrect |
36 ms |
10704 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7376 KB |
Output is correct |
2 |
Correct |
5 ms |
7372 KB |
Output is correct |
3 |
Correct |
3 ms |
7372 KB |
Output is correct |
4 |
Correct |
3 ms |
7380 KB |
Output is correct |
5 |
Correct |
3 ms |
7380 KB |
Output is correct |
6 |
Correct |
3 ms |
7368 KB |
Output is correct |
7 |
Correct |
4 ms |
7380 KB |
Output is correct |
8 |
Correct |
4 ms |
7380 KB |
Output is correct |
9 |
Correct |
4 ms |
7324 KB |
Output is correct |
10 |
Correct |
4 ms |
7372 KB |
Output is correct |
11 |
Correct |
5 ms |
7380 KB |
Output is correct |
12 |
Correct |
5 ms |
7380 KB |
Output is correct |
13 |
Correct |
5 ms |
7536 KB |
Output is correct |
14 |
Correct |
20 ms |
9172 KB |
Output is correct |
15 |
Incorrect |
36 ms |
10704 KB |
Output isn't correct |
16 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7376 KB |
Output is correct |
2 |
Correct |
5 ms |
7372 KB |
Output is correct |
3 |
Correct |
3 ms |
7372 KB |
Output is correct |
4 |
Correct |
3 ms |
7380 KB |
Output is correct |
5 |
Correct |
3 ms |
7380 KB |
Output is correct |
6 |
Correct |
3 ms |
7368 KB |
Output is correct |
7 |
Correct |
4 ms |
7380 KB |
Output is correct |
8 |
Correct |
4 ms |
7380 KB |
Output is correct |
9 |
Correct |
4 ms |
7324 KB |
Output is correct |
10 |
Correct |
4 ms |
7372 KB |
Output is correct |
11 |
Correct |
5 ms |
7380 KB |
Output is correct |
12 |
Correct |
5 ms |
7380 KB |
Output is correct |
13 |
Correct |
5 ms |
7536 KB |
Output is correct |
14 |
Correct |
20 ms |
9172 KB |
Output is correct |
15 |
Incorrect |
36 ms |
10704 KB |
Output isn't correct |
16 |
Halted |
0 ms |
0 KB |
- |