#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
#define int long long
using namespace std;
const int MAXN = 3e5 + 11;
const int MAXQ = 5e4 + 11;
const int SQ = 1000;
int a[MAXN];
int ans[MAXQ];
int c[MAXN], cc[MAXN];
int s0, s1, s2;
set<int> s;
void add(int x){
s0 -= c[x] * (c[x] + 1) / 2;
s1 -= c[x];
s2 -= c[x] * c[x];
cc[c[x]]--; if(cc[c[x]] == 0) s.erase(c[x]);
c[x]++;
s0 += c[x] * (c[x] + 1) / 2;
s1 += c[x];
s2 += c[x] * c[x];
cc[c[x]]++; if(cc[c[x]] == 1) s.insert(c[x]);
}
void rem(int x){
s0 -= c[x] * (c[x] + 1) / 2;
s1 -= c[x];
s2 -= c[x] * c[x];
cc[c[x]]--; if(cc[c[x]] == 0) s.erase(c[x]);
c[x]--;
s0 += c[x] * (c[x] + 1) / 2;
s1 += c[x];
s2 += c[x] * c[x];
cc[c[x]]++; if(cc[c[x]] == 1) s.insert(c[x]);
}
int f(int x0, int d, int y, int n){
return n * d * (x0 * 2 + (d - 1) * y) / 2 - d * x0 * x0 - (d - 1) * d * x0 * y - (d - 1) * d * (d * 2 - 1) * y * y;
}
struct query{
int l, r, id;
};
int32_t main(){
int n, q; cin >> n >> q;
for(int i = 0; i < n; i++){
cin >> a[i];
}
vector<query> Q;
for(int i = 0; i < q; i++){
int l, r; cin >> l >> r; --l; --r; Q.push_back({l, r, i});
}
sort(Q.begin(), Q.end(), [](query a, query b){
if(a.l / SQ != b.l / SQ) return a.l / SQ < b.l / SQ;
return a.l / SQ % 2 ? a.r > b.r : a.r < b.r;
});
int l = 0, r = -1;
for(auto [ql, qr, id] : Q){
while(l > ql) add(a[--l]);
while(r < qr) add(a[++r]);
while(l < ql) rem(a[l++]);
while(r > qr) rem(a[r--]);
int res = s0 + (s1 * s1 - s2) / 2;
int cl = 0, cr = 0;
for(auto y : s){
if(cl > cr) swap(cl, cr);
int cnt = cc[y];
int d = min(cnt, (cr - cl) / y);
res += f(cl, d, y, s1); cl += d * y; cnt -= d;
res += f(cl, (cnt + 1) / 2, y, s1);
res += f(cr, cnt / 2, y, s1);
cl += ((cnt + 1) / 2) * y, cr += (cnt / 2) * y;
}
res += cl * (s1 - cl);
ans[id] = res;
}
for(int i = 0; i < q; i++){
cout << ans[i] << '\n';
}
}
Compilation message
diversity.cpp: In function 'int32_t main()':
diversity.cpp:63:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
63 | for(auto [ql, qr, id] : Q){
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
18 ms |
1212 KB |
Output is correct |
5 |
Correct |
35 ms |
2424 KB |
Output is correct |
6 |
Correct |
55 ms |
3072 KB |
Output is correct |
7 |
Correct |
57 ms |
3832 KB |
Output is correct |
8 |
Correct |
54 ms |
3056 KB |
Output is correct |
9 |
Correct |
53 ms |
3020 KB |
Output is correct |
10 |
Correct |
53 ms |
3404 KB |
Output is correct |
11 |
Correct |
57 ms |
3212 KB |
Output is correct |
12 |
Correct |
56 ms |
3008 KB |
Output is correct |
13 |
Correct |
54 ms |
3008 KB |
Output is correct |
14 |
Correct |
57 ms |
3192 KB |
Output is correct |
15 |
Correct |
53 ms |
3148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
18 ms |
1212 KB |
Output is correct |
5 |
Correct |
35 ms |
2424 KB |
Output is correct |
6 |
Correct |
55 ms |
3072 KB |
Output is correct |
7 |
Correct |
57 ms |
3832 KB |
Output is correct |
8 |
Correct |
54 ms |
3056 KB |
Output is correct |
9 |
Correct |
53 ms |
3020 KB |
Output is correct |
10 |
Correct |
53 ms |
3404 KB |
Output is correct |
11 |
Correct |
57 ms |
3212 KB |
Output is correct |
12 |
Correct |
56 ms |
3008 KB |
Output is correct |
13 |
Correct |
54 ms |
3008 KB |
Output is correct |
14 |
Correct |
57 ms |
3192 KB |
Output is correct |
15 |
Correct |
53 ms |
3148 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
2 ms |
340 KB |
Output is correct |
19 |
Correct |
21 ms |
1184 KB |
Output is correct |
20 |
Correct |
40 ms |
2004 KB |
Output is correct |
21 |
Correct |
61 ms |
3052 KB |
Output is correct |
22 |
Correct |
61 ms |
2852 KB |
Output is correct |
23 |
Correct |
62 ms |
3048 KB |
Output is correct |
24 |
Correct |
62 ms |
2852 KB |
Output is correct |
25 |
Correct |
66 ms |
3004 KB |
Output is correct |
26 |
Correct |
61 ms |
2936 KB |
Output is correct |
27 |
Correct |
61 ms |
3020 KB |
Output is correct |
28 |
Correct |
59 ms |
3232 KB |
Output is correct |
29 |
Correct |
60 ms |
2864 KB |
Output is correct |
30 |
Correct |
61 ms |
2892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
18 ms |
1212 KB |
Output is correct |
5 |
Correct |
35 ms |
2424 KB |
Output is correct |
6 |
Correct |
55 ms |
3072 KB |
Output is correct |
7 |
Correct |
57 ms |
3832 KB |
Output is correct |
8 |
Correct |
54 ms |
3056 KB |
Output is correct |
9 |
Correct |
53 ms |
3020 KB |
Output is correct |
10 |
Correct |
53 ms |
3404 KB |
Output is correct |
11 |
Correct |
57 ms |
3212 KB |
Output is correct |
12 |
Correct |
56 ms |
3008 KB |
Output is correct |
13 |
Correct |
54 ms |
3008 KB |
Output is correct |
14 |
Correct |
57 ms |
3192 KB |
Output is correct |
15 |
Correct |
53 ms |
3148 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
2 ms |
340 KB |
Output is correct |
19 |
Correct |
21 ms |
1184 KB |
Output is correct |
20 |
Correct |
40 ms |
2004 KB |
Output is correct |
21 |
Correct |
61 ms |
3052 KB |
Output is correct |
22 |
Correct |
61 ms |
2852 KB |
Output is correct |
23 |
Correct |
62 ms |
3048 KB |
Output is correct |
24 |
Correct |
62 ms |
2852 KB |
Output is correct |
25 |
Correct |
66 ms |
3004 KB |
Output is correct |
26 |
Correct |
61 ms |
2936 KB |
Output is correct |
27 |
Correct |
61 ms |
3020 KB |
Output is correct |
28 |
Correct |
59 ms |
3232 KB |
Output is correct |
29 |
Correct |
60 ms |
2864 KB |
Output is correct |
30 |
Correct |
61 ms |
2892 KB |
Output is correct |
31 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
32 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |