結果

問題 No.776 A Simple RMQ Problem
ユーザー りあんりあん
提出日時 2018-12-07 17:05:18
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 272 ms / 3,000 ms
コード長 3,180 bytes
コンパイル時間 3,581 ms
コンパイル使用メモリ 204,464 KB
実行使用メモリ 11,492 KB
最終ジャッジ日時 2023-10-14 22:20:14
合計ジャッジ時間 12,659 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,352 KB
testcase_01 AC 2 ms
4,352 KB
testcase_02 AC 22 ms
5,100 KB
testcase_03 AC 83 ms
11,280 KB
testcase_04 AC 107 ms
5,172 KB
testcase_05 AC 182 ms
11,220 KB
testcase_06 AC 52 ms
7,252 KB
testcase_07 AC 112 ms
11,240 KB
testcase_08 AC 143 ms
7,212 KB
testcase_09 AC 46 ms
11,488 KB
testcase_10 AC 80 ms
7,260 KB
testcase_11 AC 145 ms
11,228 KB
testcase_12 AC 188 ms
11,332 KB
testcase_13 AC 187 ms
11,232 KB
testcase_14 AC 190 ms
11,272 KB
testcase_15 AC 186 ms
11,272 KB
testcase_16 AC 188 ms
11,224 KB
testcase_17 AC 272 ms
11,492 KB
testcase_18 AC 124 ms
11,236 KB
testcase_19 AC 231 ms
11,228 KB
testcase_20 AC 231 ms
11,268 KB
testcase_21 AC 231 ms
11,344 KB
testcase_22 AC 228 ms
11,348 KB
testcase_23 AC 228 ms
11,368 KB
testcase_24 AC 227 ms
11,204 KB
testcase_25 AC 32 ms
4,352 KB
testcase_26 AC 111 ms
11,216 KB
testcase_27 AC 94 ms
11,228 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;

const long long LM = 1e18;

struct node {
    long long lmax, rmax, inmax, sum;
    node(long long lmax, long long rmax, long long inmax, long long sum) : lmax(lmax), rmax(rmax), inmax(inmax), sum(sum) {}
    node(long long x) : node(x, x, x, x) {}
};

const node ex(-LM, -LM, -LM, 0);
const node compose(const node& l, const node& r) {
    return node(max(l.lmax, l.sum + r.lmax),
                max(r.rmax, l.rmax + r.sum),
                max({ l.inmax, r.inmax, l.rmax + r.lmax }),
                l.sum + r.sum);
}

class segtree {
private:
    int n, s, t;
    vector<node> tr;

    const node q(int k, int l, int r) {
        return r <= s || t <= l ? ex : s <= l && r <= t ? tr[k]
                : compose(q(k << 1 | 1, l, (l + r) >> 1), q((k + 1) << 1, (l + r) >> 1, r));
    }
public:
    segtree() {}
    segtree(int m) {
        n = 1;
        while (n < m) n <<= 1;
        tr = vector<node>((n << 1) - 1, ex);
    }
    void assign(int j, const node& x) {
        tr[j + n - 1] = x;
    }
    void update(int j, const node& x) {
        assign(j, x);
        update(j);
    }
    void update(int j) {
        int i = j + n - 1;
        while (i > 0) {
            i = (i - 1) >> 1;
            tr[i] = compose(tr[i << 1 | 1], tr[(i + 1) << 1]);
        }
    }
    void all_update() {
        for (int i = n - 2; i >= 0; i--)
            tr[i] = compose(tr[i << 1 | 1], tr[i + 1 << 1]);
    }

    // [s, t)
    const node run(int _s, int _t) {
        s = _s;
        t = _t;
        return q(0, 0, n);
    }
};


segtree sg;

long long calc(int l, int r) {
    return sg.run(l, r).inmax;
}

// l1 < l2 <= r1 < r2
long long calc(int l1, int l2, int r1, int r2) {
    return sg.run(l1, l2).rmax + sg.run(r1, r2).lmax + (l2 == r1 ? 0 : sg.run(l2, r1).sum);
}


int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n, q;
    cin >> n >> q;
    assert(1 <= n && n <= 100000);
    assert(1 <= q && q <= 100000);
    sg = segtree(n);
    for (int i = 0; i < n; ++i) {
        long long a;
        cin >> a;
        assert(-1000000000 <= a && a <= 1000000000);
        sg.assign(i, node(a));
    }
    sg.all_update();
    for (int _ = 0; _ < q; ++_) {
        string s;
        cin >> s;
        if (s == "set") {
            int i;
            long long x;
            cin >> i >> x;
            assert(1 <= i && i <= n);
            assert(-1000000000 <= x && x <= 1000000000);
            --i;
            sg.update(i, node(x));
        }
        else if (s == "max")  {
            int l1, l2, r1, r2;
            cin >> l1 >> l2 >> r1 >> r2;
            assert(1 <= l1 && l1 <= n);
            assert(1 <= l2 && l2 <= n);
            assert(1 <= r1 && r1 <= n);
            assert(1 <= r2 && r2 <= n);
            --l1;
            --r1;
            assert(l1 < l2 && r1 < r2 && l1 < r2);
            l2 = min(l2, r2);
            r1 = max(l1, r1);
            if (l2 <= r1)
                cout << calc(l1, l2, r1, r2) << "\n";
            else
                cout << max({ calc(r1, l2), calc(l1, l2, l2, r2), calc(l1, r1, r1, r2) }) << "\n";
        }
        else assert(0);
    }
    return 0;
}
0