#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef vector<ll> vi;
typedef pair<ll, ll> pii;
typedef vector<pii> vpii;
#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 split(int x)
{
vi sp;
int j = 0;
while (2 * (1 << j) <= x)
{
sp.push_back(1 << j);
x -= 1 << j;
sp.push_back(1 << j);
x -= 1 << j;
++j;
}
j = 0;
while (x > 0)
{
if (x & (1 << j))
sp.push_back(1 << j), x ^= 1 << j;
++j;
}
return sp;
}
ll numberOfSubsequences(ll n)
{
return n * (n + 1) / 2;
}
ll totalLengthOfSubsequences(ll n)
{
return n * (n + 1) * (n + 1) / 2 - (2 * n + 1) * (n + 1) * n / 6;
}
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]];
vpii S;
for (auto it = cnts.begin(); it != cnts.end(); ++it)
for (int x : split(it->second))
S.push_back({it->first, x});
ll pref = 0;
int num = sz(S);
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] = totalLengthOfSubsequences(n);
for (int j = 0; j < num; ++j)
{
int ii = j & 1;
int ni = ii ^ 1;
fill(all(dp[ni]), INF);
ll x = S[j].first; // how much
ll y = S[j].second; // how often
ll len = x * y;
ll subsure = 0;
// if both are in set
subsure += y * (totalLengthOfSubsequences(x) - numberOfSubsequences(x));
for (int i = 2; i <= y; ++i)
subsure += x * x * (y - i + 1) * i * (y - 1);
// either with left or right endpoint in set
ll factor = len * (len + 1) / 2 - x * y * (y + 1) / 2;
subsure += factor * (n - len);
for (ll left = 0; left < sz(dp[ii]); ++left)
{
ll tmp = dp[ii][left];
if (tmp == INF)
continue;
tmp -= subsure;
// left
dp[ni][left + len] = min(dp[ni][left + len], tmp - (len - y) * left * (n - left - len));
// right
ll right = pref - left;
dp[ni][left] = min(dp[ni][left], tmp - (len - y) * right * (n - right - len));
}
pref += len;
}
ans = *min_element(all(dp[num & 1]));
cout << ans << "\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 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 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
584 KB |
Output is correct |
4 |
Correct |
6 ms |
3660 KB |
Output is correct |
5 |
Correct |
12 ms |
6868 KB |
Output is correct |
6 |
Correct |
20 ms |
10348 KB |
Output is correct |
7 |
Correct |
19 ms |
10324 KB |
Output is correct |
8 |
Correct |
18 ms |
10348 KB |
Output is correct |
9 |
Correct |
19 ms |
10312 KB |
Output is correct |
10 |
Correct |
19 ms |
10312 KB |
Output is correct |
11 |
Correct |
19 ms |
10324 KB |
Output is correct |
12 |
Correct |
19 ms |
10352 KB |
Output is correct |
13 |
Correct |
19 ms |
10324 KB |
Output is correct |
14 |
Correct |
19 ms |
10268 KB |
Output is correct |
15 |
Correct |
20 ms |
10332 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 |
584 KB |
Output is correct |
4 |
Correct |
6 ms |
3660 KB |
Output is correct |
5 |
Correct |
12 ms |
6868 KB |
Output is correct |
6 |
Correct |
20 ms |
10348 KB |
Output is correct |
7 |
Correct |
19 ms |
10324 KB |
Output is correct |
8 |
Correct |
18 ms |
10348 KB |
Output is correct |
9 |
Correct |
19 ms |
10312 KB |
Output is correct |
10 |
Correct |
19 ms |
10312 KB |
Output is correct |
11 |
Correct |
19 ms |
10324 KB |
Output is correct |
12 |
Correct |
19 ms |
10352 KB |
Output is correct |
13 |
Correct |
19 ms |
10324 KB |
Output is correct |
14 |
Correct |
19 ms |
10268 KB |
Output is correct |
15 |
Correct |
20 ms |
10332 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 |
11 ms |
3724 KB |
Output is correct |
20 |
Correct |
17 ms |
7124 KB |
Output is correct |
21 |
Correct |
26 ms |
10452 KB |
Output is correct |
22 |
Correct |
26 ms |
10472 KB |
Output is correct |
23 |
Correct |
26 ms |
10472 KB |
Output is correct |
24 |
Correct |
26 ms |
10452 KB |
Output is correct |
25 |
Correct |
27 ms |
10416 KB |
Output is correct |
26 |
Correct |
32 ms |
10476 KB |
Output is correct |
27 |
Correct |
26 ms |
10452 KB |
Output is correct |
28 |
Correct |
26 ms |
10448 KB |
Output is correct |
29 |
Correct |
26 ms |
10444 KB |
Output is correct |
30 |
Correct |
26 ms |
10476 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 |
584 KB |
Output is correct |
4 |
Correct |
6 ms |
3660 KB |
Output is correct |
5 |
Correct |
12 ms |
6868 KB |
Output is correct |
6 |
Correct |
20 ms |
10348 KB |
Output is correct |
7 |
Correct |
19 ms |
10324 KB |
Output is correct |
8 |
Correct |
18 ms |
10348 KB |
Output is correct |
9 |
Correct |
19 ms |
10312 KB |
Output is correct |
10 |
Correct |
19 ms |
10312 KB |
Output is correct |
11 |
Correct |
19 ms |
10324 KB |
Output is correct |
12 |
Correct |
19 ms |
10352 KB |
Output is correct |
13 |
Correct |
19 ms |
10324 KB |
Output is correct |
14 |
Correct |
19 ms |
10268 KB |
Output is correct |
15 |
Correct |
20 ms |
10332 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 |
11 ms |
3724 KB |
Output is correct |
20 |
Correct |
17 ms |
7124 KB |
Output is correct |
21 |
Correct |
26 ms |
10452 KB |
Output is correct |
22 |
Correct |
26 ms |
10472 KB |
Output is correct |
23 |
Correct |
26 ms |
10472 KB |
Output is correct |
24 |
Correct |
26 ms |
10452 KB |
Output is correct |
25 |
Correct |
27 ms |
10416 KB |
Output is correct |
26 |
Correct |
32 ms |
10476 KB |
Output is correct |
27 |
Correct |
26 ms |
10452 KB |
Output is correct |
28 |
Correct |
26 ms |
10448 KB |
Output is correct |
29 |
Correct |
26 ms |
10444 KB |
Output is correct |
30 |
Correct |
26 ms |
10476 KB |
Output is correct |
31 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
32 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 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 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 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 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |