#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;
A = vi(AA.begin() + l, AA.begin() + r);
}
ll n = sz(A);
vi heatMap(*max_element(all(A)) + 1, 0);
for (int i = 0; i < sz(A); ++i)
++heatMap[A[i]];
map<int, int> cnts;
for (int i = 0; i < sz(heatMap); ++i)
if (heatMap[i])
++cnts[heatMap[i]];
vi L, R;
{
int maxi = 0;
for (auto it = cnts.begin(); it != cnts.end(); ++it)
maxi = max(maxi, it->second);
L.resize(maxi + 1), R.resize(maxi + 1);
}
ll pref = 0;
int 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;
auto it = cnts.begin();
for (int j = 0; j < num; ++j)
{
int ii = j & 1;
int ni = ii ^ 1;
fill(all(dp[ni]), INF);
ll x = it->first; // how much
ll y = it->second; // how often
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 every such set
subsure *= y;
for (ll left = 0; left < sz(dp[ii]); ++left)
{
ll tmp = dp[ii][left];
if (tmp == INF)
continue;
tmp -= subsure;
ll l = left, r = pref - left;
L[0] = R[0] = 0;
for (ll i = 1; i <= y; ++i)
{
L[i] = L[i - 1] + (x - 1) * l * (n - l - x);
R[i] = R[i - 1] + (x - 1) * r * (n - r - x);
l += x, r += x;
}
for (ll i = 0; i <= y; ++i)
dp[ni][left + x * i] = min(dp[ni][left + x * i], tmp - L[i] - R[y - i]);
}
++it;
pref += x * y;
}
ans = *min_element(all(dp[num & 1]));
cout << ans << "\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
320 KB |
Output is correct |
4 |
Correct |
1 ms |
320 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
316 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
596 KB |
Output is correct |
4 |
Correct |
7 ms |
3524 KB |
Output is correct |
5 |
Correct |
16 ms |
6848 KB |
Output is correct |
6 |
Correct |
34 ms |
9920 KB |
Output is correct |
7 |
Correct |
22 ms |
9932 KB |
Output is correct |
8 |
Correct |
24 ms |
9944 KB |
Output is correct |
9 |
Correct |
33 ms |
9864 KB |
Output is correct |
10 |
Correct |
34 ms |
9940 KB |
Output is correct |
11 |
Correct |
21 ms |
9940 KB |
Output is correct |
12 |
Correct |
27 ms |
9928 KB |
Output is correct |
13 |
Correct |
24 ms |
9936 KB |
Output is correct |
14 |
Correct |
26 ms |
9952 KB |
Output is correct |
15 |
Correct |
24 ms |
9956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
596 KB |
Output is correct |
4 |
Correct |
7 ms |
3524 KB |
Output is correct |
5 |
Correct |
16 ms |
6848 KB |
Output is correct |
6 |
Correct |
34 ms |
9920 KB |
Output is correct |
7 |
Correct |
22 ms |
9932 KB |
Output is correct |
8 |
Correct |
24 ms |
9944 KB |
Output is correct |
9 |
Correct |
33 ms |
9864 KB |
Output is correct |
10 |
Correct |
34 ms |
9940 KB |
Output is correct |
11 |
Correct |
21 ms |
9940 KB |
Output is correct |
12 |
Correct |
27 ms |
9928 KB |
Output is correct |
13 |
Correct |
24 ms |
9936 KB |
Output is correct |
14 |
Correct |
26 ms |
9952 KB |
Output is correct |
15 |
Correct |
24 ms |
9956 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
596 KB |
Output is correct |
19 |
Correct |
13 ms |
3564 KB |
Output is correct |
20 |
Correct |
24 ms |
6840 KB |
Output is correct |
21 |
Correct |
39 ms |
10060 KB |
Output is correct |
22 |
Correct |
36 ms |
10092 KB |
Output is correct |
23 |
Correct |
37 ms |
10024 KB |
Output is correct |
24 |
Correct |
37 ms |
10060 KB |
Output is correct |
25 |
Correct |
39 ms |
10004 KB |
Output is correct |
26 |
Correct |
37 ms |
10080 KB |
Output is correct |
27 |
Correct |
36 ms |
10060 KB |
Output is correct |
28 |
Correct |
36 ms |
10056 KB |
Output is correct |
29 |
Correct |
38 ms |
9976 KB |
Output is correct |
30 |
Correct |
32 ms |
10012 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
596 KB |
Output is correct |
4 |
Correct |
7 ms |
3524 KB |
Output is correct |
5 |
Correct |
16 ms |
6848 KB |
Output is correct |
6 |
Correct |
34 ms |
9920 KB |
Output is correct |
7 |
Correct |
22 ms |
9932 KB |
Output is correct |
8 |
Correct |
24 ms |
9944 KB |
Output is correct |
9 |
Correct |
33 ms |
9864 KB |
Output is correct |
10 |
Correct |
34 ms |
9940 KB |
Output is correct |
11 |
Correct |
21 ms |
9940 KB |
Output is correct |
12 |
Correct |
27 ms |
9928 KB |
Output is correct |
13 |
Correct |
24 ms |
9936 KB |
Output is correct |
14 |
Correct |
26 ms |
9952 KB |
Output is correct |
15 |
Correct |
24 ms |
9956 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
596 KB |
Output is correct |
19 |
Correct |
13 ms |
3564 KB |
Output is correct |
20 |
Correct |
24 ms |
6840 KB |
Output is correct |
21 |
Correct |
39 ms |
10060 KB |
Output is correct |
22 |
Correct |
36 ms |
10092 KB |
Output is correct |
23 |
Correct |
37 ms |
10024 KB |
Output is correct |
24 |
Correct |
37 ms |
10060 KB |
Output is correct |
25 |
Correct |
39 ms |
10004 KB |
Output is correct |
26 |
Correct |
37 ms |
10080 KB |
Output is correct |
27 |
Correct |
36 ms |
10060 KB |
Output is correct |
28 |
Correct |
36 ms |
10056 KB |
Output is correct |
29 |
Correct |
38 ms |
9976 KB |
Output is correct |
30 |
Correct |
32 ms |
10012 KB |
Output is correct |
31 |
Correct |
1 ms |
328 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
5 ms |
552 KB |
Output is correct |
35 |
Correct |
5 ms |
592 KB |
Output is correct |
36 |
Correct |
10 ms |
596 KB |
Output is correct |
37 |
Correct |
43 ms |
1920 KB |
Output is correct |
38 |
Correct |
61 ms |
2024 KB |
Output is correct |
39 |
Correct |
145 ms |
3608 KB |
Output is correct |
40 |
Correct |
368 ms |
6740 KB |
Output is correct |
41 |
Correct |
649 ms |
9872 KB |
Output is correct |
42 |
Correct |
645 ms |
9876 KB |
Output is correct |
43 |
Correct |
615 ms |
9876 KB |
Output is correct |
44 |
Correct |
607 ms |
9868 KB |
Output is correct |
45 |
Correct |
636 ms |
9868 KB |
Output is correct |
46 |
Correct |
629 ms |
9864 KB |
Output is correct |
47 |
Correct |
624 ms |
9864 KB |
Output is correct |
48 |
Correct |
605 ms |
9860 KB |
Output is correct |
49 |
Correct |
636 ms |
9872 KB |
Output is correct |
50 |
Correct |
623 ms |
9868 KB |
Output is correct |
51 |
Correct |
621 ms |
9864 KB |
Output is correct |
52 |
Correct |
607 ms |
9844 KB |
Output is correct |
53 |
Correct |
602 ms |
9860 KB |
Output is correct |
54 |
Correct |
606 ms |
9864 KB |
Output is correct |
55 |
Correct |
623 ms |
9884 KB |
Output is correct |
56 |
Correct |
656 ms |
9872 KB |
Output is correct |
57 |
Correct |
620 ms |
9860 KB |
Output is correct |
58 |
Correct |
633 ms |
9864 KB |
Output is correct |
59 |
Correct |
616 ms |
9864 KB |
Output is correct |
60 |
Correct |
627 ms |
9864 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
320 KB |
Output is correct |
4 |
Correct |
1 ms |
320 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
316 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
596 KB |
Output is correct |
14 |
Correct |
7 ms |
3524 KB |
Output is correct |
15 |
Correct |
16 ms |
6848 KB |
Output is correct |
16 |
Correct |
34 ms |
9920 KB |
Output is correct |
17 |
Correct |
22 ms |
9932 KB |
Output is correct |
18 |
Correct |
24 ms |
9944 KB |
Output is correct |
19 |
Correct |
33 ms |
9864 KB |
Output is correct |
20 |
Correct |
34 ms |
9940 KB |
Output is correct |
21 |
Correct |
21 ms |
9940 KB |
Output is correct |
22 |
Correct |
27 ms |
9928 KB |
Output is correct |
23 |
Correct |
24 ms |
9936 KB |
Output is correct |
24 |
Correct |
26 ms |
9952 KB |
Output is correct |
25 |
Correct |
24 ms |
9956 KB |
Output is correct |
26 |
Correct |
0 ms |
212 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
596 KB |
Output is correct |
29 |
Correct |
13 ms |
3564 KB |
Output is correct |
30 |
Correct |
24 ms |
6840 KB |
Output is correct |
31 |
Correct |
39 ms |
10060 KB |
Output is correct |
32 |
Correct |
36 ms |
10092 KB |
Output is correct |
33 |
Correct |
37 ms |
10024 KB |
Output is correct |
34 |
Correct |
37 ms |
10060 KB |
Output is correct |
35 |
Correct |
39 ms |
10004 KB |
Output is correct |
36 |
Correct |
37 ms |
10080 KB |
Output is correct |
37 |
Correct |
36 ms |
10060 KB |
Output is correct |
38 |
Correct |
36 ms |
10056 KB |
Output is correct |
39 |
Correct |
38 ms |
9976 KB |
Output is correct |
40 |
Correct |
32 ms |
10012 KB |
Output is correct |
41 |
Correct |
1 ms |
328 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
1 ms |
340 KB |
Output is correct |
44 |
Correct |
5 ms |
552 KB |
Output is correct |
45 |
Correct |
5 ms |
592 KB |
Output is correct |
46 |
Correct |
10 ms |
596 KB |
Output is correct |
47 |
Correct |
43 ms |
1920 KB |
Output is correct |
48 |
Correct |
61 ms |
2024 KB |
Output is correct |
49 |
Correct |
145 ms |
3608 KB |
Output is correct |
50 |
Correct |
368 ms |
6740 KB |
Output is correct |
51 |
Correct |
649 ms |
9872 KB |
Output is correct |
52 |
Correct |
645 ms |
9876 KB |
Output is correct |
53 |
Correct |
615 ms |
9876 KB |
Output is correct |
54 |
Correct |
607 ms |
9868 KB |
Output is correct |
55 |
Correct |
636 ms |
9868 KB |
Output is correct |
56 |
Correct |
629 ms |
9864 KB |
Output is correct |
57 |
Correct |
624 ms |
9864 KB |
Output is correct |
58 |
Correct |
605 ms |
9860 KB |
Output is correct |
59 |
Correct |
636 ms |
9872 KB |
Output is correct |
60 |
Correct |
623 ms |
9868 KB |
Output is correct |
61 |
Correct |
621 ms |
9864 KB |
Output is correct |
62 |
Correct |
607 ms |
9844 KB |
Output is correct |
63 |
Correct |
602 ms |
9860 KB |
Output is correct |
64 |
Correct |
606 ms |
9864 KB |
Output is correct |
65 |
Correct |
623 ms |
9884 KB |
Output is correct |
66 |
Correct |
656 ms |
9872 KB |
Output is correct |
67 |
Correct |
620 ms |
9860 KB |
Output is correct |
68 |
Correct |
633 ms |
9864 KB |
Output is correct |
69 |
Correct |
616 ms |
9864 KB |
Output is correct |
70 |
Correct |
627 ms |
9864 KB |
Output is correct |
71 |
Correct |
95 ms |
2032 KB |
Output is correct |
72 |
Correct |
101 ms |
2044 KB |
Output is correct |
73 |
Correct |
94 ms |
1992 KB |
Output is correct |
74 |
Correct |
94 ms |
2004 KB |
Output is correct |
75 |
Correct |
92 ms |
2032 KB |
Output is correct |
76 |
Correct |
450 ms |
3636 KB |
Output is correct |
77 |
Correct |
453 ms |
3632 KB |
Output is correct |
78 |
Correct |
455 ms |
3540 KB |
Output is correct |
79 |
Correct |
454 ms |
3540 KB |
Output is correct |
80 |
Correct |
458 ms |
3628 KB |
Output is correct |
81 |
Correct |
1775 ms |
6800 KB |
Output is correct |
82 |
Correct |
2739 ms |
6796 KB |
Output is correct |
83 |
Correct |
1749 ms |
6800 KB |
Output is correct |
84 |
Correct |
1788 ms |
6800 KB |
Output is correct |
85 |
Correct |
1749 ms |
6800 KB |
Output is correct |
86 |
Correct |
3608 ms |
6904 KB |
Output is correct |
87 |
Correct |
3638 ms |
6900 KB |
Output is correct |
88 |
Correct |
3671 ms |
6908 KB |
Output is correct |
89 |
Correct |
3640 ms |
7164 KB |
Output is correct |
90 |
Correct |
3609 ms |
7160 KB |
Output is correct |
91 |
Execution timed out |
7049 ms |
11724 KB |
Time limit exceeded |
92 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
320 KB |
Output is correct |
4 |
Correct |
1 ms |
320 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
316 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
596 KB |
Output is correct |
14 |
Correct |
7 ms |
3524 KB |
Output is correct |
15 |
Correct |
16 ms |
6848 KB |
Output is correct |
16 |
Correct |
34 ms |
9920 KB |
Output is correct |
17 |
Correct |
22 ms |
9932 KB |
Output is correct |
18 |
Correct |
24 ms |
9944 KB |
Output is correct |
19 |
Correct |
33 ms |
9864 KB |
Output is correct |
20 |
Correct |
34 ms |
9940 KB |
Output is correct |
21 |
Correct |
21 ms |
9940 KB |
Output is correct |
22 |
Correct |
27 ms |
9928 KB |
Output is correct |
23 |
Correct |
24 ms |
9936 KB |
Output is correct |
24 |
Correct |
26 ms |
9952 KB |
Output is correct |
25 |
Correct |
24 ms |
9956 KB |
Output is correct |
26 |
Correct |
0 ms |
212 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
596 KB |
Output is correct |
29 |
Correct |
13 ms |
3564 KB |
Output is correct |
30 |
Correct |
24 ms |
6840 KB |
Output is correct |
31 |
Correct |
39 ms |
10060 KB |
Output is correct |
32 |
Correct |
36 ms |
10092 KB |
Output is correct |
33 |
Correct |
37 ms |
10024 KB |
Output is correct |
34 |
Correct |
37 ms |
10060 KB |
Output is correct |
35 |
Correct |
39 ms |
10004 KB |
Output is correct |
36 |
Correct |
37 ms |
10080 KB |
Output is correct |
37 |
Correct |
36 ms |
10060 KB |
Output is correct |
38 |
Correct |
36 ms |
10056 KB |
Output is correct |
39 |
Correct |
38 ms |
9976 KB |
Output is correct |
40 |
Correct |
32 ms |
10012 KB |
Output is correct |
41 |
Correct |
1 ms |
328 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
1 ms |
340 KB |
Output is correct |
44 |
Correct |
5 ms |
552 KB |
Output is correct |
45 |
Correct |
5 ms |
592 KB |
Output is correct |
46 |
Correct |
10 ms |
596 KB |
Output is correct |
47 |
Correct |
43 ms |
1920 KB |
Output is correct |
48 |
Correct |
61 ms |
2024 KB |
Output is correct |
49 |
Correct |
145 ms |
3608 KB |
Output is correct |
50 |
Correct |
368 ms |
6740 KB |
Output is correct |
51 |
Correct |
649 ms |
9872 KB |
Output is correct |
52 |
Correct |
645 ms |
9876 KB |
Output is correct |
53 |
Correct |
615 ms |
9876 KB |
Output is correct |
54 |
Correct |
607 ms |
9868 KB |
Output is correct |
55 |
Correct |
636 ms |
9868 KB |
Output is correct |
56 |
Correct |
629 ms |
9864 KB |
Output is correct |
57 |
Correct |
624 ms |
9864 KB |
Output is correct |
58 |
Correct |
605 ms |
9860 KB |
Output is correct |
59 |
Correct |
636 ms |
9872 KB |
Output is correct |
60 |
Correct |
623 ms |
9868 KB |
Output is correct |
61 |
Correct |
621 ms |
9864 KB |
Output is correct |
62 |
Correct |
607 ms |
9844 KB |
Output is correct |
63 |
Correct |
602 ms |
9860 KB |
Output is correct |
64 |
Correct |
606 ms |
9864 KB |
Output is correct |
65 |
Correct |
623 ms |
9884 KB |
Output is correct |
66 |
Correct |
656 ms |
9872 KB |
Output is correct |
67 |
Correct |
620 ms |
9860 KB |
Output is correct |
68 |
Correct |
633 ms |
9864 KB |
Output is correct |
69 |
Correct |
616 ms |
9864 KB |
Output is correct |
70 |
Correct |
627 ms |
9864 KB |
Output is correct |
71 |
Correct |
95 ms |
2032 KB |
Output is correct |
72 |
Correct |
101 ms |
2044 KB |
Output is correct |
73 |
Correct |
94 ms |
1992 KB |
Output is correct |
74 |
Correct |
94 ms |
2004 KB |
Output is correct |
75 |
Correct |
92 ms |
2032 KB |
Output is correct |
76 |
Correct |
450 ms |
3636 KB |
Output is correct |
77 |
Correct |
453 ms |
3632 KB |
Output is correct |
78 |
Correct |
455 ms |
3540 KB |
Output is correct |
79 |
Correct |
454 ms |
3540 KB |
Output is correct |
80 |
Correct |
458 ms |
3628 KB |
Output is correct |
81 |
Correct |
1775 ms |
6800 KB |
Output is correct |
82 |
Correct |
2739 ms |
6796 KB |
Output is correct |
83 |
Correct |
1749 ms |
6800 KB |
Output is correct |
84 |
Correct |
1788 ms |
6800 KB |
Output is correct |
85 |
Correct |
1749 ms |
6800 KB |
Output is correct |
86 |
Correct |
3608 ms |
6904 KB |
Output is correct |
87 |
Correct |
3638 ms |
6900 KB |
Output is correct |
88 |
Correct |
3671 ms |
6908 KB |
Output is correct |
89 |
Correct |
3640 ms |
7164 KB |
Output is correct |
90 |
Correct |
3609 ms |
7160 KB |
Output is correct |
91 |
Execution timed out |
7049 ms |
11724 KB |
Time limit exceeded |
92 |
Halted |
0 ms |
0 KB |
- |