#include <bits/stdc++.h>
#define fo(i, d, c) for (int i = d; i <= c; i++)
#define fod(i, c, d) for (int i = c; i >= d; i--)
#define maxn 1000010
#define N 1010
#define fi first
#define se second
#define pb emplace_back
#define en cout << "\n";
#define inf (int)1e9
#define bitcount(x) __builtin_popcountll(x)
#define pii pair<int, int>
#define vii vector<pii>
#define lb(x) x & -x
#define bit(i, j) ((i >> j) & 1)
#define offbit(i, j) (i ^ (1LL << j))
#define onbit(i, j) (i | (1LL << j))
#define vi vector<int>
#define all(x) x.begin(), x.end()
#define ss(x) (int)x.size()
#define UNIQUE(v) v.erase(unique(all(v)), v.end())
template <typename T1, typename T2>
bool minimize(T1 &a, T2 b)
{
if (a > b)
{
a = b;
return true;
}
return false;
}
template <typename T1, typename T2>
bool maximize(T1 &a, T2 b)
{
if (a < b)
{
a = b;
return true;
}
return false;
}
using namespace std;
const int nsqrt = 450;
const int mod = 1e9 + 7;
void add(int &x, int k)
{
x += k;
x %= mod;
if (x < 0)
x += mod;
}
void del(int &x, int k)
{
x -= k;
x %= mod;
if (x < 0)
x += mod;
}
struct SegmentTree
{
vector<int> st;
int n;
/// real n
SegmentTree(int _n = 0) : n(_n)
{
st.resize(4 * n + 10, inf);
}
void resize(int _n)
{
n = _n;
st.resize(4 * n + 10, inf);
}
void update(int id, int l, int r, int x, int val)
{
if (x > r or x < l)
return;
if (l == r)
{
st[id] += val;
return;
}
int mid = (l + r) >> 1;
update(id << 1, l, mid, x, val);
update(id << 1 | 1, mid + 1, r, x, val);
st[id] = min(st[id << 1], st[id << 1 | 1]);
}
void assign(int id, int l, int r, int x, int val)
{
if (x > r or x < l)
return;
if (l == r)
{
st[id] = val;
return;
}
int mid = (l + r) >> 1;
assign(id << 1, l, mid, x, val);
assign(id << 1 | 1, mid + 1, r, x, val);
st[id] = min(st[id << 1], st[id << 1 | 1]);
}
int get(int id, int l, int r, int u, int v)
{
if (l > v || u > r)
return inf;
if (u <= l && r <= v)
return st[id];
int mid = (l + r) >> 1;
return min(get(id << 1, l, mid, u, v), get(id << 1 | 1, mid + 1, r, u, v));
}
int walkright(int id, int l, int r, int x, int val)
{
if (st[id] >= val)
return -1;
if (r < x)
return -1;
if (l == r)
return l;
int ans = -1;
int mid = (l + r) >> 1;
if (st[id << 1] < val)
ans = walkright(id << 1, l, mid, x, val);
if (ans == -1)
ans = walkright(id << 1 | 1, mid + 1, r, x, val);
return ans;
}
int walkleft(int id, int l, int r, int x, int val)
{
if (st[id] >= val)
return -1;
if (x < l)
return -1;
if (l == r)
return l;
int ans = -1;
int mid = (l + r) >> 1;
if (st[id << 1 | 1] < val)
ans = walkleft(id << 1 | 1, mid + 1, r, x, val);
if (ans == -1)
ans = walkleft(id << 1, l, mid, x, val);
return ans;
}
int get(int l, int r)
{
return get(1, 1, n, l, r);
}
void update(int x, int val)
{
update(1, 1, n, x, val);
}
void assign(int x, int val)
{
assign(1, 1, n, x, val);
}
int walkleft(int x, int val)
{
return walkleft(1, 1, n, x, val);
}
int walkright(int x, int val)
{
return walkright(1, 1, n, x, val);
}
};
struct SegmentTree1
{
vector<int> st, lazy;
int n;
/// real n
SegmentTree1(int _n = 0) : n(_n)
{
st.resize(4 * n + 10,inf);
lazy.resize(4 * n + 10,inf);
}
void resize(int _n)
{
n = _n;
st.resize(4 * n + 10,inf);
lazy.resize(4 * n + 10,inf);
}
void down(int id, int l, int r)
{
if(lazy[id] == inf) return;
minimize(st[id << 1],lazy[id]);
minimize(lazy[id << 1],lazy[id]);
minimize(st[id << 1 | 1],lazy[id]);
minimize(lazy[id << 1 | 1],lazy[id]);
lazy[id] = inf;
}
void update(int id, int l, int r, int u, int v, int val)
{
if (u > r || l > v)
return;
if (u <= l && r <= v)
{
minimize(st[id],val);
minimize(lazy[id],val);
return;
}
down(id, l, r);
int mid = (l + r) >> 1;
update(id << 1, l, mid, u, v, val);
update(id << 1 | 1, mid + 1, r, u, v, val);
st[id] = min(st[id << 1], st[id << 1 | 1]);
}
int get(int id, int l, int r, int u, int v)
{
if (l > v || u > r)
return inf;
if (u <= l && r <= v)
return st[id];
down(id, l, r);
int mid = (l + r) >> 1;
return min(get(id << 1, l, mid, u, v), get(id << 1 | 1, mid + 1, r, u, v));
}
int get(int l, int r)
{
return get(1, 1, n, l, r);
}
void update(int l, int r, int val)
{
update(1, 1, n, l, r, val);
}
};
int n, k, a[maxn];
int l[maxn], r[maxn], f[2][maxn];
void L(int a[], int n, int ans[])
{
stack<int> st;
fo(i, 1, n)
{
while (st.size() and a[st.top()] <= a[i])
st.pop();
ans[i] = (st.empty() ? 0 : st.top());
st.push(i);
}
}
void R(int a[], int n, int ans[])
{
stack<int> st;
fod(i, n, 1)
{
while (st.size() and a[st.top()] <= a[i])
st.pop();
ans[i] = (st.empty() ? n + 1 : st.top());
st.push(i);
}
}
main()
{
#define name "TASK"
if (fopen(name ".inp", "r"))
{
freopen(name ".inp", "r", stdin);
freopen(name ".out", "w", stdout);
}
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> k;
fo(i, 1, n) cin >> a[i];
L(a, n, l);
R(a, n, r);
int maxx = 0;
fo(i,1,n) maximize(maxx,a[i]),f[1][i] = maxx;
fo(j, 2, k)
{
SegmentTree st(n);
SegmentTree1 minn(n);
fo(i, 1, n)
{
f[j & 1][i] = st.get(max(l[i],1ll), i - 1) + a[i];
minimize(f[j & 1][i],minn.get(i,i));
st.assign(i,f[j & 1 ^ 1][i]);
minn.update(i,r[i] - 1,f[j & 1][i]);
}
}
int ans = inf;
fod(i, n, 1) if (r[i] == n + 1) minimize(ans, f[k & 1][i]);
cout << ans;
}
Compilation message
blocks.cpp:249:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
249 | main()
| ^~~~
blocks.cpp: In function 'int main()':
blocks.cpp:271:46: error: no matching function for call to 'max(int&, long long int)'
271 | f[j & 1][i] = st.get(max(l[i],1ll), i - 1) + a[i];
| ^
In file included from /usr/include/c++/10/bits/specfun.h:45,
from /usr/include/c++/10/cmath:1927,
from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:41,
from blocks.cpp:1:
/usr/include/c++/10/bits/stl_algobase.h:254:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
254 | max(const _Tp& __a, const _Tp& __b)
| ^~~
/usr/include/c++/10/bits/stl_algobase.h:254:5: note: template argument deduction/substitution failed:
blocks.cpp:271:46: note: deduced conflicting types for parameter 'const _Tp' ('int' and 'long long int')
271 | f[j & 1][i] = st.get(max(l[i],1ll), i - 1) + a[i];
| ^
In file included from /usr/include/c++/10/bits/specfun.h:45,
from /usr/include/c++/10/cmath:1927,
from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:41,
from blocks.cpp:1:
/usr/include/c++/10/bits/stl_algobase.h:300:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
300 | max(const _Tp& __a, const _Tp& __b, _Compare __comp)
| ^~~
/usr/include/c++/10/bits/stl_algobase.h:300:5: note: template argument deduction/substitution failed:
blocks.cpp:271:46: note: deduced conflicting types for parameter 'const _Tp' ('int' and 'long long int')
271 | f[j & 1][i] = st.get(max(l[i],1ll), i - 1) + a[i];
| ^
In file included from /usr/include/c++/10/algorithm:62,
from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:65,
from blocks.cpp:1:
/usr/include/c++/10/bits/stl_algo.h:3480:5: note: candidate: 'template<class _Tp> constexpr _Tp std::max(std::initializer_list<_Tp>)'
3480 | max(initializer_list<_Tp> __l)
| ^~~
/usr/include/c++/10/bits/stl_algo.h:3480:5: note: template argument deduction/substitution failed:
blocks.cpp:271:46: note: mismatched types 'std::initializer_list<_Tp>' and 'int'
271 | f[j & 1][i] = st.get(max(l[i],1ll), i - 1) + a[i];
| ^
In file included from /usr/include/c++/10/algorithm:62,
from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:65,
from blocks.cpp:1:
/usr/include/c++/10/bits/stl_algo.h:3486:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::max(std::initializer_list<_Tp>, _Compare)'
3486 | max(initializer_list<_Tp> __l, _Compare __comp)
| ^~~
/usr/include/c++/10/bits/stl_algo.h:3486:5: note: template argument deduction/substitution failed:
blocks.cpp:271:46: note: mismatched types 'std::initializer_list<_Tp>' and 'int'
271 | f[j & 1][i] = st.get(max(l[i],1ll), i - 1) + a[i];
| ^
blocks.cpp:273:29: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
273 | st.assign(i,f[j & 1 ^ 1][i]);
| ~~^~~
blocks.cpp:254:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
254 | freopen(name ".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
blocks.cpp:255:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
255 | freopen(name ".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~