#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef vector<ll> vi;
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
const ll INF = 1e18;
int N, Q;
vi A, AA;
ll ans;
vi P;
vi answerPermutation;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin >> N >> Q;
AA.resize(N);
for (int i = 0; i < N; ++i)
cin >> AA[i];
while (Q--)
{
int l, r;
cin >> l >> r;
--l;
ll n = r - l;
A = vi(AA.begin() + l, AA.begin() + r);
vi heatMap(*max_element(all(A)) + 1, 0);
for (int i = 0; i < sz(A); ++i)
++heatMap[A[i]];
vi cnts;
for (int i = 0; i < sz(heatMap); ++i)
if (heatMap[i])
cnts.push_back(heatMap[i]);
sort(all(cnts));
vi pref(sz(cnts));
partial_sum(all(cnts), pref.begin());
ll num = sz(cnts);
vi dp[2];
for (int foo = 0; foo < 2; ++foo)
dp[foo].reserve(N + 1), dp[foo].resize(N + 1);
fill(all(dp[0]), INF);
dp[0][0] = n * (n + 1) * (n + 1) / 2 - (2 * n + 1) * (n + 1) * n / 6;
for (int j = 0; j < num; ++j)
{
int ii = j & 1;
int ni = ii ^ 1;
fill(all(dp[ni]), INF);
ll x = cnts[j];
ll subsure = 0;
// either with left or right endpoint in set
subsure += (n - x) * x * (x - 1) / 2;
// if both are in set
subsure += x * x * (x - 1) / 2 - (2 * x - 1) * x * (x - 1) / 6;
for (ll left = 0; left < sz(dp[ii]); ++left)
{
ll tmp = dp[ii][left];
if (tmp == INF)
continue;
tmp -= subsure;
// left
dp[ni][left + x] = min(dp[ni][left + x], tmp - (x - 1) * left * (n - left - x));
// right
ll right = pref[j] - x - left;
dp[ni][left] = min(dp[ni][left], tmp - (x - 1) * right * (n - right - x));
}
}
ans = *min_element(all(dp[num & 1]));
cout << ans << "\n";
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
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 |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
300 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
320 KB |
Output is correct |
3 |
Correct |
1 ms |
596 KB |
Output is correct |
4 |
Correct |
6 ms |
3540 KB |
Output is correct |
5 |
Correct |
12 ms |
6904 KB |
Output is correct |
6 |
Correct |
19 ms |
10324 KB |
Output is correct |
7 |
Correct |
19 ms |
10184 KB |
Output is correct |
8 |
Correct |
19 ms |
10324 KB |
Output is correct |
9 |
Correct |
19 ms |
10324 KB |
Output is correct |
10 |
Correct |
19 ms |
10348 KB |
Output is correct |
11 |
Correct |
20 ms |
10344 KB |
Output is correct |
12 |
Correct |
20 ms |
10368 KB |
Output is correct |
13 |
Correct |
19 ms |
10284 KB |
Output is correct |
14 |
Correct |
19 ms |
10324 KB |
Output is correct |
15 |
Correct |
19 ms |
10340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
320 KB |
Output is correct |
3 |
Correct |
1 ms |
596 KB |
Output is correct |
4 |
Correct |
6 ms |
3540 KB |
Output is correct |
5 |
Correct |
12 ms |
6904 KB |
Output is correct |
6 |
Correct |
19 ms |
10324 KB |
Output is correct |
7 |
Correct |
19 ms |
10184 KB |
Output is correct |
8 |
Correct |
19 ms |
10324 KB |
Output is correct |
9 |
Correct |
19 ms |
10324 KB |
Output is correct |
10 |
Correct |
19 ms |
10348 KB |
Output is correct |
11 |
Correct |
20 ms |
10344 KB |
Output is correct |
12 |
Correct |
20 ms |
10368 KB |
Output is correct |
13 |
Correct |
19 ms |
10284 KB |
Output is correct |
14 |
Correct |
19 ms |
10324 KB |
Output is correct |
15 |
Correct |
19 ms |
10340 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
596 KB |
Output is correct |
19 |
Correct |
9 ms |
3796 KB |
Output is correct |
20 |
Correct |
17 ms |
7004 KB |
Output is correct |
21 |
Correct |
28 ms |
10468 KB |
Output is correct |
22 |
Correct |
26 ms |
10488 KB |
Output is correct |
23 |
Correct |
27 ms |
10380 KB |
Output is correct |
24 |
Correct |
26 ms |
10364 KB |
Output is correct |
25 |
Correct |
26 ms |
10472 KB |
Output is correct |
26 |
Correct |
27 ms |
10472 KB |
Output is correct |
27 |
Correct |
26 ms |
10468 KB |
Output is correct |
28 |
Correct |
26 ms |
10416 KB |
Output is correct |
29 |
Correct |
26 ms |
10444 KB |
Output is correct |
30 |
Correct |
26 ms |
10512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
320 KB |
Output is correct |
3 |
Correct |
1 ms |
596 KB |
Output is correct |
4 |
Correct |
6 ms |
3540 KB |
Output is correct |
5 |
Correct |
12 ms |
6904 KB |
Output is correct |
6 |
Correct |
19 ms |
10324 KB |
Output is correct |
7 |
Correct |
19 ms |
10184 KB |
Output is correct |
8 |
Correct |
19 ms |
10324 KB |
Output is correct |
9 |
Correct |
19 ms |
10324 KB |
Output is correct |
10 |
Correct |
19 ms |
10348 KB |
Output is correct |
11 |
Correct |
20 ms |
10344 KB |
Output is correct |
12 |
Correct |
20 ms |
10368 KB |
Output is correct |
13 |
Correct |
19 ms |
10284 KB |
Output is correct |
14 |
Correct |
19 ms |
10324 KB |
Output is correct |
15 |
Correct |
19 ms |
10340 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
596 KB |
Output is correct |
19 |
Correct |
9 ms |
3796 KB |
Output is correct |
20 |
Correct |
17 ms |
7004 KB |
Output is correct |
21 |
Correct |
28 ms |
10468 KB |
Output is correct |
22 |
Correct |
26 ms |
10488 KB |
Output is correct |
23 |
Correct |
27 ms |
10380 KB |
Output is correct |
24 |
Correct |
26 ms |
10364 KB |
Output is correct |
25 |
Correct |
26 ms |
10472 KB |
Output is correct |
26 |
Correct |
27 ms |
10472 KB |
Output is correct |
27 |
Correct |
26 ms |
10468 KB |
Output is correct |
28 |
Correct |
26 ms |
10416 KB |
Output is correct |
29 |
Correct |
26 ms |
10444 KB |
Output is correct |
30 |
Correct |
26 ms |
10512 KB |
Output is correct |
31 |
Correct |
1 ms |
320 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
3 ms |
560 KB |
Output is correct |
35 |
Correct |
4 ms |
596 KB |
Output is correct |
36 |
Correct |
15 ms |
660 KB |
Output is correct |
37 |
Correct |
37 ms |
2004 KB |
Output is correct |
38 |
Correct |
72 ms |
1996 KB |
Output is correct |
39 |
Correct |
146 ms |
3864 KB |
Output is correct |
40 |
Correct |
290 ms |
7264 KB |
Output is correct |
41 |
Correct |
435 ms |
10880 KB |
Output is correct |
42 |
Correct |
432 ms |
10888 KB |
Output is correct |
43 |
Correct |
420 ms |
10836 KB |
Output is correct |
44 |
Correct |
437 ms |
10884 KB |
Output is correct |
45 |
Correct |
424 ms |
10876 KB |
Output is correct |
46 |
Correct |
425 ms |
10852 KB |
Output is correct |
47 |
Correct |
436 ms |
10896 KB |
Output is correct |
48 |
Correct |
423 ms |
10856 KB |
Output is correct |
49 |
Correct |
468 ms |
10880 KB |
Output is correct |
50 |
Correct |
428 ms |
10756 KB |
Output is correct |
51 |
Correct |
440 ms |
10888 KB |
Output is correct |
52 |
Correct |
430 ms |
10876 KB |
Output is correct |
53 |
Correct |
431 ms |
10880 KB |
Output is correct |
54 |
Correct |
426 ms |
10884 KB |
Output is correct |
55 |
Correct |
427 ms |
10836 KB |
Output is correct |
56 |
Correct |
441 ms |
10884 KB |
Output is correct |
57 |
Correct |
428 ms |
10836 KB |
Output is correct |
58 |
Correct |
438 ms |
10892 KB |
Output is correct |
59 |
Correct |
426 ms |
10888 KB |
Output is correct |
60 |
Correct |
434 ms |
10872 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
300 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
320 KB |
Output is correct |
13 |
Correct |
1 ms |
596 KB |
Output is correct |
14 |
Correct |
6 ms |
3540 KB |
Output is correct |
15 |
Correct |
12 ms |
6904 KB |
Output is correct |
16 |
Correct |
19 ms |
10324 KB |
Output is correct |
17 |
Correct |
19 ms |
10184 KB |
Output is correct |
18 |
Correct |
19 ms |
10324 KB |
Output is correct |
19 |
Correct |
19 ms |
10324 KB |
Output is correct |
20 |
Correct |
19 ms |
10348 KB |
Output is correct |
21 |
Correct |
20 ms |
10344 KB |
Output is correct |
22 |
Correct |
20 ms |
10368 KB |
Output is correct |
23 |
Correct |
19 ms |
10284 KB |
Output is correct |
24 |
Correct |
19 ms |
10324 KB |
Output is correct |
25 |
Correct |
19 ms |
10340 KB |
Output is correct |
26 |
Correct |
0 ms |
212 KB |
Output is correct |
27 |
Correct |
0 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
596 KB |
Output is correct |
29 |
Correct |
9 ms |
3796 KB |
Output is correct |
30 |
Correct |
17 ms |
7004 KB |
Output is correct |
31 |
Correct |
28 ms |
10468 KB |
Output is correct |
32 |
Correct |
26 ms |
10488 KB |
Output is correct |
33 |
Correct |
27 ms |
10380 KB |
Output is correct |
34 |
Correct |
26 ms |
10364 KB |
Output is correct |
35 |
Correct |
26 ms |
10472 KB |
Output is correct |
36 |
Correct |
27 ms |
10472 KB |
Output is correct |
37 |
Correct |
26 ms |
10468 KB |
Output is correct |
38 |
Correct |
26 ms |
10416 KB |
Output is correct |
39 |
Correct |
26 ms |
10444 KB |
Output is correct |
40 |
Correct |
26 ms |
10512 KB |
Output is correct |
41 |
Correct |
1 ms |
320 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
1 ms |
340 KB |
Output is correct |
44 |
Correct |
3 ms |
560 KB |
Output is correct |
45 |
Correct |
4 ms |
596 KB |
Output is correct |
46 |
Correct |
15 ms |
660 KB |
Output is correct |
47 |
Correct |
37 ms |
2004 KB |
Output is correct |
48 |
Correct |
72 ms |
1996 KB |
Output is correct |
49 |
Correct |
146 ms |
3864 KB |
Output is correct |
50 |
Correct |
290 ms |
7264 KB |
Output is correct |
51 |
Correct |
435 ms |
10880 KB |
Output is correct |
52 |
Correct |
432 ms |
10888 KB |
Output is correct |
53 |
Correct |
420 ms |
10836 KB |
Output is correct |
54 |
Correct |
437 ms |
10884 KB |
Output is correct |
55 |
Correct |
424 ms |
10876 KB |
Output is correct |
56 |
Correct |
425 ms |
10852 KB |
Output is correct |
57 |
Correct |
436 ms |
10896 KB |
Output is correct |
58 |
Correct |
423 ms |
10856 KB |
Output is correct |
59 |
Correct |
468 ms |
10880 KB |
Output is correct |
60 |
Correct |
428 ms |
10756 KB |
Output is correct |
61 |
Correct |
440 ms |
10888 KB |
Output is correct |
62 |
Correct |
430 ms |
10876 KB |
Output is correct |
63 |
Correct |
431 ms |
10880 KB |
Output is correct |
64 |
Correct |
426 ms |
10884 KB |
Output is correct |
65 |
Correct |
427 ms |
10836 KB |
Output is correct |
66 |
Correct |
441 ms |
10884 KB |
Output is correct |
67 |
Correct |
428 ms |
10836 KB |
Output is correct |
68 |
Correct |
438 ms |
10892 KB |
Output is correct |
69 |
Correct |
426 ms |
10888 KB |
Output is correct |
70 |
Correct |
434 ms |
10872 KB |
Output is correct |
71 |
Correct |
140 ms |
2132 KB |
Output is correct |
72 |
Correct |
144 ms |
2168 KB |
Output is correct |
73 |
Correct |
144 ms |
2164 KB |
Output is correct |
74 |
Correct |
143 ms |
2168 KB |
Output is correct |
75 |
Correct |
142 ms |
2132 KB |
Output is correct |
76 |
Correct |
691 ms |
4068 KB |
Output is correct |
77 |
Correct |
713 ms |
4072 KB |
Output is correct |
78 |
Correct |
708 ms |
4088 KB |
Output is correct |
79 |
Correct |
689 ms |
4052 KB |
Output is correct |
80 |
Correct |
698 ms |
4068 KB |
Output is correct |
81 |
Correct |
2827 ms |
7824 KB |
Output is correct |
82 |
Correct |
2838 ms |
7828 KB |
Output is correct |
83 |
Correct |
2757 ms |
7824 KB |
Output is correct |
84 |
Correct |
2807 ms |
7820 KB |
Output is correct |
85 |
Correct |
2761 ms |
7828 KB |
Output is correct |
86 |
Correct |
5543 ms |
8256 KB |
Output is correct |
87 |
Correct |
5540 ms |
8264 KB |
Output is correct |
88 |
Correct |
5559 ms |
8256 KB |
Output is correct |
89 |
Correct |
5564 ms |
8256 KB |
Output is correct |
90 |
Correct |
5632 ms |
8260 KB |
Output is correct |
91 |
Execution timed out |
7072 ms |
13896 KB |
Time limit exceeded |
92 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
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 |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
300 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
320 KB |
Output is correct |
13 |
Correct |
1 ms |
596 KB |
Output is correct |
14 |
Correct |
6 ms |
3540 KB |
Output is correct |
15 |
Correct |
12 ms |
6904 KB |
Output is correct |
16 |
Correct |
19 ms |
10324 KB |
Output is correct |
17 |
Correct |
19 ms |
10184 KB |
Output is correct |
18 |
Correct |
19 ms |
10324 KB |
Output is correct |
19 |
Correct |
19 ms |
10324 KB |
Output is correct |
20 |
Correct |
19 ms |
10348 KB |
Output is correct |
21 |
Correct |
20 ms |
10344 KB |
Output is correct |
22 |
Correct |
20 ms |
10368 KB |
Output is correct |
23 |
Correct |
19 ms |
10284 KB |
Output is correct |
24 |
Correct |
19 ms |
10324 KB |
Output is correct |
25 |
Correct |
19 ms |
10340 KB |
Output is correct |
26 |
Correct |
0 ms |
212 KB |
Output is correct |
27 |
Correct |
0 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
596 KB |
Output is correct |
29 |
Correct |
9 ms |
3796 KB |
Output is correct |
30 |
Correct |
17 ms |
7004 KB |
Output is correct |
31 |
Correct |
28 ms |
10468 KB |
Output is correct |
32 |
Correct |
26 ms |
10488 KB |
Output is correct |
33 |
Correct |
27 ms |
10380 KB |
Output is correct |
34 |
Correct |
26 ms |
10364 KB |
Output is correct |
35 |
Correct |
26 ms |
10472 KB |
Output is correct |
36 |
Correct |
27 ms |
10472 KB |
Output is correct |
37 |
Correct |
26 ms |
10468 KB |
Output is correct |
38 |
Correct |
26 ms |
10416 KB |
Output is correct |
39 |
Correct |
26 ms |
10444 KB |
Output is correct |
40 |
Correct |
26 ms |
10512 KB |
Output is correct |
41 |
Correct |
1 ms |
320 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
1 ms |
340 KB |
Output is correct |
44 |
Correct |
3 ms |
560 KB |
Output is correct |
45 |
Correct |
4 ms |
596 KB |
Output is correct |
46 |
Correct |
15 ms |
660 KB |
Output is correct |
47 |
Correct |
37 ms |
2004 KB |
Output is correct |
48 |
Correct |
72 ms |
1996 KB |
Output is correct |
49 |
Correct |
146 ms |
3864 KB |
Output is correct |
50 |
Correct |
290 ms |
7264 KB |
Output is correct |
51 |
Correct |
435 ms |
10880 KB |
Output is correct |
52 |
Correct |
432 ms |
10888 KB |
Output is correct |
53 |
Correct |
420 ms |
10836 KB |
Output is correct |
54 |
Correct |
437 ms |
10884 KB |
Output is correct |
55 |
Correct |
424 ms |
10876 KB |
Output is correct |
56 |
Correct |
425 ms |
10852 KB |
Output is correct |
57 |
Correct |
436 ms |
10896 KB |
Output is correct |
58 |
Correct |
423 ms |
10856 KB |
Output is correct |
59 |
Correct |
468 ms |
10880 KB |
Output is correct |
60 |
Correct |
428 ms |
10756 KB |
Output is correct |
61 |
Correct |
440 ms |
10888 KB |
Output is correct |
62 |
Correct |
430 ms |
10876 KB |
Output is correct |
63 |
Correct |
431 ms |
10880 KB |
Output is correct |
64 |
Correct |
426 ms |
10884 KB |
Output is correct |
65 |
Correct |
427 ms |
10836 KB |
Output is correct |
66 |
Correct |
441 ms |
10884 KB |
Output is correct |
67 |
Correct |
428 ms |
10836 KB |
Output is correct |
68 |
Correct |
438 ms |
10892 KB |
Output is correct |
69 |
Correct |
426 ms |
10888 KB |
Output is correct |
70 |
Correct |
434 ms |
10872 KB |
Output is correct |
71 |
Correct |
140 ms |
2132 KB |
Output is correct |
72 |
Correct |
144 ms |
2168 KB |
Output is correct |
73 |
Correct |
144 ms |
2164 KB |
Output is correct |
74 |
Correct |
143 ms |
2168 KB |
Output is correct |
75 |
Correct |
142 ms |
2132 KB |
Output is correct |
76 |
Correct |
691 ms |
4068 KB |
Output is correct |
77 |
Correct |
713 ms |
4072 KB |
Output is correct |
78 |
Correct |
708 ms |
4088 KB |
Output is correct |
79 |
Correct |
689 ms |
4052 KB |
Output is correct |
80 |
Correct |
698 ms |
4068 KB |
Output is correct |
81 |
Correct |
2827 ms |
7824 KB |
Output is correct |
82 |
Correct |
2838 ms |
7828 KB |
Output is correct |
83 |
Correct |
2757 ms |
7824 KB |
Output is correct |
84 |
Correct |
2807 ms |
7820 KB |
Output is correct |
85 |
Correct |
2761 ms |
7828 KB |
Output is correct |
86 |
Correct |
5543 ms |
8256 KB |
Output is correct |
87 |
Correct |
5540 ms |
8264 KB |
Output is correct |
88 |
Correct |
5559 ms |
8256 KB |
Output is correct |
89 |
Correct |
5564 ms |
8256 KB |
Output is correct |
90 |
Correct |
5632 ms |
8260 KB |
Output is correct |
91 |
Execution timed out |
7072 ms |
13896 KB |
Time limit exceeded |
92 |
Halted |
0 ms |
0 KB |
- |