#include "bits/stdc++.h"
using namespace std;
#define ar array
#define int int64_t
const int N = 3e5 + 5;
const int Q = 5e4 + 5;
const int B = 550;
int a[N], l[Q], r[Q];
int cnt[N], res[Q];
signed main(){
ios::sync_with_stdio(0); cin.tie(0);
int n, q; cin>>n>>q;
vector<int> p;
for(int i=0;i<n;i++){
cin>>a[i];
}
for(int i=0;i<q;i++){
cin>>l[i]>>r[i];
l[i]--;
p.push_back(i);
}
sort(p.begin(), p.end(), [&](int i, int j){
if(l[i] / B != l[j] / B){
return l[i] < l[j];
}
return r[i] < r[j];
});
map<int, int> mm;
auto add = [&](int i){
if(cnt[a[i]]){
mm[cnt[a[i]]]--;
if(!mm[cnt[a[i]]]) mm.erase(cnt[a[i]]);
}
cnt[a[i]]++;
mm[cnt[a[i]]]++;
};
auto del = [&](int i){
assert(cnt[a[i]]);
mm[cnt[a[i]]]--;
if(!mm[cnt[a[i]]]) mm.erase(cnt[a[i]]);
cnt[a[i]]--;
if(cnt[a[i]]) mm[cnt[a[i]]]++;
};
int lx = 0, rx = 0;
for(auto i : p){
while(rx > r[i]){
del(--rx);
}
while(rx < r[i]){
add(rx++);
}
while(lx > l[i]){
add(--lx);
}
while(lx < l[i]){
del(lx++);
}
int ls = 0, rs = 0;
deque<ar<int, 2>> L, R;
for(auto [v, c] : mm){
int b = (c + 1) / 2, s = c - b;
if(ls > rs) swap(b, s);
if(b) L.push_back({v, b}), ls += b;
if(s) R.push_front({v, s}), rs += s;
}
L.insert(L.end(), R.begin(), R.end());
//~ for(auto x : L) cout<<x[0]<<" "<<x[1]<<"\n";
int sum = 0, sumj = 0, p = 0;
for(auto [v, c] : L){
res[i] += v * (p * c + c * (c - 1) / 2) * sum;
res[i] -= v * sumj;
res[i] += (c * c * (c + 1) / 2 - c * (c + 1) * (2 * c + 1) / 6) * v * v;
sumj += v * (p * c + c * (c - 1) / 2);
p += c, sum += v * c;
}
res[i] += sum * (sum + 1) / 2;
//~ cout<<"\n";
}
for(int i=0;i<q;i++) cout<<res[i]<<"\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
16 ms |
1108 KB |
Output is correct |
5 |
Correct |
28 ms |
1876 KB |
Output is correct |
6 |
Correct |
47 ms |
2676 KB |
Output is correct |
7 |
Correct |
42 ms |
2684 KB |
Output is correct |
8 |
Correct |
46 ms |
2592 KB |
Output is correct |
9 |
Correct |
45 ms |
2668 KB |
Output is correct |
10 |
Correct |
43 ms |
2672 KB |
Output is correct |
11 |
Correct |
44 ms |
2672 KB |
Output is correct |
12 |
Correct |
45 ms |
2628 KB |
Output is correct |
13 |
Correct |
45 ms |
2672 KB |
Output is correct |
14 |
Correct |
44 ms |
2656 KB |
Output is correct |
15 |
Correct |
43 ms |
2644 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
16 ms |
1108 KB |
Output is correct |
5 |
Correct |
28 ms |
1876 KB |
Output is correct |
6 |
Correct |
47 ms |
2676 KB |
Output is correct |
7 |
Correct |
42 ms |
2684 KB |
Output is correct |
8 |
Correct |
46 ms |
2592 KB |
Output is correct |
9 |
Correct |
45 ms |
2668 KB |
Output is correct |
10 |
Correct |
43 ms |
2672 KB |
Output is correct |
11 |
Correct |
44 ms |
2672 KB |
Output is correct |
12 |
Correct |
45 ms |
2628 KB |
Output is correct |
13 |
Correct |
45 ms |
2672 KB |
Output is correct |
14 |
Correct |
44 ms |
2656 KB |
Output is correct |
15 |
Correct |
43 ms |
2644 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
3 ms |
340 KB |
Output is correct |
19 |
Correct |
22 ms |
1128 KB |
Output is correct |
20 |
Correct |
37 ms |
1892 KB |
Output is correct |
21 |
Correct |
63 ms |
2676 KB |
Output is correct |
22 |
Correct |
60 ms |
2684 KB |
Output is correct |
23 |
Correct |
51 ms |
2644 KB |
Output is correct |
24 |
Correct |
58 ms |
2684 KB |
Output is correct |
25 |
Correct |
61 ms |
2644 KB |
Output is correct |
26 |
Correct |
65 ms |
2672 KB |
Output is correct |
27 |
Correct |
53 ms |
2672 KB |
Output is correct |
28 |
Correct |
52 ms |
2684 KB |
Output is correct |
29 |
Correct |
58 ms |
2612 KB |
Output is correct |
30 |
Correct |
54 ms |
2676 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
16 ms |
1108 KB |
Output is correct |
5 |
Correct |
28 ms |
1876 KB |
Output is correct |
6 |
Correct |
47 ms |
2676 KB |
Output is correct |
7 |
Correct |
42 ms |
2684 KB |
Output is correct |
8 |
Correct |
46 ms |
2592 KB |
Output is correct |
9 |
Correct |
45 ms |
2668 KB |
Output is correct |
10 |
Correct |
43 ms |
2672 KB |
Output is correct |
11 |
Correct |
44 ms |
2672 KB |
Output is correct |
12 |
Correct |
45 ms |
2628 KB |
Output is correct |
13 |
Correct |
45 ms |
2672 KB |
Output is correct |
14 |
Correct |
44 ms |
2656 KB |
Output is correct |
15 |
Correct |
43 ms |
2644 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
3 ms |
340 KB |
Output is correct |
19 |
Correct |
22 ms |
1128 KB |
Output is correct |
20 |
Correct |
37 ms |
1892 KB |
Output is correct |
21 |
Correct |
63 ms |
2676 KB |
Output is correct |
22 |
Correct |
60 ms |
2684 KB |
Output is correct |
23 |
Correct |
51 ms |
2644 KB |
Output is correct |
24 |
Correct |
58 ms |
2684 KB |
Output is correct |
25 |
Correct |
61 ms |
2644 KB |
Output is correct |
26 |
Correct |
65 ms |
2672 KB |
Output is correct |
27 |
Correct |
53 ms |
2672 KB |
Output is correct |
28 |
Correct |
52 ms |
2684 KB |
Output is correct |
29 |
Correct |
58 ms |
2612 KB |
Output is correct |
30 |
Correct |
54 ms |
2676 KB |
Output is correct |
31 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
32 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |