結果

問題 No.776 A Simple RMQ Problem
ユーザー PachicobuePachicobue
提出日時 2018-12-23 01:21:27
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 6,025 bytes
コンパイル時間 2,785 ms
コンパイル使用メモリ 215,172 KB
実行使用メモリ 13,952 KB
最終ジャッジ日時 2024-09-25 10:22:18
合計ジャッジ時間 8,907 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 AC 32 ms
6,940 KB
testcase_26 WA -
testcase_27 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define show(x) std::cerr << #x << " = " << (x) << std::endl
using ll = long long;
using ull = unsigned long long;
using ld = long double;
constexpr ll MOD = 1000000007LL;
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 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 get(const std::size_t a) const { return accumulate(a, a + 1); }
    void set(std::size_t a, const T& val)
    {
        modify(a, a + 1, OpMonoid::id()), value[a += half] = val;
        while (a >>= 1) { up(a); }
    }
    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);
    }
    std::vector<T> data() const
    {
        std::vector<T> ans(data_num);
        for (std::size_t i = 0; i < data_num; i++) { ans[i] = get(i); }
        return ans;
    }

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{};
};
template <typename T>
std::ostream& operator<<(std::ostream& os, const LazySegmentTree<T>& seg)
{
    os << "[";
    for (const auto& e : seg.data()) { os << e << ","; }
    return (os << "]" << std::endl);
}
struct Min_Plus
{
    using T = ll;
    struct ValMonoid
    {
        T operator()(const T& a, const T& b) const { return std::min(a, b); }
        static constexpr T id() { return INF<T>; }
    };
    struct OpMonoid
    {
        using T = ll;
        T operator()(const T& f1, const T& f2) const { return f1 + f2; }
        static constexpr T id() { return 0; }
    };
    T operator()(const OpMonoid::T& f, const T& x) const { return f + x; }
};
int main()
{
    std::cin.tie(0);
    std::ios::sync_with_stdio(false);
    int N, Q;
    std::cin >> N >> Q;
    std::vector<ll> a(N + 2, 0);
    for (int i = 1; i <= N; i++) { std::cin >> a[i]; }
    auto l = a, r = a;
    for (int i = 1; i <= N; i++) { l[i] += l[i - 1]; }
    for (int i = N; i >= 0; i--) { r[i] += r[i + 1]; }
    LazySegmentTree<Min_Plus> lseg(l.begin(), l.end());
    LazySegmentTree<Min_Plus> rseg(r.begin(), r.end());
    for (int q = 0; q < Q; q++) {
        std::string s;
        std::cin >> s;
        if (s == "set") {
            int i, x;
            std::cin >> i >> x;
            lseg.modify(i, N + 1, x - a[i]), rseg.modify(0, i + 1, x - a[i]), 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 - 1 < r1 + 1) {
                const ll lm = lseg.accumulate(l1 - 1, l2);
                const ll rm = rseg.accumulate(r1 + 1, r2 + 2);
                std::cout << lseg.get(N) - lm - rm << "\n";
            } else {
                const ll S = lseg.get(N);
                ll max = -INF<ll>;
                const ll m1 = lseg.accumulate(l1 - 1, r1 + 1) + rseg.accumulate(r1 + 1, r2 + 2);
                const ll m2 = lseg.accumulate(l1 - 1, l2) + rseg.accumulate(l2, r2 + 2);
                const ll m3 = lseg.accumulate(r1 + 1, l2) + rseg.accumulate(r1 + 1, l2);
                //                show(m1), show(m2), show(m3);
                max = std::max({max, S - m1, S - m2, m3 + S, 0LL});
                std::cout << max << "\n";
            }
        }
        //        show(lseg), show(rseg);
    }
    return 0;
}
0