제출 #569875

#제출 시각아이디문제언어결과실행 시간메모리
569875shiomusubi496수열 (APIO14_sequence)C++17
49 / 100
90 ms131072 KiB
#line 2 "library/other/template.hpp" #include<bits/stdc++.h> #ifndef __COUNTER__ #define __COUNTER__ __LINE__ #endif #define REP_SELECTER(a, b, c, d, e, ...) e #define REP1_0(b, c) REP1_1(b, c) #define REP1_1(b, c) for (ll REP_COUNTER_ ## c = 0; REP_COUNTER_ ## c < (ll)(b); ++ REP_COUNTER_ ## c) #define REP1(b) REP1_0(b, __COUNTER__) #define REP2(i, b) for (ll i = 0; i < (ll)(b); ++i) #define REP3(i, a, b) for (ll i = (ll)(a); i < (ll)(b); ++i) #define REP4(i, a, b, c) for (ll i = (ll)(a); i < (ll)(b); i += (ll)(c)) #define rep(...) REP_SELECTER(__VA_ARGS__, REP4, REP3, REP2, REP1) (__VA_ARGS__) #define RREP2(i, a) for (ll i = (ll)(a) - 1; i >= 0; --i) #define RREP3(i, a, b) for (ll i = (ll)(a) - 1; i >= (ll)(b); --i) #define RREP4(i, a, b, c) for (ll i = (ll)(a) - 1; i >= (ll)(b); i -= (ll)(c)) #define rrep(...) REP_SELECTER(__VA_ARGS__, RREP4, RREP3, RREP2) (__VA_ARGS__) #define REPS2(i, b) for (ll i = 1; i <= (ll)(b); ++i) #define REPS3(i, a, b) for (ll i = (ll)(a) + 1; i <= (ll)(b); ++i) #define REPS4(i, a, b, c) for (ll i = (ll)(a) + 1; i <= (ll)(b); i += (ll)(c)) #define reps(...) REP_SELECTER(__VA_ARGS__, REPS4, REPS3, REPS2) (__VA_ARGS__) #define RREPS2(i, a) for (ll i = (ll)(a); i > 0; --i) #define RREPS3(i, a, b) for (ll i = (ll)(a); i > (ll)(b); --i) #define RREPS4(i, a, b, c) for (ll i = (ll)(a); i > (ll)(b); i -= (ll)(c)) #define rreps(...) REP_SELECTER(__VA_ARGS__, RREPS4, RREPS3, RREPS2) (__VA_ARGS__) #define each_for(...) for (auto&& __VA_ARGS__) #define each_const(...) for (const auto& __VA_ARGS__) #define all(v) std::begin(v), std::end(v) #define rall(v) std::rbegin(v), std::rend(v) #if __cplusplus >= 201402L #define CONSTEXPR constexpr #else #define CONSTEXPR #endif #ifdef __cpp_if_constexpr #define IF_CONSTEXPR constexpr #else #define IF_CONSTEXPR #endif using ll = long long; using ull = unsigned long long; using ld = long double; using PLL = std::pair<ll, ll>; template<class T> using prique = std::priority_queue<T, std::vector<T>, std::greater<T>>; template<class T> class infinity { public: static constexpr T value = std::numeric_limits<T>::max() / 2; static constexpr T mvalue = std::numeric_limits<T>::min() / 2; static constexpr T max = std::numeric_limits<T>::max(); static constexpr T min = std::numeric_limits<T>::min(); }; #if __cplusplus <= 201402L template<class T> constexpr T infinity<T>::value; template<class T> constexpr T infinity<T>::mvalue; template<class T> constexpr T infinity<T>::max; template<class T> constexpr T infinity<T>::min; #endif #if __cplusplus >= 201402L template<class T> constexpr T INF = infinity<T>::value; #endif constexpr ll inf = infinity<ll>::value; constexpr ld EPS = 1e-8; constexpr ld PI = 3.1415926535897932384626; template<class T, class U> std::ostream& operator<<(std::ostream& ost, const std::pair<T, U>& p) { return ost << p.first << ' ' << p.second; } template<class T, class U> std::istream& operator>>(std::istream& ist, std::pair<T, U>& p) { return ist >> p.first >> p.second; } template<class Container, typename std::enable_if<!std::is_same<Container, std::string>::value>::type* = nullptr> auto operator<<(std::ostream& ost, const Container& cont) -> decltype(cont.begin(), cont.end(), ost) { for (auto itr = cont.begin(); itr != cont.end(); ++itr) { if (itr != cont.begin()) ost << ' '; ost << *itr; } return ost; } template<class Container, typename std::enable_if<!std::is_same<Container, std::string>::value>::type* = nullptr> auto operator>>(std::istream& ist, Container& cont) -> decltype(cont.begin(), cont.end(), ist) { for (auto itr = cont.begin(); itr != cont.end(); ++itr) ist >> *itr; return ist; } template<class T, class U> inline constexpr bool chmin(T &a, const U &b) noexcept { return a > b ? a = b, true : false; } template<class T, class U> inline constexpr bool chmax(T &a, const U &b) noexcept { return a < b ? a = b, true : false; } inline CONSTEXPR ll gcd(ll a, ll b) noexcept { while (b) { const ll c = a; a = b; b = c % b; } return a; } inline CONSTEXPR ll lcm(ll a, ll b) noexcept { return a / gcd(a, b) * b; } inline CONSTEXPR bool is_prime(ll N) noexcept { if (N <= 1) return false; for (ll i = 2; i * i <= N; ++i) { if (N % i == 0) return false; } return true; } inline std::vector<ll> prime_factor(ll N) noexcept { std::vector<ll> res; for (ll i = 2; i * i <= N; ++i) { while (N % i == 0) { res.push_back(i); N /= i; } } if (N != 1) res.push_back(N); return res; } inline CONSTEXPR ll my_pow(ll a, ll b) noexcept { ll res = 1; while (b) { if (b & 1) res *= a; b >>= 1; a *= a; } return res; } inline CONSTEXPR ll mod_pow(ll a, ll b, ll mod) noexcept { a %= mod; ll res = 1; while (b) { if (b & 1) (res *= a) %= mod; b >>= 1; (a *= a) %= mod; } return res; } inline PLL extGCD(ll a, ll b) noexcept { const ll n = a, m = b; ll x = 1, y = 0, u = 0, v = 1; ll t; while (b) { t = a / b; std::swap(a -= t * b, b); std::swap(x -= t * u, u); std::swap(y -= t * v, v); } if (x < 0) { x += m; y -= n; } return {x, y}; } inline ll mod_inv(ll a, ll mod) noexcept { ll b = mod; ll x = 1, u = 0; ll t; while (b) { t = a / b; std::swap(a -= t * b, b); std::swap(x -= t * u, u); } if (x < 0) x += mod; assert(a == 1); return x; } inline PLL ChineseRemainder(ll b1, ll m1, ll b2, ll m2) noexcept { const PLL p = extGCD(m1, m2); const ll g = p.first * m1 + p.second * m2; const ll l = m1 / g * m2; if ((b2 - b1) % g != 0) return PLL{-1, -1}; const ll x = (b2 - b1) / g * p.first % (m2 / g); return {(x * m1 + b1 + l) % l, l}; } PLL ChineseRemainders(const std::vector<ll>& b, const std::vector<ll>& m) noexcept { PLL res{0, 1}; rep (i, b.size()) { res = ChineseRemainder(res.first, res.second, b[i], m[i]); if (res.first == -1) return res; } return res; } template<class F> class RecLambda { private: F f; public: explicit constexpr RecLambda(F&& f_) : f(std::forward<F>(f_)) {} template<class... Args> constexpr auto operator()(Args&&... args) const -> decltype(f(*this, std::forward<Args>(args)...)) { return f(*this, std::forward<Args>(args)...); } }; template<class F> inline constexpr RecLambda<F> rec_lambda(F&& f) { return RecLambda<F>(std::forward<F>(f)); } template<class Head, class... Tail> struct multi_dim_vector { using type = std::vector<typename multi_dim_vector<Tail...>::type>; }; template<class T> struct multi_dim_vector<T> { using type = T; }; template<class T, class Arg> constexpr std::vector<T> make_vec(int n, Arg&& arg) { return std::vector<T>(n, std::forward<Arg>(arg)); } template<class T, class... Args> constexpr typename multi_dim_vector<Args..., T>::type make_vec(int n, Args&&... args) { return typename multi_dim_vector<Args..., T>::type (n, make_vec<T>(std::forward<Args>(args)...)); } inline CONSTEXPR int popcnt(ull x) { #if __cplusplus >= 202002L return std::popcount(x); #endif x = (x & 0x5555555555555555) + ((x >> 1 ) & 0x5555555555555555); x = (x & 0x3333333333333333) + ((x >> 2 ) & 0x3333333333333333); x = (x & 0x0f0f0f0f0f0f0f0f) + ((x >> 4 ) & 0x0f0f0f0f0f0f0f0f); x = (x & 0x00ff00ff00ff00ff) + ((x >> 8 ) & 0x00ff00ff00ff00ff); x = (x & 0x0000ffff0000ffff) + ((x >> 16) & 0x0000ffff0000ffff); return (x & 0x00000000ffffffff) + ((x >> 32) & 0x00000000ffffffff); } template<class T, class Comp = std::less<T>> class presser { protected: std::vector<T> dat; Comp cmp; bool sorted = false; public: presser() : presser(Comp()) {} presser(const Comp& cmp) : cmp(cmp) {} presser(const std::vector<T>& vec, const Comp& cmp = Comp()) : dat(vec), cmp(cmp) {} presser(std::vector<T>&& vec, const Comp& cmp = Comp()) : dat(std::move(vec)), cmp(cmp) {} presser(std::initializer_list<T> il, const Comp& cmp = Comp()) : dat(il.begin(), il.end()), cmp(cmp) {} void reserve(int n) { assert(!sorted); dat.reserve(n); } void push_back(const T& v) { assert(!sorted); dat.push_back(v); } void push_back(T&& v) { assert(!sorted); dat.push_back(std::move(v)); } void push(const std::vector<T>& vec) { assert(!sorted); const int n = dat.size(); dat.resize(n + vec.size()); rep (i, vec.size()) dat[n + i] = vec[i]; } int build() { assert(!sorted); sorted = true; std::sort(all(dat), cmp); dat.erase(std::unique(all(dat), [&](const T& a, const T& b) -> bool { return !cmp(a, b) && !cmp(b, a); }), dat.end()); return dat.size(); } const T& operator[](int k) const& { assert(sorted); assert(0 <= k && k < (int)dat.size()); return dat[k]; } T operator[](int k) && { assert(sorted); assert(0 <= k && k < (int)dat.size()); return std::move(dat[k]); } int get_index(const T& val) const { assert(sorted); return static_cast<int>(std::lower_bound(all(dat), val, cmp) - dat.begin()); } std::vector<int> pressed(const std::vector<T>& vec) const { assert(sorted); std::vector<int> res(vec.size()); rep (i, vec.size()) res[i] = get_index(vec[i]); return res; } void press(std::vector<T>& vec) const { static_assert(std::is_integral<T>::value, "template argument must be convertible from int type"); assert(sorted); each_for (i : vec) i = get_index(i); } int size() const { assert(sorted); return dat.size(); } const std::vector<T>& data() const& { return dat; } std::vector<T> data() && { return std::move(dat); } }; #line 2 "library/data-struct/cht/ConvexHullTrickAddMonotone.hpp" #line 4 "library/data-struct/cht/ConvexHullTrickAddMonotone.hpp" template<class T = ll, bool is_max = false, class LargeT = __int128_t> class ConvexHullTrickAddMonotone { protected: struct Line { T a, b; bool is_query; int idx; mutable ll nxt_a, nxt_b; mutable bool has_nxt; T get(T x) const { return a * x + b; } T get_nxt(T x) const { return nxt_a * x + nxt_b; } Line() = default; Line(T a, T b, int id, bool i = false) : a(a), b(b), idx(id), is_query(i), has_nxt(false) {} friend bool operator<(const Line& lhs, const Line& rhs) { assert(!lhs.is_query || !rhs.is_query); if (lhs.is_query) { if (!rhs.has_nxt) return true; return rhs.get(lhs.a) < rhs.get_nxt(lhs.a); } if (rhs.is_query) { if (!lhs.has_nxt) return false; return lhs.get(rhs.a) > lhs.get_nxt(rhs.a); } return lhs.a == rhs.a ? lhs.b < rhs.b : lhs.a < rhs.a; } }; int line_count = 0; std::deque<Line> que; bool is_necessary(const typename std::deque<Line>::iterator& itr) { if (itr != que.begin() && itr->a == prev(itr)->a) return itr->b < prev(itr)->b; if (itr != prev(que.end()) && itr->a == next(itr)->a) return itr->b < next(itr)->b; if (itr == que.begin() || itr == prev(que.end())) return true; return (LargeT)(itr->b - prev(itr)->b) * (next(itr)->a - itr->a) < (LargeT)(itr->b - next(itr)->b) * (prev(itr)->a - itr->a); } public: ConvexHullTrickAddMonotone() = default; int add_line(T a, T b) { if IF_CONSTEXPR (is_max) a = - a, b = - b; typename std::deque<Line>::iterator itr; if (que.empty() || que.back().a <= a) { que.emplace_back(a, b, line_count); itr = prev(que.end()); } else { assert(a <= que.front().a); que.emplace_front(a, b, line_count); itr = que.begin(); } if (!is_necessary(itr)) { que.erase(itr); return line_count++; } while (itr != que.begin() && !is_necessary(prev(itr))) { que.pop_back(); que.pop_back(); que.emplace_back(a, b, line_count); itr = prev(que.end()); } while (itr != prev(que.end()) && !is_necessary(next(itr))) { que.pop_front(); que.pop_front(); que.emplace_front(a, b, line_count); itr = que.begin(); } if (itr != que.begin()) { prev(itr)->nxt_a = itr->a; prev(itr)->nxt_b = itr->b; prev(itr)->has_nxt = true; } if (itr != prev(que.end())) { itr->nxt_a = next(itr)->a; itr->nxt_b = next(itr)->b; itr->has_nxt = true; } else itr->has_nxt = false; return line_count++; } Line get_min_line(T x) const { auto itr = lower_bound(all(que), Line{x, 0, -1, true}); Line res{*itr}; if IF_CONSTEXPR (is_max) res.a = - res.a, res.b = - res.b; return res; } T get_min(T x) const { return get_min_line(x).get(x); } Line dec_get_min_line(T x) { while (que.size() > 1 && que.begin()->get(x) > next(que.begin())->get(x)) que.pop_front(); Line res{que.front()}; if IF_CONSTEXPR (is_max) res.a = - res.a, res.b = - res.b; return res; } T dec_get_min(T x) { return dec_get_min_line(x).get(x); } Line inc_get_min_line(T x) { while (que.size() > 1 && prev(que.end())->get(x) > prev(que.end(), 2)->get(x)) que.pop_back(); Line res{que.back()}; if IF_CONSTEXPR (is_max) res.a = - res.a, res.b = - res.b; return res; } T inc_get_min(T x) { return inc_get_min_line(x).get(x); } bool empty() const { return que.empty(); } }; /** * @brief ConvexHullTrickAddMonotone * @docs docs/ConvexHullTrickAddMonotone.md */ #line 3 "main.cpp" using namespace std; int main() { ll n, k; cin >> n >> k; vector<ll> a(n); cin >> a; vector<ll> r(n + 1); rep (i, n) r[i + 1] = r[i] + a[i]; vector<vector<ll>> b(k + 2, vector<ll>(n + 1, -inf)); vector<vector<ll>> c(k + 2, vector<ll>(n + 1, -1)); b[0][0] = 0; rep (j, k + 1) { ConvexHullTrickAddMonotone<ll, true> cht; cht.add_line(0, 0); reps (i, n) { auto l = cht.inc_get_min_line(r[i]); b[j + 1][i] = l.get(r[i]); c[j + 1][i] = l.idx; cht.add_line(r[i], b[j][i] - r[i] * r[i]); } } cout << b[k + 1][n] << endl; vector<ll> ans(k); { int x = n; rrep (i, k + 2, 2) { ans[i - 2] = c[i][x]; x = c[i][x]; } } cout << ans << endl; }

컴파일 시 표준 에러 (stderr) 메시지

library/data-struct/cht/ConvexHullTrickAddMonotone.hpp: In instantiation of 'ConvexHullTrickAddMonotone<T, is_max, LargeT>::Line::Line(T, T, int, bool) [with T = long long int; bool is_max = true; LargeT = __int128]':
/usr/include/c++/10/ext/new_allocator.h:150:4:   required from 'void __gnu_cxx::new_allocator<_Tp>::construct(_Up*, _Args&& ...) [with _Up = ConvexHullTrickAddMonotone<long long int, true>::Line; _Args = {long long int&, long long int&, int&}; _Tp = ConvexHullTrickAddMonotone<long long int, true>::Line]'
/usr/include/c++/10/bits/alloc_traits.h:512:17:   required from 'static void std::allocator_traits<std::allocator<_CharT> >::construct(std::allocator_traits<std::allocator<_CharT> >::allocator_type&, _Up*, _Args&& ...) [with _Up = ConvexHullTrickAddMonotone<long long int, true>::Line; _Args = {long long int&, long long int&, int&}; _Tp = ConvexHullTrickAddMonotone<long long int, true>::Line; std::allocator_traits<std::allocator<_CharT> >::allocator_type = std::allocator<ConvexHullTrickAddMonotone<long long int, true>::Line>]'
/usr/include/c++/10/bits/deque.tcc:170:30:   required from 'std::deque<_Tp, _Alloc>::reference std::deque<_Tp, _Alloc>::emplace_back(_Args&& ...) [with _Args = {long long int&, long long int&, int&}; _Tp = ConvexHullTrickAddMonotone<long long int, true>::Line; _Alloc = std::allocator<ConvexHullTrickAddMonotone<long long int, true>::Line>; std::deque<_Tp, _Alloc>::reference = ConvexHullTrickAddMonotone<long long int, true>::Line&]'
library/data-struct/cht/ConvexHullTrickAddMonotone.hpp:45:29:   required from 'int ConvexHullTrickAddMonotone<T, is_max, LargeT>::add_line(T, T) [with T = long long int; bool is_max = true; LargeT = __int128]'
main.cpp:16:26:   required from here
library/data-struct/cht/ConvexHullTrickAddMonotone.hpp:10:13: warning: 'ConvexHullTrickAddMonotone<long long int, true>::Line::idx' will be initialized after [-Wreorder]
library/data-struct/cht/ConvexHullTrickAddMonotone.hpp:9:14: warning:   'bool ConvexHullTrickAddMonotone<long long int, true>::Line::is_query' [-Wreorder]
library/data-struct/cht/ConvexHullTrickAddMonotone.hpp:16:9: warning:   when initialized here [-Wreorder]
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...