結果
問題 | No.1294 マウンテン数列 |
ユーザー |
![]() |
提出日時 | 2020-11-21 00:27:17 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 718 ms / 2,000 ms |
コード長 | 17,785 bytes |
コンパイル時間 | 959 ms |
コンパイル使用メモリ | 83,656 KB |
最終ジャッジ日時 | 2025-01-16 03:39:58 |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 17 |
ソースコード
#line 1 "main.cpp"/*** @title Template*/#include <iostream>#include <algorithm>#include <utility>#include <numeric>#include <vector>#include <array>#include <cassert>#line 2 "/Users/kodamankod/Desktop/cpp_programming/Library/other/chmin_chmax.cpp"template <class T, class U>constexpr bool chmin(T &lhs, const U &rhs) {if (lhs > rhs) { lhs = rhs; return true; }return false;}template <class T, class U>constexpr bool chmax(T &lhs, const U &rhs) {if (lhs < rhs) { lhs = rhs; return true; }return false;}/*** @title Chmin/Chmax*/#line 2 "/Users/kodamankod/Desktop/cpp_programming/Library/other/range.cpp"#line 4 "/Users/kodamankod/Desktop/cpp_programming/Library/other/range.cpp"class range {struct iter {std::size_t itr;constexpr iter(std::size_t pos) noexcept: itr(pos) { }constexpr void operator ++ () noexcept { ++itr; }constexpr bool operator != (iter other) const noexcept { return itr != other.itr; }constexpr std::size_t operator * () const noexcept { return itr; }};struct reviter {std::size_t itr;constexpr reviter(std::size_t pos) noexcept: itr(pos) { }constexpr void operator ++ () noexcept { --itr; }constexpr bool operator != (reviter other) const noexcept { return itr != other.itr; }constexpr std::size_t operator * () const noexcept { return itr; }};const iter first, last;public:constexpr range(std::size_t first, std::size_t last) noexcept: first(first), last(std::max(first, last)) { }constexpr iter begin() const noexcept { return first; }constexpr iter end() const noexcept { return last; }constexpr reviter rbegin() const noexcept { return reviter(*last - 1); }constexpr reviter rend() const noexcept { return reviter(*first - 1); }};/*** @title Range*/#line 2 "/Users/kodamankod/Desktop/cpp_programming/Library/container/segment_tree.cpp"#line 2 "/Users/kodamankod/Desktop/cpp_programming/Library/other/monoid.cpp"#include <type_traits>#line 5 "/Users/kodamankod/Desktop/cpp_programming/Library/other/monoid.cpp"#include <stdexcept>template <class T, class = void>class has_identity: public std::false_type { };template <class T>class has_identity<T, typename std::conditional<false, decltype(T::identity()), void>::type>: public std::true_type { };template <class T>constexpr typename std::enable_if<has_identity<T>::value, typename T::type>::type empty_exception() {return T::identity();}template <class T>[[noreturn]] typename std::enable_if<!has_identity<T>::value, typename T::type>::type empty_exception() {throw std::runtime_error("type T has no identity");}template <class T, bool HasIdentity>class fixed_monoid_impl: public T {public:using type = typename T::type;static constexpr type convert(const type &value) { return value; }static constexpr type revert(const type &value) { return value; }template <class Mapping, class Value, class... Args>static constexpr void operate(Mapping &&func, Value &value, const type &op, Args&&... args) {value = func(value, op, std::forward<Args>(args)...);}template <class Constraint>static constexpr bool satisfies(Constraint &&func, const type &value) {return func(value);}};template <class T>class fixed_monoid_impl<T, false> {public:class type {public:typename T::type value;bool state;explicit constexpr type(): value(typename T::type { }), state(false) { }explicit constexpr type(const typename T::type &value): value(value), state(true) { }};static constexpr type convert(const typename T::type &value) { return type(value); }static constexpr typename T::type revert(const type &value) {if (!value.state) throw std::runtime_error("attempted to revert identity to non-monoid");return value.value;}static constexpr type identity() { return type(); }static constexpr type operation(const type &v1, const type &v2) {if (!v1.state) return v2;if (!v2.state) return v1;return type(T::operation(v1.value, v2.value));}template <class Mapping, class Value, class... Args>static constexpr void operate(Mapping &&func, Value &value, const type &op, Args&&... args) {if (!op.state) return;value = func(value, op.value, std::forward<Args>(args)...);}template <class Constraint>static constexpr bool satisfies(Constraint &&func, const type &value) {if (!value.state) return false;return func(value.value);}};template <class T>using fixed_monoid = fixed_monoid_impl<T, has_identity<T>::value>;/*** @title Monoid Utility*/#line 2 "/Users/kodamankod/Desktop/cpp_programming/Library/other/bit_operation.cpp"#include <cstddef>#include <cstdint>constexpr size_t bit_ppc(const uint64_t x) { return __builtin_popcountll(x); }constexpr size_t bit_ctzr(const uint64_t x) { return x == 0 ? 64 : __builtin_ctzll(x); }constexpr size_t bit_ctzl(const uint64_t x) { return x == 0 ? 64 : __builtin_clzll(x); }constexpr size_t bit_width(const uint64_t x) { return 64 - bit_ctzl(x); }constexpr uint64_t bit_msb(const uint64_t x) { return x == 0 ? 0 : uint64_t(1) << (bit_width(x) - 1); }constexpr uint64_t bit_lsb(const uint64_t x) { return x & (-x); }constexpr uint64_t bit_cover(const uint64_t x) { return x == 0 ? 0 : bit_msb(2 * x - 1); }constexpr uint64_t bit_rev(uint64_t x) {x = ((x >> 1) & 0x5555555555555555) | ((x & 0x5555555555555555) << 1);x = ((x >> 2) & 0x3333333333333333) | ((x & 0x3333333333333333) << 2);x = ((x >> 4) & 0x0F0F0F0F0F0F0F0F) | ((x & 0x0F0F0F0F0F0F0F0F) << 4);x = ((x >> 8) & 0x00FF00FF00FF00FF) | ((x & 0x00FF00FF00FF00FF) << 8);x = ((x >> 16) & 0x0000FFFF0000FFFF) | ((x & 0x0000FFFF0000FFFF) << 16);x = (x >> 32) | (x << 32);return x;}/*** @title Bit Operations*/#line 5 "/Users/kodamankod/Desktop/cpp_programming/Library/container/segment_tree.cpp"#line 8 "/Users/kodamankod/Desktop/cpp_programming/Library/container/segment_tree.cpp"#include <iterator>#line 11 "/Users/kodamankod/Desktop/cpp_programming/Library/container/segment_tree.cpp"#include <type_traits>#line 13 "/Users/kodamankod/Desktop/cpp_programming/Library/container/segment_tree.cpp"template <class Monoid>class segment_tree {public:using structure = Monoid;using value_monoid = typename Monoid::value_structure;using value_type = typename Monoid::value_structure::type;using size_type = size_t;private:using fixed_value_monoid = fixed_monoid<value_monoid>;using fixed_value_type = typename fixed_value_monoid::type;std::vector<fixed_value_type> M_tree;void M_fix_change(const size_type index) {M_tree[index] = fixed_value_monoid::operation(M_tree[index << 1 | 0], M_tree[index << 1 | 1]);}public:segment_tree() = default;explicit segment_tree(const size_type size) { initialize(size); }template <class InputIterator>explicit segment_tree(InputIterator first, InputIterator last) { construct(first, last); }void initialize(const size_type size) {clear();M_tree.assign(size << 1, fixed_value_monoid::identity());}template <class InputIterator>void construct(InputIterator first, InputIterator last) {clear();const size_type size = std::distance(first, last);M_tree.reserve(size << 1);M_tree.assign(size, fixed_value_monoid::identity());std::transform(first, last, std::back_inserter(M_tree), [&](const value_type &value) {return fixed_value_monoid::convert(value);});for (size_type index = size - 1; index != 0; --index) {M_fix_change(index);}}void assign(size_type index, const value_type &value) {assert(index < size());index += size();M_tree[index] = fixed_value_monoid::convert(value);while (index != 1) {index >>= 1;M_fix_change(index);}}value_type at(const size_type index) const {assert(index < size());return fixed_value_monoid::revert(M_tree[index + size()]);}value_type fold(size_type first, size_type last) const {assert(first <= last);assert(last <= size());first += size();last += size();fixed_value_type fold_l = fixed_value_monoid::identity();fixed_value_type fold_r = fixed_value_monoid::identity();while (first != last) {if (first & 1) {fold_l = fixed_value_monoid::operation(fold_l, M_tree[first]);++first;}if (last & 1) {--last;fold_r = fixed_value_monoid::operation(M_tree[last], fold_r);}first >>= 1;last >>= 1;}return fixed_value_monoid::revert(fixed_value_monoid::operation(fold_l, fold_r));}template <bool ToRight = true, class Constraint, std::enable_if_t<ToRight>* = nullptr>size_type satisfies(const size_type left, Constraint &&func) const {assert(left <= size());if (fixed_value_monoid::satisfies(std::forward<Constraint>(func),fixed_value_monoid::identity())) return left;size_type first = left + size();size_type last = 2 * size();const size_type last_c = last;fixed_value_type fold = fixed_value_monoid::identity();const auto try_merge = [&](const size_type index) {fixed_value_type tmp = fixed_value_monoid::operation(fold, M_tree[index]);if (fixed_value_monoid::satisfies(std::forward<Constraint>(func), tmp)) return true;fold = std::move(tmp);return false;};const auto subtree = [&](size_type index) {while (index < size()) {index <<= 1;if (!try_merge(index)) ++index;}return index - size() + 1;};size_type story = 0;while (first < last) {if (first & 1) {if (try_merge(first)) return subtree(first);++first;}first >>= 1;last >>= 1;++story;}while (story--) {last = last_c >> story;if (last & 1) {--last;if (try_merge(last)) return subtree(last);}}return size() + 1;}template <bool ToRight = true, class Constraint, std::enable_if_t<!ToRight>* = nullptr>size_type satisfies(const size_type right, Constraint &&func) const {assert(right <= size());if (fixed_value_monoid::satisfies(std::forward<Constraint>(func),fixed_value_monoid::identity())) return right;size_type first = size();size_type last = right + size();const size_type first_c = first;fixed_value_type fold = fixed_value_monoid::identity();const auto try_merge = [&](const size_type index) {fixed_value_type tmp = fixed_value_monoid::operation(M_tree[index], fold);if (fixed_value_monoid::satisfies(std::forward<Constraint>(func), tmp)) return true;fold = std::move(tmp);return false;};const auto subtree = [&](size_type index) {while (index < size()) {index <<= 1;if (try_merge(index + 1)) ++index;}return index - size();};size_type story = 0;while (first < last) {if (first & 1) ++first;if (last & 1) {--last;if (try_merge(last)) return subtree(last);}first >>= 1;last >>= 1;++story;}const size_type cover = bit_cover(first_c);while (story--) {first = (cover >> story) - ((cover - first_c) >> story);if (first & 1) {if (try_merge(first)) return subtree(first);}}return size_type(-1);}void clear() {M_tree.clear();M_tree.shrink_to_fit();}size_type size() const {return M_tree.size() >> 1;}};/*** @title Segment Tree*/#line 2 "/Users/kodamankod/Desktop/cpp_programming/Library/algebraic/modular.cpp"#line 2 "/Users/kodamankod/Desktop/cpp_programming/Library/algebraic/mod_inv.cpp"#line 5 "/Users/kodamankod/Desktop/cpp_programming/Library/algebraic/mod_inv.cpp"constexpr std::pair<int64_t, int64_t> mod_inv(int64_t a, int64_t b) {if ((a %= b) == 0) return { b, 0 };int64_t s = b, t = (a < 0 ? a + b : a);int64_t m0 = 0, m1 = 1, tmp = 0;while (t > 0) {const auto u = s / t;s -= t * u; m0 -= m1 * u;tmp = s; s = t; t = tmp; tmp = m0; m0 = m1; m1 = tmp;}return { s, (m0 < 0 ? m0 + b / s : m0) };}/*** @title Extended GCD*/#line 4 "/Users/kodamankod/Desktop/cpp_programming/Library/algebraic/modular.cpp"#line 8 "/Users/kodamankod/Desktop/cpp_programming/Library/algebraic/modular.cpp"#include <type_traits>template <class Modulus>class modular {public:using value_type = uint32_t;using cover_type = uint64_t;static constexpr uint32_t mod() { return Modulus::mod(); }template <class T>static constexpr value_type normalize(T value_) noexcept {if (value_ < 0) {value_ = -value_;value_ %= mod();if (value_ == 0) return 0;return mod() - value_;}return value_ % mod();}private:value_type value;template <bool IsPrime, std::enable_if_t<IsPrime>* = nullptr>constexpr modular inverse_helper() const noexcept { return power(*this, mod() - 2); }template <bool IsPrime, std::enable_if_t<!IsPrime>* = nullptr>constexpr modular inverse_helper() const noexcept {const auto tmp = mod_inv(value, mod());assert(tmp.first == 1);return modular(tmp.second);}public:constexpr modular() noexcept : value(0) { }template <class T>explicit constexpr modular(T value_) noexcept : value(normalize(value_)) { }template <class T>explicit constexpr operator T() const noexcept { return static_cast<T>(value); }constexpr value_type get() const noexcept { return value; }constexpr value_type &extract() noexcept { return value; }constexpr modular operator - () const noexcept { return modular(mod() - value); }constexpr modular operator ~ () const noexcept { return inverse(*this); }constexpr modular operator + (const modular &rhs) const noexcept { return modular(*this) += rhs; }constexpr modular& operator += (const modular &rhs) noexcept {if ((value += rhs.value) >= mod()) value -= mod();return *this;}constexpr modular operator - (const modular &rhs) const noexcept { return modular(*this) -= rhs; }constexpr modular& operator -= (const modular &rhs) noexcept {if ((value += mod() - rhs.value) >= mod()) value -= mod();return *this;}constexpr modular operator * (const modular &rhs) const noexcept { return modular(*this) *= rhs; }constexpr modular& operator *= (const modular &rhs) noexcept {value = (cover_type) value * rhs.value % mod();return *this;}constexpr modular operator / (const modular &rhs) const noexcept { return modular(*this) /= rhs; }constexpr modular& operator /= (const modular &rhs) noexcept { return (*this) *= inverse(rhs); }constexpr bool zero() const noexcept { return value == 0; }constexpr bool operator == (const modular &rhs) const noexcept { return value == rhs.value; }constexpr bool operator != (const modular &rhs) const noexcept { return value != rhs.value; }friend std::ostream& operator << (std::ostream &stream, const modular &rhs) { return stream << rhs.value; }friend constexpr modular inverse(const modular &val) noexcept { return val.inverse_helper<Modulus::is_prime>(); }friend constexpr modular power(modular val, cover_type exp) noexcept {modular res(1);for (; exp > 0; exp >>= 1, val *= val) if (exp & 1) res *= val;return res;}};template <uint32_t Mod, bool IsPrime = true>struct static_modulus {static constexpr uint32_t mod() noexcept { return Mod; }static constexpr bool is_prime = IsPrime;};template <uint32_t Id = 0, bool IsPrime = false>struct dynamic_modulus {static uint32_t &mod() noexcept { static uint32_t val = 0; return val; }static constexpr bool is_prime = IsPrime;};template <uint32_t Mod, bool IsPrime = true>using mint32_t = modular<static_modulus<Mod, IsPrime>>;using rmint32_t = modular<dynamic_modulus<>>;/** @title Modint*/#line 18 "main.cpp"using i32 = std::int32_t;using i64 = std::int64_t;using u32 = std::uint32_t;using u64 = std::uint64_t;using isize = std::ptrdiff_t;using usize = std::size_t;constexpr i32 inf32 = (i32(1) << 30) - 1;constexpr i64 inf64 = (i64(1) << 62) - 1;using Fp = mint32_t<998244353>;struct st_monoid {struct value_structure {using type = Fp;static type identity() { return Fp(0); }static type operation(const type& v1, const type& v2) {return v1 + v2;}};};int main() {usize N;std::cin >> N;std::vector<usize> A(N);for (auto &x: A) {std::cin >> x;}std::reverse(A.begin(), A.end());usize mx = 0;for (auto i: range(1, N)) {chmax(mx, A[i - 1] - A[i]);}std::vector<Fp> ans(2500);for (auto max_dif: range(mx, 2500)) {// std::vector<std::vector<Fp>> dp(N, std::vector<Fp>(N));// dp[0][0] = Fp(1);// for (auto i: range(0, N - 1)) {// for (auto j: range(0, i + 1)) {// dp[i + 1][j] += dp[i][j];// if (A[j] - A[i + 1] <= max_dif) {// dp[i + 1][i] += dp[i][j];// }// }// }// for (auto i: range(0, N)) {// ans[max_dif] += dp[N - 1][i];// }std::vector<usize> left(N - 1);{usize idx = 0;for (auto i: range(0, N - 1)) {while (A[idx] - A[i + 1] > max_dif) {idx += 1;}left[i] = idx;}}segment_tree<st_monoid> seg(N);seg.assign(0, Fp(2));for (auto i: range(0, N - 1)) {seg.assign(i, seg.fold(left[i], i + 1));}ans[max_dif] = seg.fold(0, N);}Fp sum;for (auto i: range(1, 2500)) {sum += Fp(i) * (ans[i] - ans[i - 1]);}std::cout << sum << '\n';return 0;}