//#pragma GCC optimize("Ofast,unroll-loops")
#include <bits/stdc++.h>
using namespace std;
typedef long long llo;
#define mp make_pair
#define pb push_back
#define a first
#define b second
#define endl '\n'
//vector<llo> adj[200001];
llo it[300001];
llo dp[300001];//[30001];
llo dp2[300001];
llo pre[300001];
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
llo n,q;
cin>>n>>q;
map<llo,llo> ss;
for(llo i=0;i<n;i++){
cin>>it[i];
ss[it[i]]++;
}
vector<llo> tt;
for(auto j:ss){
tt.pb(j.b);
}
sort(tt.begin(),tt.end());
for(llo i=1;i<=tt.size();i++){
pre[i]=pre[i-1]+tt[i-1];
}
llo m=tt.size();
llo ans=m*n*(n+1)-(m-1)*n;
if(m==1){
}
else{
for(llo i=0;i<tt.size();i++){
for(llo j=0;j<=n;j++){
dp[j]=-(llo)1e18;
}
if(i==0){
dp[tt[0]]=tt[0]*tt[0]+(n-tt[0])*(n-tt[0]);
}
else{
for(llo j=0;j<=n;j++){
llo cur=j+pre[tt.size()]-pre[i];
llo su=0;
su+=cur*cur;
su+=(n-cur)*(n-cur);
if(cur==n or cur==0){
su=0;
}
dp[j]=max(dp[j],dp2[j]+su);
if(j>=tt[i]){
llo cur=j;
llo su=0;
su+=cur*cur;
su+=(n-cur)*(n-cur);
if(cur==n or cur==0){
su=0;
}
dp[j]=max(dp[j],dp2[j-tt[i]]+su);
}
}
}
for(int j=0;j<=n;j++){
dp2[j]=dp[j];
}
}
llo ma=0;
for(llo i=0;i<=n;i++){
ma=max(ma,dp[i]);
}
ans-=ma;
}
ans/=2;
//divide ans/2
while(q--){
llo l,r;
cin>>l>>r;
l--;
r--;
cout<<ans<<endl;
}
return 0;
}
Compilation message
diversity.cpp: In function 'int main()':
diversity.cpp:31:15: warning: comparison of integer expressions of different signedness: 'llo' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
31 | for(llo i=1;i<=tt.size();i++){
| ~^~~~~~~~~~~
diversity.cpp:41:16: warning: comparison of integer expressions of different signedness: 'llo' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
41 | for(llo i=0;i<tt.size();i++){
| ~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
1 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 |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
460 KB |
Output is correct |
4 |
Correct |
13 ms |
2636 KB |
Output is correct |
5 |
Correct |
35 ms |
5152 KB |
Output is correct |
6 |
Correct |
41 ms |
7468 KB |
Output is correct |
7 |
Correct |
43 ms |
7408 KB |
Output is correct |
8 |
Correct |
42 ms |
7460 KB |
Output is correct |
9 |
Correct |
39 ms |
7492 KB |
Output is correct |
10 |
Correct |
38 ms |
7364 KB |
Output is correct |
11 |
Correct |
60 ms |
7492 KB |
Output is correct |
12 |
Correct |
42 ms |
7484 KB |
Output is correct |
13 |
Correct |
57 ms |
7404 KB |
Output is correct |
14 |
Correct |
48 ms |
7492 KB |
Output is correct |
15 |
Correct |
43 ms |
7412 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
460 KB |
Output is correct |
4 |
Correct |
13 ms |
2636 KB |
Output is correct |
5 |
Correct |
35 ms |
5152 KB |
Output is correct |
6 |
Correct |
41 ms |
7468 KB |
Output is correct |
7 |
Correct |
43 ms |
7408 KB |
Output is correct |
8 |
Correct |
42 ms |
7460 KB |
Output is correct |
9 |
Correct |
39 ms |
7492 KB |
Output is correct |
10 |
Correct |
38 ms |
7364 KB |
Output is correct |
11 |
Correct |
60 ms |
7492 KB |
Output is correct |
12 |
Correct |
42 ms |
7484 KB |
Output is correct |
13 |
Correct |
57 ms |
7404 KB |
Output is correct |
14 |
Correct |
48 ms |
7492 KB |
Output is correct |
15 |
Correct |
43 ms |
7412 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
2 ms |
588 KB |
Output is correct |
19 |
Correct |
26 ms |
2824 KB |
Output is correct |
20 |
Correct |
39 ms |
5144 KB |
Output is correct |
21 |
Correct |
70 ms |
7496 KB |
Output is correct |
22 |
Correct |
76 ms |
7376 KB |
Output is correct |
23 |
Correct |
63 ms |
7420 KB |
Output is correct |
24 |
Correct |
62 ms |
7428 KB |
Output is correct |
25 |
Correct |
68 ms |
7496 KB |
Output is correct |
26 |
Correct |
64 ms |
7480 KB |
Output is correct |
27 |
Correct |
64 ms |
7488 KB |
Output is correct |
28 |
Correct |
62 ms |
7488 KB |
Output is correct |
29 |
Correct |
59 ms |
7488 KB |
Output is correct |
30 |
Correct |
70 ms |
7488 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
460 KB |
Output is correct |
4 |
Correct |
13 ms |
2636 KB |
Output is correct |
5 |
Correct |
35 ms |
5152 KB |
Output is correct |
6 |
Correct |
41 ms |
7468 KB |
Output is correct |
7 |
Correct |
43 ms |
7408 KB |
Output is correct |
8 |
Correct |
42 ms |
7460 KB |
Output is correct |
9 |
Correct |
39 ms |
7492 KB |
Output is correct |
10 |
Correct |
38 ms |
7364 KB |
Output is correct |
11 |
Correct |
60 ms |
7492 KB |
Output is correct |
12 |
Correct |
42 ms |
7484 KB |
Output is correct |
13 |
Correct |
57 ms |
7404 KB |
Output is correct |
14 |
Correct |
48 ms |
7492 KB |
Output is correct |
15 |
Correct |
43 ms |
7412 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
2 ms |
588 KB |
Output is correct |
19 |
Correct |
26 ms |
2824 KB |
Output is correct |
20 |
Correct |
39 ms |
5144 KB |
Output is correct |
21 |
Correct |
70 ms |
7496 KB |
Output is correct |
22 |
Correct |
76 ms |
7376 KB |
Output is correct |
23 |
Correct |
63 ms |
7420 KB |
Output is correct |
24 |
Correct |
62 ms |
7428 KB |
Output is correct |
25 |
Correct |
68 ms |
7496 KB |
Output is correct |
26 |
Correct |
64 ms |
7480 KB |
Output is correct |
27 |
Correct |
64 ms |
7488 KB |
Output is correct |
28 |
Correct |
62 ms |
7488 KB |
Output is correct |
29 |
Correct |
59 ms |
7488 KB |
Output is correct |
30 |
Correct |
70 ms |
7488 KB |
Output is correct |
31 |
Correct |
1 ms |
332 KB |
Output is correct |
32 |
Correct |
1 ms |
376 KB |
Output is correct |
33 |
Correct |
2 ms |
332 KB |
Output is correct |
34 |
Correct |
10 ms |
604 KB |
Output is correct |
35 |
Correct |
15 ms |
516 KB |
Output is correct |
36 |
Correct |
46 ms |
680 KB |
Output is correct |
37 |
Correct |
149 ms |
1664 KB |
Output is correct |
38 |
Correct |
236 ms |
1712 KB |
Output is correct |
39 |
Correct |
534 ms |
2900 KB |
Output is correct |
40 |
Correct |
1102 ms |
5232 KB |
Output is correct |
41 |
Correct |
1437 ms |
7580 KB |
Output is correct |
42 |
Correct |
1519 ms |
7572 KB |
Output is correct |
43 |
Correct |
1422 ms |
7572 KB |
Output is correct |
44 |
Correct |
1424 ms |
7576 KB |
Output is correct |
45 |
Correct |
1381 ms |
7576 KB |
Output is correct |
46 |
Correct |
1431 ms |
7572 KB |
Output is correct |
47 |
Correct |
1376 ms |
7572 KB |
Output is correct |
48 |
Correct |
1421 ms |
7576 KB |
Output is correct |
49 |
Correct |
1353 ms |
7576 KB |
Output is correct |
50 |
Correct |
1371 ms |
7580 KB |
Output is correct |
51 |
Correct |
1339 ms |
7644 KB |
Output is correct |
52 |
Correct |
1375 ms |
7572 KB |
Output is correct |
53 |
Correct |
1381 ms |
7632 KB |
Output is correct |
54 |
Correct |
1370 ms |
7572 KB |
Output is correct |
55 |
Correct |
1410 ms |
7572 KB |
Output is correct |
56 |
Correct |
1411 ms |
7580 KB |
Output is correct |
57 |
Correct |
1395 ms |
7576 KB |
Output is correct |
58 |
Correct |
1442 ms |
7620 KB |
Output is correct |
59 |
Correct |
1413 ms |
7576 KB |
Output is correct |
60 |
Correct |
1369 ms |
7576 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
1 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 |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
460 KB |
Output is correct |
14 |
Correct |
13 ms |
2636 KB |
Output is correct |
15 |
Correct |
35 ms |
5152 KB |
Output is correct |
16 |
Correct |
41 ms |
7468 KB |
Output is correct |
17 |
Correct |
43 ms |
7408 KB |
Output is correct |
18 |
Correct |
42 ms |
7460 KB |
Output is correct |
19 |
Correct |
39 ms |
7492 KB |
Output is correct |
20 |
Correct |
38 ms |
7364 KB |
Output is correct |
21 |
Correct |
60 ms |
7492 KB |
Output is correct |
22 |
Correct |
42 ms |
7484 KB |
Output is correct |
23 |
Correct |
57 ms |
7404 KB |
Output is correct |
24 |
Correct |
48 ms |
7492 KB |
Output is correct |
25 |
Correct |
43 ms |
7412 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
1 ms |
332 KB |
Output is correct |
28 |
Correct |
2 ms |
588 KB |
Output is correct |
29 |
Correct |
26 ms |
2824 KB |
Output is correct |
30 |
Correct |
39 ms |
5144 KB |
Output is correct |
31 |
Correct |
70 ms |
7496 KB |
Output is correct |
32 |
Correct |
76 ms |
7376 KB |
Output is correct |
33 |
Correct |
63 ms |
7420 KB |
Output is correct |
34 |
Correct |
62 ms |
7428 KB |
Output is correct |
35 |
Correct |
68 ms |
7496 KB |
Output is correct |
36 |
Correct |
64 ms |
7480 KB |
Output is correct |
37 |
Correct |
64 ms |
7488 KB |
Output is correct |
38 |
Correct |
62 ms |
7488 KB |
Output is correct |
39 |
Correct |
59 ms |
7488 KB |
Output is correct |
40 |
Correct |
70 ms |
7488 KB |
Output is correct |
41 |
Correct |
1 ms |
332 KB |
Output is correct |
42 |
Correct |
1 ms |
376 KB |
Output is correct |
43 |
Correct |
2 ms |
332 KB |
Output is correct |
44 |
Correct |
10 ms |
604 KB |
Output is correct |
45 |
Correct |
15 ms |
516 KB |
Output is correct |
46 |
Correct |
46 ms |
680 KB |
Output is correct |
47 |
Correct |
149 ms |
1664 KB |
Output is correct |
48 |
Correct |
236 ms |
1712 KB |
Output is correct |
49 |
Correct |
534 ms |
2900 KB |
Output is correct |
50 |
Correct |
1102 ms |
5232 KB |
Output is correct |
51 |
Correct |
1437 ms |
7580 KB |
Output is correct |
52 |
Correct |
1519 ms |
7572 KB |
Output is correct |
53 |
Correct |
1422 ms |
7572 KB |
Output is correct |
54 |
Correct |
1424 ms |
7576 KB |
Output is correct |
55 |
Correct |
1381 ms |
7576 KB |
Output is correct |
56 |
Correct |
1431 ms |
7572 KB |
Output is correct |
57 |
Correct |
1376 ms |
7572 KB |
Output is correct |
58 |
Correct |
1421 ms |
7576 KB |
Output is correct |
59 |
Correct |
1353 ms |
7576 KB |
Output is correct |
60 |
Correct |
1371 ms |
7580 KB |
Output is correct |
61 |
Correct |
1339 ms |
7644 KB |
Output is correct |
62 |
Correct |
1375 ms |
7572 KB |
Output is correct |
63 |
Correct |
1381 ms |
7632 KB |
Output is correct |
64 |
Correct |
1370 ms |
7572 KB |
Output is correct |
65 |
Correct |
1410 ms |
7572 KB |
Output is correct |
66 |
Correct |
1411 ms |
7580 KB |
Output is correct |
67 |
Correct |
1395 ms |
7576 KB |
Output is correct |
68 |
Correct |
1442 ms |
7620 KB |
Output is correct |
69 |
Correct |
1413 ms |
7576 KB |
Output is correct |
70 |
Correct |
1369 ms |
7576 KB |
Output is correct |
71 |
Correct |
455 ms |
1808 KB |
Output is correct |
72 |
Correct |
449 ms |
1740 KB |
Output is correct |
73 |
Correct |
440 ms |
1804 KB |
Output is correct |
74 |
Correct |
446 ms |
1804 KB |
Output is correct |
75 |
Correct |
442 ms |
1740 KB |
Output is correct |
76 |
Correct |
2233 ms |
3276 KB |
Output is correct |
77 |
Correct |
2188 ms |
3276 KB |
Output is correct |
78 |
Correct |
2228 ms |
3268 KB |
Output is correct |
79 |
Correct |
2234 ms |
3272 KB |
Output is correct |
80 |
Correct |
2237 ms |
3276 KB |
Output is correct |
81 |
Execution timed out |
7063 ms |
5972 KB |
Time limit exceeded |
82 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
1 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 |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
460 KB |
Output is correct |
14 |
Correct |
13 ms |
2636 KB |
Output is correct |
15 |
Correct |
35 ms |
5152 KB |
Output is correct |
16 |
Correct |
41 ms |
7468 KB |
Output is correct |
17 |
Correct |
43 ms |
7408 KB |
Output is correct |
18 |
Correct |
42 ms |
7460 KB |
Output is correct |
19 |
Correct |
39 ms |
7492 KB |
Output is correct |
20 |
Correct |
38 ms |
7364 KB |
Output is correct |
21 |
Correct |
60 ms |
7492 KB |
Output is correct |
22 |
Correct |
42 ms |
7484 KB |
Output is correct |
23 |
Correct |
57 ms |
7404 KB |
Output is correct |
24 |
Correct |
48 ms |
7492 KB |
Output is correct |
25 |
Correct |
43 ms |
7412 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
1 ms |
332 KB |
Output is correct |
28 |
Correct |
2 ms |
588 KB |
Output is correct |
29 |
Correct |
26 ms |
2824 KB |
Output is correct |
30 |
Correct |
39 ms |
5144 KB |
Output is correct |
31 |
Correct |
70 ms |
7496 KB |
Output is correct |
32 |
Correct |
76 ms |
7376 KB |
Output is correct |
33 |
Correct |
63 ms |
7420 KB |
Output is correct |
34 |
Correct |
62 ms |
7428 KB |
Output is correct |
35 |
Correct |
68 ms |
7496 KB |
Output is correct |
36 |
Correct |
64 ms |
7480 KB |
Output is correct |
37 |
Correct |
64 ms |
7488 KB |
Output is correct |
38 |
Correct |
62 ms |
7488 KB |
Output is correct |
39 |
Correct |
59 ms |
7488 KB |
Output is correct |
40 |
Correct |
70 ms |
7488 KB |
Output is correct |
41 |
Correct |
1 ms |
332 KB |
Output is correct |
42 |
Correct |
1 ms |
376 KB |
Output is correct |
43 |
Correct |
2 ms |
332 KB |
Output is correct |
44 |
Correct |
10 ms |
604 KB |
Output is correct |
45 |
Correct |
15 ms |
516 KB |
Output is correct |
46 |
Correct |
46 ms |
680 KB |
Output is correct |
47 |
Correct |
149 ms |
1664 KB |
Output is correct |
48 |
Correct |
236 ms |
1712 KB |
Output is correct |
49 |
Correct |
534 ms |
2900 KB |
Output is correct |
50 |
Correct |
1102 ms |
5232 KB |
Output is correct |
51 |
Correct |
1437 ms |
7580 KB |
Output is correct |
52 |
Correct |
1519 ms |
7572 KB |
Output is correct |
53 |
Correct |
1422 ms |
7572 KB |
Output is correct |
54 |
Correct |
1424 ms |
7576 KB |
Output is correct |
55 |
Correct |
1381 ms |
7576 KB |
Output is correct |
56 |
Correct |
1431 ms |
7572 KB |
Output is correct |
57 |
Correct |
1376 ms |
7572 KB |
Output is correct |
58 |
Correct |
1421 ms |
7576 KB |
Output is correct |
59 |
Correct |
1353 ms |
7576 KB |
Output is correct |
60 |
Correct |
1371 ms |
7580 KB |
Output is correct |
61 |
Correct |
1339 ms |
7644 KB |
Output is correct |
62 |
Correct |
1375 ms |
7572 KB |
Output is correct |
63 |
Correct |
1381 ms |
7632 KB |
Output is correct |
64 |
Correct |
1370 ms |
7572 KB |
Output is correct |
65 |
Correct |
1410 ms |
7572 KB |
Output is correct |
66 |
Correct |
1411 ms |
7580 KB |
Output is correct |
67 |
Correct |
1395 ms |
7576 KB |
Output is correct |
68 |
Correct |
1442 ms |
7620 KB |
Output is correct |
69 |
Correct |
1413 ms |
7576 KB |
Output is correct |
70 |
Correct |
1369 ms |
7576 KB |
Output is correct |
71 |
Correct |
455 ms |
1808 KB |
Output is correct |
72 |
Correct |
449 ms |
1740 KB |
Output is correct |
73 |
Correct |
440 ms |
1804 KB |
Output is correct |
74 |
Correct |
446 ms |
1804 KB |
Output is correct |
75 |
Correct |
442 ms |
1740 KB |
Output is correct |
76 |
Correct |
2233 ms |
3276 KB |
Output is correct |
77 |
Correct |
2188 ms |
3276 KB |
Output is correct |
78 |
Correct |
2228 ms |
3268 KB |
Output is correct |
79 |
Correct |
2234 ms |
3272 KB |
Output is correct |
80 |
Correct |
2237 ms |
3276 KB |
Output is correct |
81 |
Execution timed out |
7063 ms |
5972 KB |
Time limit exceeded |
82 |
Halted |
0 ms |
0 KB |
- |