結果
問題 | No.776 A Simple RMQ Problem |
ユーザー | Pachicobue |
提出日時 | 2018-12-23 03:15:29 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 265 ms / 3,000 ms |
コード長 | 7,653 bytes |
コンパイル時間 | 2,461 ms |
コンパイル使用メモリ | 217,636 KB |
実行使用メモリ | 23,116 KB |
最終ジャッジ日時 | 2024-09-25 10:26:09 |
合計ジャッジ時間 | 8,746 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,248 KB |
testcase_02 | AC | 25 ms
8,192 KB |
testcase_03 | AC | 98 ms
21,632 KB |
testcase_04 | AC | 115 ms
8,448 KB |
testcase_05 | AC | 198 ms
22,016 KB |
testcase_06 | AC | 56 ms
12,288 KB |
testcase_07 | AC | 133 ms
22,272 KB |
testcase_08 | AC | 150 ms
12,544 KB |
testcase_09 | AC | 53 ms
22,612 KB |
testcase_10 | AC | 86 ms
12,764 KB |
testcase_11 | AC | 164 ms
22,784 KB |
testcase_12 | AC | 208 ms
23,032 KB |
testcase_13 | AC | 206 ms
23,040 KB |
testcase_14 | AC | 214 ms
23,112 KB |
testcase_15 | AC | 206 ms
22,912 KB |
testcase_16 | AC | 215 ms
23,004 KB |
testcase_17 | AC | 265 ms
23,040 KB |
testcase_18 | AC | 175 ms
22,912 KB |
testcase_19 | AC | 228 ms
23,040 KB |
testcase_20 | AC | 232 ms
23,116 KB |
testcase_21 | AC | 234 ms
23,040 KB |
testcase_22 | AC | 233 ms
23,040 KB |
testcase_23 | AC | 234 ms
23,040 KB |
testcase_24 | AC | 236 ms
23,040 KB |
testcase_25 | AC | 32 ms
5,376 KB |
testcase_26 | AC | 164 ms
23,012 KB |
testcase_27 | AC | 155 ms
23,016 KB |
ソースコード
#include <bits/stdc++.h> #define show(x) std::cerr << #x << " = " << (x) << std::endl using ll = long long; using ull = unsigned long long; template <typename T> constexpr T INF = std::numeric_limits<T>::max() / 10; std::mt19937 mt{std::random_device{}()}; constexpr std::size_t PC(ull v) { return v = (v & 0x5555555555555555ULL) + (v >> 1 & 0x5555555555555555ULL), v = (v & 0x3333333333333333ULL) + (v >> 2 & 0x3333333333333333ULL), v = (v + (v >> 4)) & 0x0F0F0F0F0F0F0F0FULL, static_cast<std::size_t>(v * 0x0101010101010101ULL >> 56 & 0x7f); } constexpr std::size_t LG(ull v) { return v == 0 ? 0 : (v--, v |= (v >> 1), v |= (v >> 2), v |= (v >> 4), v |= (v >> 8), v |= (v >> 16), v |= (v >> 32), PC(v)); } constexpr ull SZ(const ull v) { return 1ULL << LG(v); } template <typename Monoid> class SegmentTree { public: using BaseMonoid = Monoid; using T = typename Monoid::T; SegmentTree(const std::size_t n) : data_num(n), half(SZ(n)), value(half << 1, Monoid::id()) {} template <typename InIt> SegmentTree(const InIt first, const InIt last) : data_num(distance(first, last)), half(SZ(data_num)), value(half << 1, Monoid::id()) { std::copy(first, last, value.begin() + half); for (std::size_t i = half - 1; i >= 1; i--) { up(i); } } void set(std::size_t a, const T& val) { value[a += half] = val; while (a >>= 1) { up(a); } } T accumulate(std::size_t L, std::size_t R) const { T accl = Monoid::id(), accr = Monoid::id(); for (L += half, R += half; L < R; L >>= 1, R >>= 1) { if (L & 1) { accl = acc(accl, value[L++]); } if (R & 1) { accr = acc(value[--R], accr); } } return acc(accl, accr); } private: void up(const std::size_t i) { value[i] = acc(value[i << 1], value[i << 1 | 1]); } const std::size_t data_num, half; std::vector<T> value; const Monoid acc{}; }; template <typename Base> class LazySegmentTree { public: using BaseAlgebra = Base; using ValMonoid = typename BaseAlgebra::ValMonoid; using OpMonoid = typename BaseAlgebra::OpMonoid; using T = typename BaseAlgebra::T; using F = typename BaseAlgebra::OpMonoid::T; LazySegmentTree(const std::size_t n) : data_num(n), half(SZ(n)), value(half << 1, ValMonoid::id()), action(half << 1, OpMonoid::id()) {} template <typename InIt> LazySegmentTree(const InIt first, const InIt last) : data_num(distance(first, last)), half(SZ(data_num)), value(half << 1, ValMonoid::id()), action(half << 1, OpMonoid::id()) { copy(first, last, value.begin() + half); for (std::size_t i = half - 1; i >= 1; i--) { up(i); } } T accumulate(const std::size_t L, const std::size_t R) const { auto arec = [&](auto&& self, const std::size_t index, const std::size_t left, const std::size_t right) -> T { if (L <= left and right <= R) { return value[index]; } else if (right <= L or R <= left) { return ValMonoid::id(); } else { return act(action[index], acc(self(self, index << 1, left, (left + right) >> 1), self(self, index << 1 | 1, (left + right) >> 1, right))); } }; return arec(arec, 1, 0, half); } void modify(const std::size_t L, const std::size_t R, const F& f) { auto mrec = [&](auto&& self, const std::size_t index, const std::size_t left, const std::size_t right) -> void { if (L <= left and right <= R) { this->update(index, f); } else if (right <= L or R <= left) { } else { this->update(index << 1, action[index]), this->update(index << 1 | 1, action[index]); self(self, index << 1, left, (left + right) >> 1), self(self, index << 1 | 1, (left + right) >> 1, right); this->up(index), action[index] = OpMonoid::id(); } }; mrec(mrec, 1, 0, half); } private: void up(const std::size_t i) { value[i] = acc(value[i << 1], value[i << 1 | 1]); } void update(const std::size_t i, const F& f) { value[i] = act(f, value[i]), action[i] = compose(f, action[i]); } const std::size_t data_num, half; std::vector<T> value; std::vector<F> action; const ValMonoid acc{}; const OpMonoid compose{}; const BaseAlgebra act{}; }; struct PreSuf { struct T { ll prefix, suffix, sum, sub; }; T operator()(const T& a, const T& b) const { return T{ std::max(a.prefix, a.sum + b.prefix), std::max(b.suffix, a.suffix + b.sum), a.sum + b.sum, std::max({a.prefix, a.sum + b.prefix, b.suffix, a.suffix + b.sum, a.sub, b.sub, a.suffix + b.prefix}), }; } static T id() { return T{-INF<ll>, -INF<ll>, 0, -INF<ll>}; } }; std::ostream& operator<<(std::ostream& os, const PreSuf::T& t) { return (os << "[ pre=" << t.prefix << "; suf=" << t.suffix << "; sum=" << t.sum << "; sub=" << t.sub << " ]"); }; struct MinMax_Plus { struct T { ll min, max; }; struct ValMonoid { T operator()(const T& a, const T& b) const { return T{std::min(a.min, b.min), std::max(a.max, b.max)}; } static T id() { return T{INF<ll>, -INF<ll>}; } }; struct OpMonoid { using T = ll; T operator()(const T& f1, const T& f2) const { return f1 + f2; } static constexpr T id() { return 0LL; } }; T operator()(const OpMonoid::T& f, const T& x) const { return T{x.min + f, x.max + f}; } }; std::ostream& operator<<(std::ostream& os, const MinMax_Plus::T& t) { return (os << "< min=" << t.min << "; max=" << t.max << " >"); }; template <typename T, typename A> std::ostream& operator<<(std::ostream& os, const std::vector<T, A>& v) { os << "["; for (const auto& p : v) { os << p << ","; } return (os << "]\n"); } int main() { std::cin.tie(0); std::ios::sync_with_stdio(false); int N, Q; std::cin >> N >> Q; std::vector<ll> a(N + 1, 0); std::vector<MinMax_Plus::T> v1(N + 1, {0, 0}); std::vector<PreSuf::T> v2(N + 1, PreSuf::id()); for (int i = 1; i <= N; i++) { std::cin >> a[i], v1[i] = {v1[i - 1].min + a[i], v1[i - 1].max + a[i]}, v2[i] = {a[i], a[i], a[i], a[i]}; } //show(v1), show(v2); LazySegmentTree<MinMax_Plus> seg1(v1.begin(), v1.end()); SegmentTree<PreSuf> seg2(v2.begin(), v2.end()); for (int q = 0; q < Q; q++) { std::string s; std::cin >> s; if (s == "set") { int i, x; std::cin >> i >> x; seg1.modify(i, N + 1, x - a[i]), seg2.set(i, PreSuf::T{x, x, x, x}), a[i] = x; } else { int l1, l2, r1, r2; std::cin >> l1 >> l2 >> r1 >> r2, r1 = std::max(l1, r1), l2 = std::min(l2, r2); if (l2 < r1) { const auto m = seg1.accumulate(l1 - 1, l2); const auto M = seg1.accumulate(r1, r2 + 1); // show(m), show(M); std::cout << M.max - m.min << "\n"; } else { const auto m1 = seg1.accumulate(l1 - 1, r1 - 1); const auto M1 = seg1.accumulate(r1, r2 + 1); const auto m2 = seg1.accumulate(l1 - 1, l2); const auto M2 = seg1.accumulate(l2 + 1, r2 + 1); const auto mid = seg2.accumulate(r1, l2 + 1); // show(m1), show(M1); // show(m2), show(M2); // show(mid); std::cout << std::max({M1.max - m1.min, M2.max - m2.min, mid.sub}) << "\n"; } } } return 0; }