#include <vector>
#include <iostream>
#include <cassert>
#include <cmath>
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#include <map>
#include <set>
using namespace std;
vector<int64_t> p;
const int MX = 131072;
//const int MX = 300;
struct SegmentTree {
int64_t orig[2 * MX];
int64_t vec[2 * MX];
int64_t lazy[2 * MX];
const int64_t INF = 1e18;
void push_all (int dum) {
if (dum >= 2 * p.size()) {
return;
}
push(dum);
push_all(2 * dum + 1);
push_all(2 * dum + 2);
}
void push (int dum) {
if (lazy[dum] != -1) {
lazy[2 * dum + 1] = lazy[dum];
lazy[2 * dum + 2] = lazy[dum];
vec[2 * dum + 1] = lazy[dum] + orig[2 * dum + 1];
vec[2 * dum + 2] = lazy[dum] + orig[2 * dum + 2];
lazy[dum] = -1;
}
}
void upd (int dum, int tl, int tr, int l, int r, int64_t x) {
if (tl >= l and tr <= r) {
lazy[dum] = x;
vec[dum] = orig[dum] + x;
return;
}
if (tl > r or l > tr) {
return;
}
push(dum);
upd(2 * dum + 1, tl, (tl + tr)/2, l, r, x);
upd(2 * dum + 2, (tl + tr)/2 + 1, tr, l, r, x);
vec[dum] = min(vec[2 * dum + 1], vec[2 * dum + 2]);
}
int64_t query(int l, int r) {
r += 1;
l += p.size(), r += p.size();
push(l);
push(r - 1);
int64_t res = INF;
for (; l < r; l >>= 1, r >>= 1) {
if (l&1) res = min(res, vec[-1 + l++]);
if (r&1) res = min(vec[-1 + --r], res);
}
return res;
}
void upd (int l, int r, int64_t x) {
upd(0, 0, (int)p.size() - 1, l, r, x);
}
void build () {
for (int i = 2 * (int)p.size() - 2; i >= 0; i--) {
if (i >= (int)p.size() - 1) {
orig[i] = p[i - ((int)p.size() - 1)];
} else {
orig[i] = min(orig[2 * i + 1], orig[2 * i + 2]);
}
}
}
void reset () {
for (int i = 0; i < 2 * p.size(); i++) {
orig[i] = 0, lazy[i] = -1;
}
build();
for (int i = 0; i < 2 * p.size(); i++) {
vec[i] = orig[i];
}
}
SegmentTree () {
int n = p.size();
for (int i = 0; i < 2 * n; i++) {
orig[i] = 0;
lazy[i] = -1;
}
build();
for (int i = 0; i < 2 * n; i++) {
vec[i] = orig[i];
}
}
};
struct SparseTable {
int64_t dp[MX][18];
int64_t query (int l, int r) {
int sz = log2(r - l + 1);
return min(dp[l][sz], dp[r - (1 << sz) + 1][sz]);
}
void upd (vector<int64_t> &a) {
for (int j = 0; j < 18; j++) {
for (int i = 0; i < a.size(); i++) {
if (j == 0) {
dp[i][j] = a[i];
} else {
dp[i][j] = min(dp[i][j - 1], dp[min(i + (1 << (j - 1)), (int)a.size() - 1)][j - 1]);
}
}
}
}
};
int main () {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int N, K;
cin >> N >> K;
vector<int64_t> arr(N);
for (int i = 0; i < N; i++) {
cin >> arr[i];
}
int64_t INF = 1e17;
vector<int64_t> cur(N + 1);
p.assign(N + 1, INF), cur.assign(N + 1, INF);
for (int j = 1; j <= N; j++) {
p[j] = ((j == 1) ? arr[0] : max(p[j - 1], arr[j - 1]));
}
for (int i = 0; i < arr.size(); i++) {
arr[i] = -arr[i];
}
SparseTable st_a;
st_a.upd(arr);
for (int i = 0; i < arr.size(); i++) {
arr[i] = -arr[i];
}
int nxt[N];
for (int j = 0; j < N; j++) {
int l = 0;
int r = j;
while (l != r) {
int m = (l + r)/2;
if (st_a.query(m, j) == -arr[j]) {
r = m;
} else {
l = m + 1;
}
}
nxt[j] = l;
}
while (__builtin_popcount(p.size()) != 1) {
p.push_back(0);
cur.push_back(0);
}
SparseTable st_p;
SegmentTree val;
for (int i = 2; i <= K; i++) {
val.reset();
st_p.upd(p);
for (int j = 1; j <= N; j++) {
val.upd(nxt[j - 1], j - 1, arr[j - 1]);
}
int64_t pref[N + 1];
val.push_all(0);
pref[0] = val.query(0, 0);
for (int j = 1; j < N; j++) {
pref[j] = min(val.query(j, j), pref[j - 1]);
}
for (int j = 1; j <= N; j++) {
if (j == 1) {
cur[j] = p[0] + arr[0];
} else {
cur[j] = min(pref[max(nxt[j - 1] - 1, 0)], arr[j - 1] + st_p.query(nxt[j - 1], j - 1));
}
}
swap(cur, p);
if (p == cur) {
break;
}
}
cout << p[N];
}
Compilation message
blocks.cpp:6: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
6 | #pragma GCC optimization ("O3")
|
blocks.cpp:7: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
7 | #pragma GCC optimization ("unroll-loops")
|
blocks.cpp: In member function 'void SegmentTree::push_all(int)':
blocks.cpp:20:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
20 | if (dum >= 2 * p.size()) {
| ~~~~^~~~~~~~~~~~~~~
blocks.cpp: In member function 'void SegmentTree::reset()':
blocks.cpp:75:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
75 | for (int i = 0; i < 2 * p.size(); i++) {
| ~~^~~~~~~~~~~~~~
blocks.cpp:79:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
79 | for (int i = 0; i < 2 * p.size(); i++) {
| ~~^~~~~~~~~~~~~~
blocks.cpp: In member function 'void SparseTable::upd(std::vector<long int>&)':
blocks.cpp:103:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
103 | for (int i = 0; i < a.size(); i++) {
| ~~^~~~~~~~~~
blocks.cpp: In function 'int main()':
blocks.cpp:128:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
128 | for (int i = 0; i < arr.size(); i++) {
| ~~^~~~~~~~~~~~
blocks.cpp:133:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
133 | for (int i = 0; i < arr.size(); i++) {
| ~~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
43308 KB |
Output is correct |
2 |
Correct |
20 ms |
43348 KB |
Output is correct |
3 |
Correct |
20 ms |
43348 KB |
Output is correct |
4 |
Correct |
23 ms |
43304 KB |
Output is correct |
5 |
Correct |
20 ms |
43272 KB |
Output is correct |
6 |
Correct |
18 ms |
43352 KB |
Output is correct |
7 |
Correct |
19 ms |
43388 KB |
Output is correct |
8 |
Correct |
19 ms |
43288 KB |
Output is correct |
9 |
Correct |
18 ms |
43380 KB |
Output is correct |
10 |
Correct |
17 ms |
43324 KB |
Output is correct |
11 |
Correct |
18 ms |
43348 KB |
Output is correct |
12 |
Correct |
18 ms |
43348 KB |
Output is correct |
13 |
Correct |
19 ms |
43384 KB |
Output is correct |
14 |
Correct |
18 ms |
43412 KB |
Output is correct |
15 |
Correct |
18 ms |
43324 KB |
Output is correct |
16 |
Correct |
18 ms |
43324 KB |
Output is correct |
17 |
Correct |
20 ms |
43384 KB |
Output is correct |
18 |
Correct |
18 ms |
43296 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
43348 KB |
Output is correct |
2 |
Correct |
17 ms |
43392 KB |
Output is correct |
3 |
Correct |
18 ms |
43284 KB |
Output is correct |
4 |
Correct |
18 ms |
43348 KB |
Output is correct |
5 |
Correct |
16 ms |
43384 KB |
Output is correct |
6 |
Correct |
21 ms |
43348 KB |
Output is correct |
7 |
Correct |
18 ms |
43348 KB |
Output is correct |
8 |
Correct |
18 ms |
43404 KB |
Output is correct |
9 |
Correct |
18 ms |
43348 KB |
Output is correct |
10 |
Correct |
18 ms |
43296 KB |
Output is correct |
11 |
Correct |
18 ms |
43348 KB |
Output is correct |
12 |
Correct |
19 ms |
43348 KB |
Output is correct |
13 |
Correct |
18 ms |
43340 KB |
Output is correct |
14 |
Correct |
18 ms |
43320 KB |
Output is correct |
15 |
Correct |
18 ms |
43348 KB |
Output is correct |
16 |
Correct |
22 ms |
43340 KB |
Output is correct |
17 |
Correct |
24 ms |
43288 KB |
Output is correct |
18 |
Correct |
18 ms |
43320 KB |
Output is correct |
19 |
Correct |
19 ms |
43404 KB |
Output is correct |
20 |
Correct |
18 ms |
43284 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
43308 KB |
Output is correct |
2 |
Correct |
20 ms |
43348 KB |
Output is correct |
3 |
Correct |
20 ms |
43348 KB |
Output is correct |
4 |
Correct |
23 ms |
43304 KB |
Output is correct |
5 |
Correct |
20 ms |
43272 KB |
Output is correct |
6 |
Correct |
18 ms |
43352 KB |
Output is correct |
7 |
Correct |
19 ms |
43388 KB |
Output is correct |
8 |
Correct |
19 ms |
43288 KB |
Output is correct |
9 |
Correct |
18 ms |
43380 KB |
Output is correct |
10 |
Correct |
17 ms |
43324 KB |
Output is correct |
11 |
Correct |
18 ms |
43348 KB |
Output is correct |
12 |
Correct |
18 ms |
43348 KB |
Output is correct |
13 |
Correct |
19 ms |
43384 KB |
Output is correct |
14 |
Correct |
18 ms |
43412 KB |
Output is correct |
15 |
Correct |
18 ms |
43324 KB |
Output is correct |
16 |
Correct |
18 ms |
43324 KB |
Output is correct |
17 |
Correct |
20 ms |
43384 KB |
Output is correct |
18 |
Correct |
18 ms |
43296 KB |
Output is correct |
19 |
Correct |
17 ms |
43348 KB |
Output is correct |
20 |
Correct |
17 ms |
43392 KB |
Output is correct |
21 |
Correct |
18 ms |
43284 KB |
Output is correct |
22 |
Correct |
18 ms |
43348 KB |
Output is correct |
23 |
Correct |
16 ms |
43384 KB |
Output is correct |
24 |
Correct |
21 ms |
43348 KB |
Output is correct |
25 |
Correct |
18 ms |
43348 KB |
Output is correct |
26 |
Correct |
18 ms |
43404 KB |
Output is correct |
27 |
Correct |
18 ms |
43348 KB |
Output is correct |
28 |
Correct |
18 ms |
43296 KB |
Output is correct |
29 |
Correct |
18 ms |
43348 KB |
Output is correct |
30 |
Correct |
19 ms |
43348 KB |
Output is correct |
31 |
Correct |
18 ms |
43340 KB |
Output is correct |
32 |
Correct |
18 ms |
43320 KB |
Output is correct |
33 |
Correct |
18 ms |
43348 KB |
Output is correct |
34 |
Correct |
22 ms |
43340 KB |
Output is correct |
35 |
Correct |
24 ms |
43288 KB |
Output is correct |
36 |
Correct |
18 ms |
43320 KB |
Output is correct |
37 |
Correct |
19 ms |
43404 KB |
Output is correct |
38 |
Correct |
18 ms |
43284 KB |
Output is correct |
39 |
Incorrect |
20 ms |
43444 KB |
Output isn't correct |
40 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
43308 KB |
Output is correct |
2 |
Correct |
20 ms |
43348 KB |
Output is correct |
3 |
Correct |
20 ms |
43348 KB |
Output is correct |
4 |
Correct |
23 ms |
43304 KB |
Output is correct |
5 |
Correct |
20 ms |
43272 KB |
Output is correct |
6 |
Correct |
18 ms |
43352 KB |
Output is correct |
7 |
Correct |
19 ms |
43388 KB |
Output is correct |
8 |
Correct |
19 ms |
43288 KB |
Output is correct |
9 |
Correct |
18 ms |
43380 KB |
Output is correct |
10 |
Correct |
17 ms |
43324 KB |
Output is correct |
11 |
Correct |
18 ms |
43348 KB |
Output is correct |
12 |
Correct |
18 ms |
43348 KB |
Output is correct |
13 |
Correct |
19 ms |
43384 KB |
Output is correct |
14 |
Correct |
18 ms |
43412 KB |
Output is correct |
15 |
Correct |
18 ms |
43324 KB |
Output is correct |
16 |
Correct |
18 ms |
43324 KB |
Output is correct |
17 |
Correct |
20 ms |
43384 KB |
Output is correct |
18 |
Correct |
18 ms |
43296 KB |
Output is correct |
19 |
Correct |
17 ms |
43348 KB |
Output is correct |
20 |
Correct |
17 ms |
43392 KB |
Output is correct |
21 |
Correct |
18 ms |
43284 KB |
Output is correct |
22 |
Correct |
18 ms |
43348 KB |
Output is correct |
23 |
Correct |
16 ms |
43384 KB |
Output is correct |
24 |
Correct |
21 ms |
43348 KB |
Output is correct |
25 |
Correct |
18 ms |
43348 KB |
Output is correct |
26 |
Correct |
18 ms |
43404 KB |
Output is correct |
27 |
Correct |
18 ms |
43348 KB |
Output is correct |
28 |
Correct |
18 ms |
43296 KB |
Output is correct |
29 |
Correct |
18 ms |
43348 KB |
Output is correct |
30 |
Correct |
19 ms |
43348 KB |
Output is correct |
31 |
Correct |
18 ms |
43340 KB |
Output is correct |
32 |
Correct |
18 ms |
43320 KB |
Output is correct |
33 |
Correct |
18 ms |
43348 KB |
Output is correct |
34 |
Correct |
22 ms |
43340 KB |
Output is correct |
35 |
Correct |
24 ms |
43288 KB |
Output is correct |
36 |
Correct |
18 ms |
43320 KB |
Output is correct |
37 |
Correct |
19 ms |
43404 KB |
Output is correct |
38 |
Correct |
18 ms |
43284 KB |
Output is correct |
39 |
Incorrect |
20 ms |
43444 KB |
Output isn't correct |
40 |
Halted |
0 ms |
0 KB |
- |