#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef vector<int> 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));
ll num = sz(cnts);
ans = INF;
for (int i = 0; i < (1 << num); ++i)
{
// if all were different
ll tmp = n * (n + 1) * (n + 1) / 2 - (2 * n + 1) * (n + 1) * n / 6;
ll right = 0;
ll left = 0;
for (int j = 0; j < num; ++j)
{
ll x = cnts[j];
// either with left or right endpoint in set
tmp -= (n - x) * x * (x - 1) / 2;
// if both are in set
tmp -= x * x * (x - 1) / 2 - (2 * x - 1) * x * (x - 1) / 6;
if (i & (1 << j))
{ // left
tmp -= (x - 1) * left * (n - left - x);
left += x;
}
else
{ // right
tmp -= (x - 1) * right * (n - right - x);
right += x;
}
}
ans = min(ans, tmp);
}
cout << ans << "\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
0 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 |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
5 ms |
1236 KB |
Output is correct |
5 |
Correct |
9 ms |
2256 KB |
Output is correct |
6 |
Correct |
14 ms |
3292 KB |
Output is correct |
7 |
Correct |
13 ms |
3268 KB |
Output is correct |
8 |
Correct |
14 ms |
3280 KB |
Output is correct |
9 |
Correct |
14 ms |
3276 KB |
Output is correct |
10 |
Correct |
14 ms |
3284 KB |
Output is correct |
11 |
Correct |
13 ms |
3204 KB |
Output is correct |
12 |
Correct |
14 ms |
3284 KB |
Output is correct |
13 |
Correct |
14 ms |
3284 KB |
Output is correct |
14 |
Correct |
13 ms |
3288 KB |
Output is correct |
15 |
Correct |
13 ms |
3284 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
5 ms |
1236 KB |
Output is correct |
5 |
Correct |
9 ms |
2256 KB |
Output is correct |
6 |
Correct |
14 ms |
3292 KB |
Output is correct |
7 |
Correct |
13 ms |
3268 KB |
Output is correct |
8 |
Correct |
14 ms |
3280 KB |
Output is correct |
9 |
Correct |
14 ms |
3276 KB |
Output is correct |
10 |
Correct |
14 ms |
3284 KB |
Output is correct |
11 |
Correct |
13 ms |
3204 KB |
Output is correct |
12 |
Correct |
14 ms |
3284 KB |
Output is correct |
13 |
Correct |
14 ms |
3284 KB |
Output is correct |
14 |
Correct |
13 ms |
3288 KB |
Output is correct |
15 |
Correct |
13 ms |
3284 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
3 ms |
324 KB |
Output is correct |
18 |
Correct |
26 ms |
428 KB |
Output is correct |
19 |
Correct |
112 ms |
1344 KB |
Output is correct |
20 |
Correct |
233 ms |
2508 KB |
Output is correct |
21 |
Correct |
969 ms |
3480 KB |
Output is correct |
22 |
Correct |
991 ms |
3448 KB |
Output is correct |
23 |
Correct |
986 ms |
3532 KB |
Output is correct |
24 |
Correct |
979 ms |
3412 KB |
Output is correct |
25 |
Correct |
991 ms |
3532 KB |
Output is correct |
26 |
Correct |
985 ms |
3532 KB |
Output is correct |
27 |
Correct |
995 ms |
3436 KB |
Output is correct |
28 |
Correct |
978 ms |
3384 KB |
Output is correct |
29 |
Correct |
989 ms |
3412 KB |
Output is correct |
30 |
Correct |
995 ms |
3460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
5 ms |
1236 KB |
Output is correct |
5 |
Correct |
9 ms |
2256 KB |
Output is correct |
6 |
Correct |
14 ms |
3292 KB |
Output is correct |
7 |
Correct |
13 ms |
3268 KB |
Output is correct |
8 |
Correct |
14 ms |
3280 KB |
Output is correct |
9 |
Correct |
14 ms |
3276 KB |
Output is correct |
10 |
Correct |
14 ms |
3284 KB |
Output is correct |
11 |
Correct |
13 ms |
3204 KB |
Output is correct |
12 |
Correct |
14 ms |
3284 KB |
Output is correct |
13 |
Correct |
14 ms |
3284 KB |
Output is correct |
14 |
Correct |
13 ms |
3288 KB |
Output is correct |
15 |
Correct |
13 ms |
3284 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
3 ms |
324 KB |
Output is correct |
18 |
Correct |
26 ms |
428 KB |
Output is correct |
19 |
Correct |
112 ms |
1344 KB |
Output is correct |
20 |
Correct |
233 ms |
2508 KB |
Output is correct |
21 |
Correct |
969 ms |
3480 KB |
Output is correct |
22 |
Correct |
991 ms |
3448 KB |
Output is correct |
23 |
Correct |
986 ms |
3532 KB |
Output is correct |
24 |
Correct |
979 ms |
3412 KB |
Output is correct |
25 |
Correct |
991 ms |
3532 KB |
Output is correct |
26 |
Correct |
985 ms |
3532 KB |
Output is correct |
27 |
Correct |
995 ms |
3436 KB |
Output is correct |
28 |
Correct |
978 ms |
3384 KB |
Output is correct |
29 |
Correct |
989 ms |
3412 KB |
Output is correct |
30 |
Correct |
995 ms |
3460 KB |
Output is correct |
31 |
Incorrect |
64 ms |
308 KB |
Output isn't correct |
32 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
0 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 |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
5 ms |
1236 KB |
Output is correct |
15 |
Correct |
9 ms |
2256 KB |
Output is correct |
16 |
Correct |
14 ms |
3292 KB |
Output is correct |
17 |
Correct |
13 ms |
3268 KB |
Output is correct |
18 |
Correct |
14 ms |
3280 KB |
Output is correct |
19 |
Correct |
14 ms |
3276 KB |
Output is correct |
20 |
Correct |
14 ms |
3284 KB |
Output is correct |
21 |
Correct |
13 ms |
3204 KB |
Output is correct |
22 |
Correct |
14 ms |
3284 KB |
Output is correct |
23 |
Correct |
14 ms |
3284 KB |
Output is correct |
24 |
Correct |
13 ms |
3288 KB |
Output is correct |
25 |
Correct |
13 ms |
3284 KB |
Output is correct |
26 |
Correct |
1 ms |
212 KB |
Output is correct |
27 |
Correct |
3 ms |
324 KB |
Output is correct |
28 |
Correct |
26 ms |
428 KB |
Output is correct |
29 |
Correct |
112 ms |
1344 KB |
Output is correct |
30 |
Correct |
233 ms |
2508 KB |
Output is correct |
31 |
Correct |
969 ms |
3480 KB |
Output is correct |
32 |
Correct |
991 ms |
3448 KB |
Output is correct |
33 |
Correct |
986 ms |
3532 KB |
Output is correct |
34 |
Correct |
979 ms |
3412 KB |
Output is correct |
35 |
Correct |
991 ms |
3532 KB |
Output is correct |
36 |
Correct |
985 ms |
3532 KB |
Output is correct |
37 |
Correct |
995 ms |
3436 KB |
Output is correct |
38 |
Correct |
978 ms |
3384 KB |
Output is correct |
39 |
Correct |
989 ms |
3412 KB |
Output is correct |
40 |
Correct |
995 ms |
3460 KB |
Output is correct |
41 |
Incorrect |
64 ms |
308 KB |
Output isn't correct |
42 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
0 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 |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
5 ms |
1236 KB |
Output is correct |
15 |
Correct |
9 ms |
2256 KB |
Output is correct |
16 |
Correct |
14 ms |
3292 KB |
Output is correct |
17 |
Correct |
13 ms |
3268 KB |
Output is correct |
18 |
Correct |
14 ms |
3280 KB |
Output is correct |
19 |
Correct |
14 ms |
3276 KB |
Output is correct |
20 |
Correct |
14 ms |
3284 KB |
Output is correct |
21 |
Correct |
13 ms |
3204 KB |
Output is correct |
22 |
Correct |
14 ms |
3284 KB |
Output is correct |
23 |
Correct |
14 ms |
3284 KB |
Output is correct |
24 |
Correct |
13 ms |
3288 KB |
Output is correct |
25 |
Correct |
13 ms |
3284 KB |
Output is correct |
26 |
Correct |
1 ms |
212 KB |
Output is correct |
27 |
Correct |
3 ms |
324 KB |
Output is correct |
28 |
Correct |
26 ms |
428 KB |
Output is correct |
29 |
Correct |
112 ms |
1344 KB |
Output is correct |
30 |
Correct |
233 ms |
2508 KB |
Output is correct |
31 |
Correct |
969 ms |
3480 KB |
Output is correct |
32 |
Correct |
991 ms |
3448 KB |
Output is correct |
33 |
Correct |
986 ms |
3532 KB |
Output is correct |
34 |
Correct |
979 ms |
3412 KB |
Output is correct |
35 |
Correct |
991 ms |
3532 KB |
Output is correct |
36 |
Correct |
985 ms |
3532 KB |
Output is correct |
37 |
Correct |
995 ms |
3436 KB |
Output is correct |
38 |
Correct |
978 ms |
3384 KB |
Output is correct |
39 |
Correct |
989 ms |
3412 KB |
Output is correct |
40 |
Correct |
995 ms |
3460 KB |
Output is correct |
41 |
Incorrect |
64 ms |
308 KB |
Output isn't correct |
42 |
Halted |
0 ms |
0 KB |
- |