This documentation is automatically generated by competitive-verifier/competitive-verifier
// competitive-verifier: PROBLEM https://onlinejudge.u-aizu.ac.jp/problems/3086
// competitive-verifier: TLE 1
// competitive-verifier: MLE 64
#include <iostream>
#include <vector>
#include <algorithm>
#include "src/DataStructure/SegmentTree.hpp"
#include "src/Optimization/LiChaoTree.hpp"
using namespace std;
struct RMQ {
using T= long long;
static T ti() { return -1e18; }
static T op(T l, T r) { return max(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];
SegmentTree<RMQ> seg(a);
auto w= [&](int i, int j, long long d) { return d + seg.prod(j, i); };
LiChaoTree lct(w, 1, N + 1);
auto tree= lct.make_tree<MAXIMIZE>();
tree.insert(0, 0, L);
for (int i= 1; i < N; ++i) tree.insert(i, tree.query(i).first, i + L);
cout << tree.query(N).first << '\n';
return 0;
}
#line 1 "test/aoj/3086.LiCT.test.cpp"
// competitive-verifier: PROBLEM https://onlinejudge.u-aizu.ac.jp/problems/3086
// competitive-verifier: TLE 1
// 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 2 "src/Optimization/LiChaoTree.hpp"
#include <limits>
#line 5 "src/Optimization/LiChaoTree.hpp"
#include <tuple>
#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 2 "src/Optimization/MinMaxEnum.hpp"
enum MinMaxEnum { MAXIMIZE= -1, MINIMIZE= 1 };
#line 9 "src/Optimization/LiChaoTree.hpp"
template <class F, class T> class LiChaoTree {};
template <class F, class T, class... Prms> class LiChaoTree<F, std::tuple<T, Prms...>> {
using R= result_type_t<F>;
F f;
const T LB, UB;
std::vector<std::tuple<Prms...>> ps;
template <MinMaxEnum sgn, bool persistent> class LiChaoTreeInterface {
LiChaoTree *ins;
struct Node {
int id= -1;
Node *ch[2]= {nullptr, nullptr};
} *root;
inline R eval(int id, T x) const { return std::apply(ins->f, std::tuple_cat(std::make_tuple(x), ins->ps[id])); }
static inline bool cmp(const R &p, const R &n, int pi, int ni) {
if constexpr (sgn == MINIMIZE) return p > n || (p == n && pi > ni);
else return p < n || (p == n && pi > ni);
}
inline bool cmp(T x, int pi, int ni) const {
if (ni == -1) return false;
if (pi == -1) return true;
return cmp(eval(pi, x), eval(ni, x), pi, ni);
}
static inline T ub(T r) {
if constexpr (std::is_floating_point_v<T>) return r;
else return r - 1;
}
inline void addl(Node *&t, int id, T xl, T xr) {
if (!t) return t= new Node{id}, void();
bool bl= cmp(xl, t->id, id), br= cmp(ub(xr), t->id, id);
if (!bl && !br) return;
if constexpr (persistent) t= new Node(*t);
if (bl && br) return t->id= id, void();
T xm= (xl + xr) / 2;
if (cmp(xm, t->id, id)) std::swap(t->id, id), bl= !bl;
if constexpr (std::is_floating_point_v<T>)
if (xr - xl < 1e-9) return;
bl ? addl(t->ch[0], id, xl, xm) : addl(t->ch[1], id, xm, xr);
}
inline void adds(Node *&t, int id, T l, T r, T xl, T xr) {
if (r <= xl || xr <= l) return;
if (l <= xl && xr <= r) return addl(t, id, xl, xr);
if (!t) t= new Node;
else if constexpr (persistent) t= new Node(*t);
T xm= (xl + xr) / 2;
adds(t->ch[0], id, l, r, xl, xm), adds(t->ch[1], id, l, r, xm, xr);
}
inline std::pair<R, int> query(const Node *t, T x, T xl, T xr) const {
if (!t) return {R(), -1};
T xm= (xl + xr) / 2;
auto b= x < xm ? query(t->ch[0], x, xl, xm) : query(t->ch[1], x, xm, xr);
if (t->id == -1) return b;
R a= eval(t->id, x);
return b.second != -1 && cmp(a, b.first, t->id, b.second) ? b : std::make_pair(a, t->id);
}
public:
LiChaoTreeInterface()= default;
LiChaoTreeInterface(LiChaoTree *ins): ins(ins), root(nullptr) {}
void insert(const Prms &...args) { ins->ps.emplace_back(args...), addl(root, ins->ps.size() - 1, ins->LB, ins->UB); }
// [l, r)
void insert(const Prms &...args, T l, T r) {
l= std::max(l, ins->LB), r= std::min(r, ins->UB);
if (l < r) ins->ps.emplace_back(args...), adds(root, ins->ps.size() - 1, l, r, ins->LB, ins->UB);
}
// [l, UB)
void insert(const Prms &...args, T l) { insert(args..., l, ins->UB); }
std::pair<R, int> query(T x) const { return assert(ins->LB <= x && x < ins->UB), query(root, x, ins->LB, ins->UB); }
const std::tuple<Prms...> ¶ms(int id) const { return ins->ps[id]; }
};
public:
LiChaoTree(const F &f, T LB= -2e9, T UB= 2e9): f(f), LB(LB), UB(UB) {}
template <MinMaxEnum sgn= MINIMIZE, bool persistent= false> LiChaoTreeInterface<sgn, persistent> make_tree() { return this; }
};
template <class F, class T, class U> LiChaoTree(F, T, U) -> LiChaoTree<F, argument_type_t<F>>;
template <class F, class T> LiChaoTree(F, T) -> LiChaoTree<F, argument_type_t<F>>;
template <class F> LiChaoTree(F) -> LiChaoTree<F, argument_type_t<F>>;
#line 9 "test/aoj/3086.LiCT.test.cpp"
using namespace std;
struct RMQ {
using T= long long;
static T ti() { return -1e18; }
static T op(T l, T r) { return max(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];
SegmentTree<RMQ> seg(a);
auto w= [&](int i, int j, long long d) { return d + seg.prod(j, i); };
LiChaoTree lct(w, 1, N + 1);
auto tree= lct.make_tree<MAXIMIZE>();
tree.insert(0, 0, L);
for (int i= 1; i < N; ++i) tree.insert(i, tree.query(i).first, i + L);
cout << tree.query(N).first << '\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 |
![]() |
4 ms | 4 MB |
g++-13 | 10_small_03.in |
![]() |
4 ms | 3 MB |
g++-13 | 10_small_04.in |
![]() |
4 ms | 4 MB |
g++-13 | 10_small_05.in |
![]() |
4 ms | 4 MB |
g++-13 | 10_small_06.in |
![]() |
4 ms | 4 MB |
g++-13 | 10_small_07.in |
![]() |
4 ms | 3 MB |
g++-13 | 10_small_08.in |
![]() |
4 ms | 3 MB |
g++-13 | 10_small_09.in |
![]() |
4 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 | 4 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 |
![]() |
5 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 |
![]() |
9 ms | 4 MB |
g++-13 | 20_random_03.in |
![]() |
5 ms | 4 MB |
g++-13 | 20_random_04.in |
![]() |
6 ms | 4 MB |
g++-13 | 20_random_05.in |
![]() |
8 ms | 4 MB |
g++-13 | 20_random_06.in |
![]() |
6 ms | 4 MB |
g++-13 | 20_random_07.in |
![]() |
7 ms | 4 MB |
g++-13 | 20_random_08.in |
![]() |
6 ms | 4 MB |
g++-13 | 20_random_09.in |
![]() |
9 ms | 4 MB |
g++-13 | 20_random_10.in |
![]() |
5 ms | 4 MB |
g++-13 | 30_large_01.in |
![]() |
268 ms | 15 MB |
g++-13 | 30_large_02.in |
![]() |
244 ms | 14 MB |
g++-13 | 30_large_03.in |
![]() |
33 ms | 7 MB |
g++-13 | 31_large_allneg_01.in |
![]() |
267 ms | 15 MB |
g++-13 | 31_large_allneg_02.in |
![]() |
241 ms | 14 MB |
g++-13 | 31_large_allneg_03.in |
![]() |
33 ms | 7 MB |
g++-13 | 32_large_allpos_01.in |
![]() |
266 ms | 15 MB |
g++-13 | 32_large_allpos_02.in |
![]() |
245 ms | 14 MB |
g++-13 | 32_large_allpos_03.in |
![]() |
31 ms | 7 MB |
g++-13 | 40_long_01.in |
![]() |
208 ms | 13 MB |
g++-13 | 40_long_02.in |
![]() |
208 ms | 13 MB |
g++-13 | 40_long_03.in |
![]() |
232 ms | 13 MB |
g++-13 | 41_short_01.in |
![]() |
312 ms | 18 MB |
g++-13 | 41_short_02.in |
![]() |
252 ms | 14 MB |
g++-13 | 41_short_03.in |
![]() |
276 ms | 18 MB |
clang++-18 | 00_sample_01.in |
![]() |
6 ms | 4 MB |
clang++-18 | 00_sample_02.in |
![]() |
5 ms | 4 MB |
clang++-18 | 00_sample_03.in |
![]() |
5 ms | 4 MB |
clang++-18 | 00_sample_04.in |
![]() |
5 ms | 4 MB |
clang++-18 | 10_small_01.in |
![]() |
5 ms | 4 MB |
clang++-18 | 10_small_02.in |
![]() |
5 ms | 4 MB |
clang++-18 | 10_small_03.in |
![]() |
5 ms | 4 MB |
clang++-18 | 10_small_04.in |
![]() |
5 ms | 4 MB |
clang++-18 | 10_small_05.in |
![]() |
5 ms | 4 MB |
clang++-18 | 10_small_06.in |
![]() |
5 ms | 4 MB |
clang++-18 | 10_small_07.in |
![]() |
5 ms | 4 MB |
clang++-18 | 10_small_08.in |
![]() |
5 ms | 4 MB |
clang++-18 | 10_small_09.in |
![]() |
5 ms | 4 MB |
clang++-18 | 10_small_10.in |
![]() |
5 ms | 4 MB |
clang++-18 | 10_small_11.in |
![]() |
5 ms | 4 MB |
clang++-18 | 10_small_12.in |
![]() |
5 ms | 4 MB |
clang++-18 | 10_small_13.in |
![]() |
5 ms | 4 MB |
clang++-18 | 10_small_14.in |
![]() |
5 ms | 4 MB |
clang++-18 | 10_small_15.in |
![]() |
5 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 |
![]() |
5 ms | 4 MB |
clang++-18 | 10_small_19.in |
![]() |
5 ms | 4 MB |
clang++-18 | 10_small_20.in |
![]() |
5 ms | 4 MB |
clang++-18 | 20_random_01.in |
![]() |
6 ms | 4 MB |
clang++-18 | 20_random_02.in |
![]() |
10 ms | 4 MB |
clang++-18 | 20_random_03.in |
![]() |
6 ms | 4 MB |
clang++-18 | 20_random_04.in |
![]() |
7 ms | 4 MB |
clang++-18 | 20_random_05.in |
![]() |
8 ms | 4 MB |
clang++-18 | 20_random_06.in |
![]() |
6 ms | 4 MB |
clang++-18 | 20_random_07.in |
![]() |
8 ms | 4 MB |
clang++-18 | 20_random_08.in |
![]() |
6 ms | 4 MB |
clang++-18 | 20_random_09.in |
![]() |
9 ms | 4 MB |
clang++-18 | 20_random_10.in |
![]() |
6 ms | 4 MB |
clang++-18 | 30_large_01.in |
![]() |
285 ms | 15 MB |
clang++-18 | 30_large_02.in |
![]() |
260 ms | 14 MB |
clang++-18 | 30_large_03.in |
![]() |
33 ms | 7 MB |
clang++-18 | 31_large_allneg_01.in |
![]() |
284 ms | 15 MB |
clang++-18 | 31_large_allneg_02.in |
![]() |
256 ms | 14 MB |
clang++-18 | 31_large_allneg_03.in |
![]() |
33 ms | 7 MB |
clang++-18 | 32_large_allpos_01.in |
![]() |
283 ms | 15 MB |
clang++-18 | 32_large_allpos_02.in |
![]() |
261 ms | 14 MB |
clang++-18 | 32_large_allpos_03.in |
![]() |
33 ms | 7 MB |
clang++-18 | 40_long_01.in |
![]() |
222 ms | 13 MB |
clang++-18 | 40_long_02.in |
![]() |
219 ms | 13 MB |
clang++-18 | 40_long_03.in |
![]() |
244 ms | 13 MB |
clang++-18 | 41_short_01.in |
![]() |
335 ms | 17 MB |
clang++-18 | 41_short_02.in |
![]() |
269 ms | 14 MB |
clang++-18 | 41_short_03.in |
![]() |
293 ms | 17 MB |