#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int, int>
#define fi first
#define se second
const int MX = 3e5 + 10;
const int SQ = 605;
struct queries{
int l, r, id;
bool operator < (queries& b) const{
int _a = l / SQ, _b = b.l / SQ;
return (_a < _b) || (_a == _b && r < b.r);
}
};
vector<queries> q;
int N, Q, arr[MX];
int cnt[MX]; // number of values with freq i
int freq[MX];
ll ans[MX];
void upd(int p, int v){
if(freq[p] < SQ) cnt[freq[p]]--;
freq[p] += v;
if(freq[p] < SQ) cnt[freq[p]]++;
}
vector<int> lg;
vector<pii> val;
void init(){
val.clear();
for(int j = 1; j < SQ; j++)
val.push_back({j, cnt[j]});
vector<int> temp;
for(auto j : lg){
if(freq[j] >= SQ){
cnt[freq[j]]++;
temp.push_back(freq[j]);
}
}
sort(temp.begin(), temp.end());
temp.resize(unique(temp.begin(), temp.end()) - temp.begin());
for(auto j : temp){
val.push_back({j, cnt[j]});
cnt[j] = 0;
}
}
ll sum(int a, int r, int n){ // a^2 + (a + r)^2 + (a + 2r)^2 + ... (a + nr)^2
if(n < 0) return 0;
return 1ll * a * a * (n + 1) + a * r * n * (n + 1) + r * r * (n * (n + 1) * (n + n + 1) / 6);
}
ll solve(){
int st = 1, _N = 0, _K = 0;
int suml = 0, sumr = 0;
ll curr = 0;
for(auto p : val){
_N += p.fi * p.se;
_K += p.se;
}
for(auto p : val){
int l = (p.se + st) >> 1;
int r = (p.se + 1 - st) >> 1;
curr += sum(suml, p.fi, l - 1);
curr += sum(_N - suml - p.fi, -p.fi, l - 1);
curr += sum(sumr, p.fi, r - 1);
curr += sum(_N - sumr - p.fi, -p.fi, r - 1);
suml += p.fi * l;
sumr += p.fi * r;
st ^= p.se & 1;
}
return (1ll * _K * _N * (_N + 1) - 1ll * _N * (_K - 1) - curr) >> 1;
}
void answer(){
int l = 0, r = -1;
for(auto p : q){
while(r < p.r) upd(arr[++r], 1);
while(r > p.r) upd(arr[r--], -1);
while(l < p.l) upd(arr[l++], -1);
while(l > p.l) upd(arr[--l], 1);
init();
ans[p.id] = solve();
}
}
int tfreq[MX];
int main(){
scanf("%d %d", &N, &Q);
vector<int> cd;
for(int i = 0; i < N; i++){
cin >> arr[i];
cd.push_back(arr[i]);
}
sort(cd.begin(), cd.end());
cd.resize(unique(cd.begin(), cd.end()) - cd.begin());
for(int i = 0; i < N; i++){
arr[i] = lower_bound(cd.begin(), cd.end(), arr[i]) - cd.begin();
tfreq[arr[i]]++;
}
for(int i = 0; i < N; i++){
if(tfreq[i] >= SQ)
lg.push_back(i);
}
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());
answer();
for(int i = 0; i < Q; i++)
cout << ans[i] << '\n';
}
Compilation message
diversity.cpp: In function 'int main()':
diversity.cpp:117:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
117 | scanf("%d %d", &N, &Q);
| ~~~~~^~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
3 ms |
460 KB |
Output is correct |
4 |
Correct |
22 ms |
1228 KB |
Output is correct |
5 |
Correct |
48 ms |
2396 KB |
Output is correct |
6 |
Correct |
70 ms |
4084 KB |
Output is correct |
7 |
Correct |
64 ms |
4100 KB |
Output is correct |
8 |
Correct |
67 ms |
4020 KB |
Output is correct |
9 |
Correct |
92 ms |
4172 KB |
Output is correct |
10 |
Correct |
89 ms |
4136 KB |
Output is correct |
11 |
Correct |
71 ms |
4152 KB |
Output is correct |
12 |
Correct |
67 ms |
4068 KB |
Output is correct |
13 |
Correct |
73 ms |
4132 KB |
Output is correct |
14 |
Correct |
67 ms |
4024 KB |
Output is correct |
15 |
Correct |
78 ms |
4032 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
3 ms |
460 KB |
Output is correct |
4 |
Correct |
22 ms |
1228 KB |
Output is correct |
5 |
Correct |
48 ms |
2396 KB |
Output is correct |
6 |
Correct |
70 ms |
4084 KB |
Output is correct |
7 |
Correct |
64 ms |
4100 KB |
Output is correct |
8 |
Correct |
67 ms |
4020 KB |
Output is correct |
9 |
Correct |
92 ms |
4172 KB |
Output is correct |
10 |
Correct |
89 ms |
4136 KB |
Output is correct |
11 |
Correct |
71 ms |
4152 KB |
Output is correct |
12 |
Correct |
67 ms |
4068 KB |
Output is correct |
13 |
Correct |
73 ms |
4132 KB |
Output is correct |
14 |
Correct |
67 ms |
4024 KB |
Output is correct |
15 |
Correct |
78 ms |
4032 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
3 ms |
372 KB |
Output is correct |
19 |
Correct |
24 ms |
1504 KB |
Output is correct |
20 |
Correct |
52 ms |
2520 KB |
Output is correct |
21 |
Correct |
76 ms |
4280 KB |
Output is correct |
22 |
Correct |
76 ms |
4256 KB |
Output is correct |
23 |
Correct |
84 ms |
4236 KB |
Output is correct |
24 |
Correct |
97 ms |
4280 KB |
Output is correct |
25 |
Correct |
72 ms |
4272 KB |
Output is correct |
26 |
Correct |
97 ms |
4252 KB |
Output is correct |
27 |
Correct |
87 ms |
4280 KB |
Output is correct |
28 |
Correct |
72 ms |
4204 KB |
Output is correct |
29 |
Correct |
82 ms |
4168 KB |
Output is correct |
30 |
Correct |
73 ms |
4280 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
3 ms |
460 KB |
Output is correct |
4 |
Correct |
22 ms |
1228 KB |
Output is correct |
5 |
Correct |
48 ms |
2396 KB |
Output is correct |
6 |
Correct |
70 ms |
4084 KB |
Output is correct |
7 |
Correct |
64 ms |
4100 KB |
Output is correct |
8 |
Correct |
67 ms |
4020 KB |
Output is correct |
9 |
Correct |
92 ms |
4172 KB |
Output is correct |
10 |
Correct |
89 ms |
4136 KB |
Output is correct |
11 |
Correct |
71 ms |
4152 KB |
Output is correct |
12 |
Correct |
67 ms |
4068 KB |
Output is correct |
13 |
Correct |
73 ms |
4132 KB |
Output is correct |
14 |
Correct |
67 ms |
4024 KB |
Output is correct |
15 |
Correct |
78 ms |
4032 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
3 ms |
372 KB |
Output is correct |
19 |
Correct |
24 ms |
1504 KB |
Output is correct |
20 |
Correct |
52 ms |
2520 KB |
Output is correct |
21 |
Correct |
76 ms |
4280 KB |
Output is correct |
22 |
Correct |
76 ms |
4256 KB |
Output is correct |
23 |
Correct |
84 ms |
4236 KB |
Output is correct |
24 |
Correct |
97 ms |
4280 KB |
Output is correct |
25 |
Correct |
72 ms |
4272 KB |
Output is correct |
26 |
Correct |
97 ms |
4252 KB |
Output is correct |
27 |
Correct |
87 ms |
4280 KB |
Output is correct |
28 |
Correct |
72 ms |
4204 KB |
Output is correct |
29 |
Correct |
82 ms |
4168 KB |
Output is correct |
30 |
Correct |
73 ms |
4280 KB |
Output is correct |
31 |
Correct |
1 ms |
332 KB |
Output is correct |
32 |
Correct |
1 ms |
332 KB |
Output is correct |
33 |
Correct |
1 ms |
332 KB |
Output is correct |
34 |
Correct |
3 ms |
372 KB |
Output is correct |
35 |
Correct |
4 ms |
460 KB |
Output is correct |
36 |
Correct |
4 ms |
460 KB |
Output is correct |
37 |
Correct |
18 ms |
940 KB |
Output is correct |
38 |
Correct |
21 ms |
1032 KB |
Output is correct |
39 |
Correct |
36 ms |
1596 KB |
Output is correct |
40 |
Correct |
80 ms |
2732 KB |
Output is correct |
41 |
Correct |
109 ms |
4560 KB |
Output is correct |
42 |
Correct |
112 ms |
4536 KB |
Output is correct |
43 |
Correct |
107 ms |
4532 KB |
Output is correct |
44 |
Correct |
126 ms |
4600 KB |
Output is correct |
45 |
Correct |
110 ms |
4592 KB |
Output is correct |
46 |
Correct |
111 ms |
4524 KB |
Output is correct |
47 |
Correct |
108 ms |
4592 KB |
Output is correct |
48 |
Correct |
107 ms |
4632 KB |
Output is correct |
49 |
Correct |
116 ms |
4528 KB |
Output is correct |
50 |
Correct |
111 ms |
4536 KB |
Output is correct |
51 |
Correct |
107 ms |
4604 KB |
Output is correct |
52 |
Correct |
122 ms |
4640 KB |
Output is correct |
53 |
Correct |
106 ms |
4628 KB |
Output is correct |
54 |
Correct |
111 ms |
4532 KB |
Output is correct |
55 |
Correct |
111 ms |
4632 KB |
Output is correct |
56 |
Correct |
111 ms |
4616 KB |
Output is correct |
57 |
Correct |
113 ms |
4664 KB |
Output is correct |
58 |
Correct |
116 ms |
4648 KB |
Output is correct |
59 |
Correct |
110 ms |
4700 KB |
Output is correct |
60 |
Correct |
114 ms |
4588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
3 ms |
460 KB |
Output is correct |
14 |
Correct |
22 ms |
1228 KB |
Output is correct |
15 |
Correct |
48 ms |
2396 KB |
Output is correct |
16 |
Correct |
70 ms |
4084 KB |
Output is correct |
17 |
Correct |
64 ms |
4100 KB |
Output is correct |
18 |
Correct |
67 ms |
4020 KB |
Output is correct |
19 |
Correct |
92 ms |
4172 KB |
Output is correct |
20 |
Correct |
89 ms |
4136 KB |
Output is correct |
21 |
Correct |
71 ms |
4152 KB |
Output is correct |
22 |
Correct |
67 ms |
4068 KB |
Output is correct |
23 |
Correct |
73 ms |
4132 KB |
Output is correct |
24 |
Correct |
67 ms |
4024 KB |
Output is correct |
25 |
Correct |
78 ms |
4032 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
1 ms |
332 KB |
Output is correct |
28 |
Correct |
3 ms |
372 KB |
Output is correct |
29 |
Correct |
24 ms |
1504 KB |
Output is correct |
30 |
Correct |
52 ms |
2520 KB |
Output is correct |
31 |
Correct |
76 ms |
4280 KB |
Output is correct |
32 |
Correct |
76 ms |
4256 KB |
Output is correct |
33 |
Correct |
84 ms |
4236 KB |
Output is correct |
34 |
Correct |
97 ms |
4280 KB |
Output is correct |
35 |
Correct |
72 ms |
4272 KB |
Output is correct |
36 |
Correct |
97 ms |
4252 KB |
Output is correct |
37 |
Correct |
87 ms |
4280 KB |
Output is correct |
38 |
Correct |
72 ms |
4204 KB |
Output is correct |
39 |
Correct |
82 ms |
4168 KB |
Output is correct |
40 |
Correct |
73 ms |
4280 KB |
Output is correct |
41 |
Correct |
1 ms |
332 KB |
Output is correct |
42 |
Correct |
1 ms |
332 KB |
Output is correct |
43 |
Correct |
1 ms |
332 KB |
Output is correct |
44 |
Correct |
3 ms |
372 KB |
Output is correct |
45 |
Correct |
4 ms |
460 KB |
Output is correct |
46 |
Correct |
4 ms |
460 KB |
Output is correct |
47 |
Correct |
18 ms |
940 KB |
Output is correct |
48 |
Correct |
21 ms |
1032 KB |
Output is correct |
49 |
Correct |
36 ms |
1596 KB |
Output is correct |
50 |
Correct |
80 ms |
2732 KB |
Output is correct |
51 |
Correct |
109 ms |
4560 KB |
Output is correct |
52 |
Correct |
112 ms |
4536 KB |
Output is correct |
53 |
Correct |
107 ms |
4532 KB |
Output is correct |
54 |
Correct |
126 ms |
4600 KB |
Output is correct |
55 |
Correct |
110 ms |
4592 KB |
Output is correct |
56 |
Correct |
111 ms |
4524 KB |
Output is correct |
57 |
Correct |
108 ms |
4592 KB |
Output is correct |
58 |
Correct |
107 ms |
4632 KB |
Output is correct |
59 |
Correct |
116 ms |
4528 KB |
Output is correct |
60 |
Correct |
111 ms |
4536 KB |
Output is correct |
61 |
Correct |
107 ms |
4604 KB |
Output is correct |
62 |
Correct |
122 ms |
4640 KB |
Output is correct |
63 |
Correct |
106 ms |
4628 KB |
Output is correct |
64 |
Correct |
111 ms |
4532 KB |
Output is correct |
65 |
Correct |
111 ms |
4632 KB |
Output is correct |
66 |
Correct |
111 ms |
4616 KB |
Output is correct |
67 |
Correct |
113 ms |
4664 KB |
Output is correct |
68 |
Correct |
116 ms |
4648 KB |
Output is correct |
69 |
Correct |
110 ms |
4700 KB |
Output is correct |
70 |
Correct |
114 ms |
4588 KB |
Output is correct |
71 |
Correct |
20 ms |
972 KB |
Output is correct |
72 |
Correct |
21 ms |
1052 KB |
Output is correct |
73 |
Correct |
22 ms |
972 KB |
Output is correct |
74 |
Correct |
21 ms |
1040 KB |
Output is correct |
75 |
Correct |
20 ms |
1040 KB |
Output is correct |
76 |
Incorrect |
44 ms |
1640 KB |
Output isn't correct |
77 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
3 ms |
460 KB |
Output is correct |
14 |
Correct |
22 ms |
1228 KB |
Output is correct |
15 |
Correct |
48 ms |
2396 KB |
Output is correct |
16 |
Correct |
70 ms |
4084 KB |
Output is correct |
17 |
Correct |
64 ms |
4100 KB |
Output is correct |
18 |
Correct |
67 ms |
4020 KB |
Output is correct |
19 |
Correct |
92 ms |
4172 KB |
Output is correct |
20 |
Correct |
89 ms |
4136 KB |
Output is correct |
21 |
Correct |
71 ms |
4152 KB |
Output is correct |
22 |
Correct |
67 ms |
4068 KB |
Output is correct |
23 |
Correct |
73 ms |
4132 KB |
Output is correct |
24 |
Correct |
67 ms |
4024 KB |
Output is correct |
25 |
Correct |
78 ms |
4032 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
1 ms |
332 KB |
Output is correct |
28 |
Correct |
3 ms |
372 KB |
Output is correct |
29 |
Correct |
24 ms |
1504 KB |
Output is correct |
30 |
Correct |
52 ms |
2520 KB |
Output is correct |
31 |
Correct |
76 ms |
4280 KB |
Output is correct |
32 |
Correct |
76 ms |
4256 KB |
Output is correct |
33 |
Correct |
84 ms |
4236 KB |
Output is correct |
34 |
Correct |
97 ms |
4280 KB |
Output is correct |
35 |
Correct |
72 ms |
4272 KB |
Output is correct |
36 |
Correct |
97 ms |
4252 KB |
Output is correct |
37 |
Correct |
87 ms |
4280 KB |
Output is correct |
38 |
Correct |
72 ms |
4204 KB |
Output is correct |
39 |
Correct |
82 ms |
4168 KB |
Output is correct |
40 |
Correct |
73 ms |
4280 KB |
Output is correct |
41 |
Correct |
1 ms |
332 KB |
Output is correct |
42 |
Correct |
1 ms |
332 KB |
Output is correct |
43 |
Correct |
1 ms |
332 KB |
Output is correct |
44 |
Correct |
3 ms |
372 KB |
Output is correct |
45 |
Correct |
4 ms |
460 KB |
Output is correct |
46 |
Correct |
4 ms |
460 KB |
Output is correct |
47 |
Correct |
18 ms |
940 KB |
Output is correct |
48 |
Correct |
21 ms |
1032 KB |
Output is correct |
49 |
Correct |
36 ms |
1596 KB |
Output is correct |
50 |
Correct |
80 ms |
2732 KB |
Output is correct |
51 |
Correct |
109 ms |
4560 KB |
Output is correct |
52 |
Correct |
112 ms |
4536 KB |
Output is correct |
53 |
Correct |
107 ms |
4532 KB |
Output is correct |
54 |
Correct |
126 ms |
4600 KB |
Output is correct |
55 |
Correct |
110 ms |
4592 KB |
Output is correct |
56 |
Correct |
111 ms |
4524 KB |
Output is correct |
57 |
Correct |
108 ms |
4592 KB |
Output is correct |
58 |
Correct |
107 ms |
4632 KB |
Output is correct |
59 |
Correct |
116 ms |
4528 KB |
Output is correct |
60 |
Correct |
111 ms |
4536 KB |
Output is correct |
61 |
Correct |
107 ms |
4604 KB |
Output is correct |
62 |
Correct |
122 ms |
4640 KB |
Output is correct |
63 |
Correct |
106 ms |
4628 KB |
Output is correct |
64 |
Correct |
111 ms |
4532 KB |
Output is correct |
65 |
Correct |
111 ms |
4632 KB |
Output is correct |
66 |
Correct |
111 ms |
4616 KB |
Output is correct |
67 |
Correct |
113 ms |
4664 KB |
Output is correct |
68 |
Correct |
116 ms |
4648 KB |
Output is correct |
69 |
Correct |
110 ms |
4700 KB |
Output is correct |
70 |
Correct |
114 ms |
4588 KB |
Output is correct |
71 |
Correct |
20 ms |
972 KB |
Output is correct |
72 |
Correct |
21 ms |
1052 KB |
Output is correct |
73 |
Correct |
22 ms |
972 KB |
Output is correct |
74 |
Correct |
21 ms |
1040 KB |
Output is correct |
75 |
Correct |
20 ms |
1040 KB |
Output is correct |
76 |
Incorrect |
44 ms |
1640 KB |
Output isn't correct |
77 |
Halted |
0 ms |
0 KB |
- |