This documentation is automatically generated by competitive-verifier/competitive-verifier
// competitive-verifier: PROBLEM https://onlinejudge.u-aizu.ac.jp/problems/3086
// competitive-verifier: TLE 0.5
// competitive-verifier: MLE 64
#include <iostream>
#include <vector>
#include <algorithm>
#include "src/DataStructure/SegmentTree.hpp"
#include "src/Optimization/simplified_larsch_dp.hpp"
using namespace std;
struct RMQ {
using T= long long;
static T ti() { return 1e18; }
static T op(T l, T r) { return min(l, r); }
};
signed main() {
cin.tie(0);
ios::sync_with_stdio(0);
int N, L;
cin >> N >> L;
vector<long long> a(N);
for (int i= 0; i < N; ++i) cin >> a[i], a[i]= -a[i];
SegmentTree<RMQ> seg(a);
auto w= [&](int i, int j) -> long long {
if (i - j < L) return 1e18;
return seg.prod(j, i);
};
cout << -simplified_larsch_dp(N, w)[N] << '\n';
return 0;
}
#line 1 "test/aoj/3086.LARSCH.test.cpp"
// competitive-verifier: PROBLEM https://onlinejudge.u-aizu.ac.jp/problems/3086
// competitive-verifier: TLE 0.5
// competitive-verifier: MLE 64
#include <iostream>
#include <vector>
#include <algorithm>
#line 2 "src/DataStructure/SegmentTree.hpp"
#include <memory>
#include <cassert>
#line 2 "src/Internal/detection_idiom.hpp"
#include <type_traits>
#define _DETECT_BOOL(name, ...) \
template <class, class= void> struct name: std::false_type {}; \
template <class T> struct name<T, std::void_t<__VA_ARGS__>>: std::true_type {}; \
template <class T> static constexpr bool name##_v= name<T>::value
#define _DETECT_TYPE(name, type1, type2, ...) \
template <class T, class= void> struct name { \
using type= type2; \
}; \
template <class T> struct name<T, std::void_t<__VA_ARGS__>> { \
using type= type1; \
}
#line 7 "src/DataStructure/SegmentTree.hpp"
template <class M> class SegmentTree {
_DETECT_BOOL(monoid, typename T::T, decltype(&T::op), decltype(&T::ti));
_DETECT_BOOL(dual, typename T::T, typename T::E, decltype(&T::mp), decltype(&T::cp));
_DETECT_TYPE(nullptr_or_E, typename T::E, std::nullptr_t, typename T::E);
using T= typename M::T;
using E= typename nullptr_or_E<M>::type;
int n;
std::unique_ptr<T[]> dat;
std::unique_ptr<E[]> laz;
std::unique_ptr<bool[]> flg;
inline void update(int k) { dat[k]= M::op(dat[k << 1], dat[k << 1 | 1]); }
inline bool map(int k, E x, int sz) {
if constexpr (std::is_invocable_r_v<bool, decltype(M::mp), T &, E, int>) return M::mp(dat[k], x, sz);
else if constexpr (std::is_invocable_r_v<bool, decltype(M::mp), T &, E>) return M::mp(dat[k], x);
else if constexpr (std::is_invocable_r_v<void, decltype(M::mp), T &, E, int>) return M::mp(dat[k], x, sz), true;
else return M::mp(dat[k], x), true;
}
inline void prop(int k, E x, int sz) {
if (k < n) {
if (flg[k]) M::cp(laz[k], x);
else laz[k]= x;
flg[k]= true;
if constexpr (monoid_v<M>)
if (!map(k, x, sz)) push(k, sz), update(k);
} else {
if constexpr (monoid_v<M>) map(k, x, 1);
else map(k - n, x, 1);
}
}
inline void push(int k, int sz) {
if (flg[k]) prop(k << 1, laz[k], sz >> 1), prop(k << 1 | 1, laz[k], sz >> 1), flg[k]= false;
}
inline bool valid(int k) const {
int d= __builtin_clz(k) - __builtin_clz(n);
return (n >> d) != k || ((n >> d) << d) == n;
}
public:
SegmentTree() {}
SegmentTree(int n): n(n), dat(std::make_unique<T[]>(n << monoid_v<M>)) {
if constexpr (monoid_v<M>) std::fill_n(dat.get(), n << 1, M::ti());
if constexpr (dual_v<M>) laz= std::make_unique<E[]>(n), flg= std::make_unique<bool[]>(n), std::fill_n(flg.get(), n, false);
}
template <class F> SegmentTree(int n, const F &init): n(n), dat(std::make_unique<T[]>(n << monoid_v<M>)) {
auto a= dat.get() + (n & -monoid_v<M>);
for (int i= 0; i < n; ++i) a[i]= init(i);
if constexpr (monoid_v<M>) build();
if constexpr (dual_v<M>) laz= std::make_unique<E[]>(n), flg= std::make_unique<bool[]>(n), std::fill_n(flg.get(), n, false);
}
SegmentTree(int n, T x): SegmentTree(n, [x](int) { return x; }) {}
SegmentTree(const std::vector<T> &v): SegmentTree(v.size(), [&v](int i) { return v[i]; }) {}
SegmentTree(const T *bg, const T *ed): SegmentTree(ed - bg, [bg](int i) { return bg[i]; }) {}
void build() {
static_assert(monoid_v<M>, "\"build\" is not available\n");
for (int i= n; --i;) update(i);
}
inline void unsafe_set(int i, T x) {
static_assert(monoid_v<M>, "\"unsafe_set\" is not available\n");
dat[i + n]= x;
}
inline void set(int i, T x) {
get(i);
if constexpr (monoid_v<M>)
for (dat[i+= n]= x; i>>= 1;) update(i);
else dat[i]= x;
}
inline void mul(int i, T x) {
static_assert(monoid_v<M>, "\"mul\" is not available\n");
set(i, M::op(get(i), x));
}
inline T get(int i) {
i+= n;
if constexpr (dual_v<M>)
for (int j= 31 - __builtin_clz(i); j; --j) push(i >> j, 1 << j);
if constexpr (monoid_v<M>) return dat[i];
else return dat[i - n];
}
inline T operator[](int i) { return get(i); }
inline T prod(int l, int r) {
static_assert(monoid_v<M>, "\"prod\" is not available\n");
l+= n, r+= n;
if constexpr (dual_v<M>) {
for (int j= 31 - __builtin_clz(l); ((l >> j) << j) != l; --j) push(l >> j, 1 << j);
for (int j= 31 - __builtin_clz(r); ((r >> j) << j) != r; --j) push(r >> j, 1 << j);
}
T s1= M::ti(), s2= M::ti();
for (; l < r; l>>= 1, r>>= 1) {
if (l & 1) s1= M::op(s1, dat[l++]);
if (r & 1) s2= M::op(dat[--r], s2);
}
return M::op(s1, s2);
}
inline void apply(int l, int r, E x) {
static_assert(dual_v<M>, "\"apply\" is not available\n");
l+= n, r+= n;
for (int j= 31 - __builtin_clz(l); ((l >> j) << j) != l; j--) push(l >> j, 1 << j);
for (int j= 31 - __builtin_clz(r); ((r >> j) << j) != r; j--) push(r >> j, 1 << j);
for (int a= l, b= r, sz= 1; a < b; a>>= 1, b>>= 1, sz<<= 1) {
if (a & 1) prop(a++, x, sz);
if (b & 1) prop(--b, x, sz);
}
if constexpr (monoid_v<M>) {
for (int j= __builtin_ctz(l) + 1; l >> j; ++j) update(l >> j);
for (int j= __builtin_ctz(r) + 1; r >> j; ++j) update(r >> j);
}
}
template <class C> int max_right(int l, const C &check) {
static_assert(monoid_v<M>, "\"max_right\" is not available\n");
assert(check(M::ti()));
if (check(prod(l, n))) return n;
T s= M::ti(), t;
int sz= 1;
for (get(l), l+= n;; s= t, ++l) {
while (!(l & 1) && valid(l >> 1)) l>>= 1, sz<<= 1;
if (!check(t= M::op(s, dat[l]))) {
while (l < n) {
if constexpr (dual_v<M>) push(l, sz);
l<<= 1, sz>>= 1;
if (check(t= M::op(s, dat[l]))) s= t, ++l;
}
return l - n;
}
}
}
template <class C> int min_left(int r, const C &check) {
static_assert(monoid_v<M>, "\"min_left\" is not available\n");
assert(check(M::ti()));
if (check(prod(0, r))) return 0;
T s= M::ti(), t;
int sz= 1;
for (get(--r), r+= n;; s= t, --r) {
while (!valid(r)) r= r << 1 | 1, sz>>= 1;
while ((r & 1) && valid(r >> 1)) r>>= 1, sz<<= 1;
if (!check(t= M::op(dat[r], s))) {
while (r < n) {
if constexpr (dual_v<M>) push(r, sz);
r= r << 1 | 1, sz>>= 1;
if (check(t= M::op(dat[r], s))) s= t, --r;
}
return r + 1 - n;
}
}
}
};
#line 3 "src/Optimization/simplified_larsch_dp.hpp"
#include <limits>
#line 3 "src/Internal/function_traits.hpp"
// clang-format off
namespace function_template_internal{
template<class C>struct is_function_object{
template<class U,int dummy=(&U::operator(),0)> static std::true_type check(U *);
static std::false_type check(...);
static C *m;
static constexpr bool value= decltype(check(m))::value;
};
template<class F,bool,bool>struct function_type_impl{using type= void;};
template<class F>struct function_type_impl<F,true,false>{using type= F *;};
template<class F>struct function_type_impl<F,false,true>{using type= decltype(&F::operator());};
template<class F> using function_type_t= typename function_type_impl<F,std::is_function_v<F>,is_function_object<F>::value>::type;
template<class... Args>struct result_type_impl{using type= void;};
template<class R,class... Args>struct result_type_impl<R(*)(Args...)>{using type= R;};
template<class C,class R,class... Args>struct result_type_impl<R(C::*)(Args...)>{using type= R;};
template<class C,class R,class... Args>struct result_type_impl<R(C::*)(Args...)const>{using type= R;};
template<class F> using result_type_t= typename result_type_impl<function_type_t<F>>::type;
template<class... Args>struct argument_type_impl{using type= void;};
template<class R,class... Args>struct argument_type_impl<R(*)(Args...)>{using type= std::tuple<Args...>;};
template<class C,class R,class... Args>struct argument_type_impl<R(C::*)(Args...)>{using type= std::tuple<Args...>;};
template<class C,class R,class... Args>struct argument_type_impl<R(C::*)(Args...)const>{using type= std::tuple<Args...>;};
template<class F> using argument_type_t= typename argument_type_impl<function_type_t<F>>::type;
}
using function_template_internal::result_type_t,function_template_internal::argument_type_t;
// clang-format on
#line 5 "src/Optimization/simplified_larsch_dp.hpp"
// dp[i] = min_{j<i} (dp[j] + w(i,j))
// w(i,j) -> monge cost
template <class F> std::vector<result_type_t<F>> simplified_larsch_dp(int n, const F &w) {
using T= result_type_t<F>;
std::vector<T> dp(n + 1, std::numeric_limits<T>::max());
std::vector<int> x(n + 1);
auto check= [&](int i, int j) {
if (T cost= dp[j] + w(i, j); dp[i] > cost) dp[i]= cost, x[i]= j;
};
auto rec= [&](auto &rec, int l, int r) {
if (r - l <= 1) return;
int m= (l + r) / 2;
for (int i= x[l]; i <= x[r]; ++i) check(m, i);
rec(rec, l, m);
for (int i= l + 1; i <= m; ++i) check(r, i);
rec(rec, m, r);
};
return dp[0]= 0, check(n, 0), rec(rec, 0, n), dp;
}
#line 9 "test/aoj/3086.LARSCH.test.cpp"
using namespace std;
struct RMQ {
using T= long long;
static T ti() { return 1e18; }
static T op(T l, T r) { return min(l, r); }
};
signed main() {
cin.tie(0);
ios::sync_with_stdio(0);
int N, L;
cin >> N >> L;
vector<long long> a(N);
for (int i= 0; i < N; ++i) cin >> a[i], a[i]= -a[i];
SegmentTree<RMQ> seg(a);
auto w= [&](int i, int j) -> long long {
if (i - j < L) return 1e18;
return seg.prod(j, i);
};
cout << -simplified_larsch_dp(N, w)[N] << '\n';
return 0;
}
Env | Name | Status | Elapsed | Memory |
---|---|---|---|---|
g++-13 | 00_sample_01.in |
![]() |
5 ms | 4 MB |
g++-13 | 00_sample_02.in |
![]() |
5 ms | 4 MB |
g++-13 | 00_sample_03.in |
![]() |
5 ms | 4 MB |
g++-13 | 00_sample_04.in |
![]() |
4 ms | 4 MB |
g++-13 | 10_small_01.in |
![]() |
4 ms | 4 MB |
g++-13 | 10_small_02.in |
![]() |
5 ms | 4 MB |
g++-13 | 10_small_03.in |
![]() |
5 ms | 3 MB |
g++-13 | 10_small_04.in |
![]() |
5 ms | 4 MB |
g++-13 | 10_small_05.in |
![]() |
5 ms | 4 MB |
g++-13 | 10_small_06.in |
![]() |
5 ms | 4 MB |
g++-13 | 10_small_07.in |
![]() |
4 ms | 4 MB |
g++-13 | 10_small_08.in |
![]() |
4 ms | 4 MB |
g++-13 | 10_small_09.in |
![]() |
5 ms | 4 MB |
g++-13 | 10_small_10.in |
![]() |
4 ms | 4 MB |
g++-13 | 10_small_11.in |
![]() |
4 ms | 4 MB |
g++-13 | 10_small_12.in |
![]() |
4 ms | 3 MB |
g++-13 | 10_small_13.in |
![]() |
4 ms | 4 MB |
g++-13 | 10_small_14.in |
![]() |
4 ms | 4 MB |
g++-13 | 10_small_15.in |
![]() |
4 ms | 4 MB |
g++-13 | 10_small_16.in |
![]() |
4 ms | 4 MB |
g++-13 | 10_small_17.in |
![]() |
4 ms | 4 MB |
g++-13 | 10_small_18.in |
![]() |
4 ms | 4 MB |
g++-13 | 10_small_19.in |
![]() |
4 ms | 4 MB |
g++-13 | 10_small_20.in |
![]() |
4 ms | 4 MB |
g++-13 | 20_random_01.in |
![]() |
5 ms | 4 MB |
g++-13 | 20_random_02.in |
![]() |
7 ms | 4 MB |
g++-13 | 20_random_03.in |
![]() |
5 ms | 4 MB |
g++-13 | 20_random_04.in |
![]() |
5 ms | 4 MB |
g++-13 | 20_random_05.in |
![]() |
6 ms | 4 MB |
g++-13 | 20_random_06.in |
![]() |
5 ms | 4 MB |
g++-13 | 20_random_07.in |
![]() |
6 ms | 4 MB |
g++-13 | 20_random_08.in |
![]() |
5 ms | 4 MB |
g++-13 | 20_random_09.in |
![]() |
7 ms | 4 MB |
g++-13 | 20_random_10.in |
![]() |
5 ms | 4 MB |
g++-13 | 30_large_01.in |
![]() |
148 ms | 10 MB |
g++-13 | 30_large_02.in |
![]() |
115 ms | 9 MB |
g++-13 | 30_large_03.in |
![]() |
20 ms | 9 MB |
g++-13 | 31_large_allneg_01.in |
![]() |
40 ms | 10 MB |
g++-13 | 31_large_allneg_02.in |
![]() |
42 ms | 9 MB |
g++-13 | 31_large_allneg_03.in |
![]() |
21 ms | 9 MB |
g++-13 | 32_large_allpos_01.in |
![]() |
143 ms | 10 MB |
g++-13 | 32_large_allpos_02.in |
![]() |
118 ms | 9 MB |
g++-13 | 32_large_allpos_03.in |
![]() |
19 ms | 9 MB |
g++-13 | 40_long_01.in |
![]() |
123 ms | 10 MB |
g++-13 | 40_long_02.in |
![]() |
104 ms | 9 MB |
g++-13 | 40_long_03.in |
![]() |
127 ms | 9 MB |
g++-13 | 41_short_01.in |
![]() |
131 ms | 10 MB |
g++-13 | 41_short_02.in |
![]() |
108 ms | 9 MB |
g++-13 | 41_short_03.in |
![]() |
113 ms | 9 MB |
clang++-18 | 00_sample_01.in |
![]() |
5 ms | 4 MB |
clang++-18 | 00_sample_02.in |
![]() |
5 ms | 4 MB |
clang++-18 | 00_sample_03.in |
![]() |
4 ms | 4 MB |
clang++-18 | 00_sample_04.in |
![]() |
4 ms | 4 MB |
clang++-18 | 10_small_01.in |
![]() |
4 ms | 4 MB |
clang++-18 | 10_small_02.in |
![]() |
5 ms | 4 MB |
clang++-18 | 10_small_03.in |
![]() |
4 ms | 4 MB |
clang++-18 | 10_small_04.in |
![]() |
5 ms | 4 MB |
clang++-18 | 10_small_05.in |
![]() |
4 ms | 4 MB |
clang++-18 | 10_small_06.in |
![]() |
4 ms | 4 MB |
clang++-18 | 10_small_07.in |
![]() |
5 ms | 4 MB |
clang++-18 | 10_small_08.in |
![]() |
4 ms | 4 MB |
clang++-18 | 10_small_09.in |
![]() |
5 ms | 4 MB |
clang++-18 | 10_small_10.in |
![]() |
4 ms | 4 MB |
clang++-18 | 10_small_11.in |
![]() |
4 ms | 4 MB |
clang++-18 | 10_small_12.in |
![]() |
4 ms | 4 MB |
clang++-18 | 10_small_13.in |
![]() |
4 ms | 4 MB |
clang++-18 | 10_small_14.in |
![]() |
4 ms | 4 MB |
clang++-18 | 10_small_15.in |
![]() |
4 ms | 4 MB |
clang++-18 | 10_small_16.in |
![]() |
5 ms | 4 MB |
clang++-18 | 10_small_17.in |
![]() |
5 ms | 4 MB |
clang++-18 | 10_small_18.in |
![]() |
4 ms | 4 MB |
clang++-18 | 10_small_19.in |
![]() |
4 ms | 4 MB |
clang++-18 | 10_small_20.in |
![]() |
5 ms | 4 MB |
clang++-18 | 20_random_01.in |
![]() |
5 ms | 4 MB |
clang++-18 | 20_random_02.in |
![]() |
7 ms | 4 MB |
clang++-18 | 20_random_03.in |
![]() |
5 ms | 4 MB |
clang++-18 | 20_random_04.in |
![]() |
5 ms | 4 MB |
clang++-18 | 20_random_05.in |
![]() |
6 ms | 4 MB |
clang++-18 | 20_random_06.in |
![]() |
5 ms | 4 MB |
clang++-18 | 20_random_07.in |
![]() |
6 ms | 4 MB |
clang++-18 | 20_random_08.in |
![]() |
5 ms | 4 MB |
clang++-18 | 20_random_09.in |
![]() |
7 ms | 4 MB |
clang++-18 | 20_random_10.in |
![]() |
5 ms | 4 MB |
clang++-18 | 30_large_01.in |
![]() |
143 ms | 10 MB |
clang++-18 | 30_large_02.in |
![]() |
111 ms | 9 MB |
clang++-18 | 30_large_03.in |
![]() |
20 ms | 9 MB |
clang++-18 | 31_large_allneg_01.in |
![]() |
38 ms | 10 MB |
clang++-18 | 31_large_allneg_02.in |
![]() |
41 ms | 9 MB |
clang++-18 | 31_large_allneg_03.in |
![]() |
21 ms | 9 MB |
clang++-18 | 32_large_allpos_01.in |
![]() |
138 ms | 10 MB |
clang++-18 | 32_large_allpos_02.in |
![]() |
111 ms | 9 MB |
clang++-18 | 32_large_allpos_03.in |
![]() |
19 ms | 9 MB |
clang++-18 | 40_long_01.in |
![]() |
121 ms | 10 MB |
clang++-18 | 40_long_02.in |
![]() |
102 ms | 9 MB |
clang++-18 | 40_long_03.in |
![]() |
120 ms | 9 MB |
clang++-18 | 41_short_01.in |
![]() |
127 ms | 10 MB |
clang++-18 | 41_short_02.in |
![]() |
104 ms | 9 MB |
clang++-18 | 41_short_03.in |
![]() |
110 ms | 9 MB |