#include <bits/stdc++.h>
using namespace std;
#define int long long
vector<int> freq(300005);
vector<int> s, does(300005);
int val(int a, int b, int c)
{
return a*c*(a+(c-1)*b)+b*b*((((c-1)*c*(2*c-1))/6));
}
int get()
{
int k=0, n=0;
int mod=1;
sort(s.begin(),s.end());
vector<int> cur;
int l=0, r=0, ans=0;
for(auto x: s)
{
int i=x, j=freq[x];
does[i]=0;
if(j==0) continue;
does[i]=1;
cur.push_back(i);
k+=j;
n+=(i*j);
}
for(auto x: cur)
{
int i=x, j=freq[x];
int addleft=(j+mod)/2;
int addright=j-addleft;
ans+=val(l, i, addleft);
ans+=val(n-l-i, i, addleft);
ans+=val(r, i, addright);
ans+=val(n-r-i, i, addright);
l+=addleft*i;
r+=addright*i;
mod=(j+mod)%2;
}
s=cur;
return (k*n*(n+1)-n*(k-1)-ans)/2;
}
int32_t main()
{
ios_base::sync_with_stdio(false); cin.tie(0);
int n, q, b=550, cnt=0; cin>>n>>q;
vector<int> a(n), ans(q);
vector<array<int, 3>> query[n/b+2];
for(int i=0; i<n; i++) cin>>a[i];
while(q--)
{
int l, r; cin>>l>>r;
l--; r--;
query[l/b].push_back({r, l, ++cnt});
}
int l=0, r=-1;
vector<int> count(3000005);
for(int i=0; i<n/b+1; i++)
{
sort(query[i].begin(), query[i].end());
for(auto x: query[i])
{
swap(x[0], x[1]);
while(x[0]>l)
{
count[a[l]]--;
if(count[a[l]]!=0)
freq[count[a[l]]]++;
freq[count[a[l]]+1]--;
if(does[count[a[l]]]==0&&count[a[l]]!=0) does[count[a[l]]]=1,s.push_back(count[a[l]]);
l++;
}
while(x[0]<l)
{
l--;
count[a[l]]++;
freq[count[a[l]]]++;
if(freq[count[a[l]]-1]>0)
freq[count[a[l]]-1]--;
if(does[count[a[l]]]==0&&count[a[l]]!=0) does[count[a[l]]]=1, s.push_back(count[a[l]]);
}
while(x[1]<r)
{
if(count[a[r]]!=0)
freq[count[a[r]]]--;
count[a[r]]--;
freq[count[a[r]]]++;
if(does[count[a[r]]]==0&&count[a[r]]!=0)does[count[a[r]]]=1, s.push_back(count[a[r]]);
r--;
}
while(x[1]>r)
{
r++;
if(count[a[r]]!=0)
freq[count[a[r]]]--;
count[a[r]]++;
freq[count[a[r]]]++;
if(does[count[a[r]]]==0&&count[a[r]]!=0)does[count[a[r]]]=1, s.push_back(count[a[r]]);
}
ans[x[2]-1]=get();
}
}
for(int i=0; i<ans.size(); i++) cout<<ans[i]<<"\n";
}
Compilation message
diversity.cpp: In function 'int32_t main()':
diversity.cpp:137:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
137 | for(int i=0; i<ans.size(); i++) cout<<ans[i]<<"\n";
| ~^~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
28500 KB |
Output is correct |
2 |
Correct |
13 ms |
28472 KB |
Output is correct |
3 |
Correct |
14 ms |
28456 KB |
Output is correct |
4 |
Correct |
15 ms |
28420 KB |
Output is correct |
5 |
Correct |
13 ms |
28500 KB |
Output is correct |
6 |
Incorrect |
12 ms |
28456 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
28500 KB |
Output is correct |
2 |
Correct |
15 ms |
28488 KB |
Output is correct |
3 |
Correct |
14 ms |
28560 KB |
Output is correct |
4 |
Correct |
20 ms |
29872 KB |
Output is correct |
5 |
Correct |
33 ms |
31536 KB |
Output is correct |
6 |
Correct |
37 ms |
32128 KB |
Output is correct |
7 |
Correct |
37 ms |
33608 KB |
Output is correct |
8 |
Correct |
35 ms |
32080 KB |
Output is correct |
9 |
Correct |
34 ms |
32080 KB |
Output is correct |
10 |
Correct |
41 ms |
32600 KB |
Output is correct |
11 |
Correct |
34 ms |
32544 KB |
Output is correct |
12 |
Correct |
40 ms |
32096 KB |
Output is correct |
13 |
Correct |
33 ms |
32068 KB |
Output is correct |
14 |
Correct |
33 ms |
32588 KB |
Output is correct |
15 |
Correct |
33 ms |
32636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
28500 KB |
Output is correct |
2 |
Correct |
15 ms |
28488 KB |
Output is correct |
3 |
Correct |
14 ms |
28560 KB |
Output is correct |
4 |
Correct |
20 ms |
29872 KB |
Output is correct |
5 |
Correct |
33 ms |
31536 KB |
Output is correct |
6 |
Correct |
37 ms |
32128 KB |
Output is correct |
7 |
Correct |
37 ms |
33608 KB |
Output is correct |
8 |
Correct |
35 ms |
32080 KB |
Output is correct |
9 |
Correct |
34 ms |
32080 KB |
Output is correct |
10 |
Correct |
41 ms |
32600 KB |
Output is correct |
11 |
Correct |
34 ms |
32544 KB |
Output is correct |
12 |
Correct |
40 ms |
32096 KB |
Output is correct |
13 |
Correct |
33 ms |
32068 KB |
Output is correct |
14 |
Correct |
33 ms |
32588 KB |
Output is correct |
15 |
Correct |
33 ms |
32636 KB |
Output is correct |
16 |
Correct |
12 ms |
28500 KB |
Output is correct |
17 |
Correct |
11 ms |
28472 KB |
Output is correct |
18 |
Correct |
11 ms |
28588 KB |
Output is correct |
19 |
Correct |
21 ms |
29856 KB |
Output is correct |
20 |
Correct |
26 ms |
30952 KB |
Output is correct |
21 |
Correct |
33 ms |
32200 KB |
Output is correct |
22 |
Correct |
34 ms |
32252 KB |
Output is correct |
23 |
Correct |
36 ms |
32208 KB |
Output is correct |
24 |
Correct |
34 ms |
32196 KB |
Output is correct |
25 |
Correct |
37 ms |
32196 KB |
Output is correct |
26 |
Correct |
36 ms |
32200 KB |
Output is correct |
27 |
Correct |
42 ms |
32196 KB |
Output is correct |
28 |
Correct |
35 ms |
32716 KB |
Output is correct |
29 |
Correct |
34 ms |
32188 KB |
Output is correct |
30 |
Correct |
35 ms |
32204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
28500 KB |
Output is correct |
2 |
Correct |
15 ms |
28488 KB |
Output is correct |
3 |
Correct |
14 ms |
28560 KB |
Output is correct |
4 |
Correct |
20 ms |
29872 KB |
Output is correct |
5 |
Correct |
33 ms |
31536 KB |
Output is correct |
6 |
Correct |
37 ms |
32128 KB |
Output is correct |
7 |
Correct |
37 ms |
33608 KB |
Output is correct |
8 |
Correct |
35 ms |
32080 KB |
Output is correct |
9 |
Correct |
34 ms |
32080 KB |
Output is correct |
10 |
Correct |
41 ms |
32600 KB |
Output is correct |
11 |
Correct |
34 ms |
32544 KB |
Output is correct |
12 |
Correct |
40 ms |
32096 KB |
Output is correct |
13 |
Correct |
33 ms |
32068 KB |
Output is correct |
14 |
Correct |
33 ms |
32588 KB |
Output is correct |
15 |
Correct |
33 ms |
32636 KB |
Output is correct |
16 |
Correct |
12 ms |
28500 KB |
Output is correct |
17 |
Correct |
11 ms |
28472 KB |
Output is correct |
18 |
Correct |
11 ms |
28588 KB |
Output is correct |
19 |
Correct |
21 ms |
29856 KB |
Output is correct |
20 |
Correct |
26 ms |
30952 KB |
Output is correct |
21 |
Correct |
33 ms |
32200 KB |
Output is correct |
22 |
Correct |
34 ms |
32252 KB |
Output is correct |
23 |
Correct |
36 ms |
32208 KB |
Output is correct |
24 |
Correct |
34 ms |
32196 KB |
Output is correct |
25 |
Correct |
37 ms |
32196 KB |
Output is correct |
26 |
Correct |
36 ms |
32200 KB |
Output is correct |
27 |
Correct |
42 ms |
32196 KB |
Output is correct |
28 |
Correct |
35 ms |
32716 KB |
Output is correct |
29 |
Correct |
34 ms |
32188 KB |
Output is correct |
30 |
Correct |
35 ms |
32204 KB |
Output is correct |
31 |
Incorrect |
13 ms |
28500 KB |
Output isn't correct |
32 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
28500 KB |
Output is correct |
2 |
Correct |
13 ms |
28472 KB |
Output is correct |
3 |
Correct |
14 ms |
28456 KB |
Output is correct |
4 |
Correct |
15 ms |
28420 KB |
Output is correct |
5 |
Correct |
13 ms |
28500 KB |
Output is correct |
6 |
Incorrect |
12 ms |
28456 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
28500 KB |
Output is correct |
2 |
Correct |
13 ms |
28472 KB |
Output is correct |
3 |
Correct |
14 ms |
28456 KB |
Output is correct |
4 |
Correct |
15 ms |
28420 KB |
Output is correct |
5 |
Correct |
13 ms |
28500 KB |
Output is correct |
6 |
Incorrect |
12 ms |
28456 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |