結果

問題 No.776 A Simple RMQ Problem
ユーザー parukiparuki
提出日時 2019-01-24 21:34:34
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 5,594 bytes
コンパイル時間 2,095 ms
コンパイル使用メモリ 182,708 KB
実行使用メモリ 17,708 KB
最終ジャッジ日時 2023-10-14 09:45:11
合計ジャッジ時間 12,240 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,352 KB
testcase_01 AC 1 ms
4,348 KB
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 51 ms
4,352 KB
testcase_26 WA -
testcase_27 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include "bits/stdc++.h"
using namespace std;
#define FOR(i,j,k) for(int (i)=(j);(i)<(int)(k);++(i))
#define rep(i,j) FOR(i,0,j)
#define each(x,y) for(auto &(x):(y))
#define mp make_pair
#define MT make_tuple
#define all(x) (x).begin(),(x).end()
#define debug(x) cout<<#x<<": "<<(x)<<endl
#define smax(x,y) (x)=max((x),(y))
#define smin(x,y) (x)=min((x),(y))
#define MEM(x,y) memset((x),(y),sizeof (x))
#define sz(x) (int)(x).size()
#define RT return
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
using vll = vector<ll>;

template<class P, class Q>
struct LazySegTree {
    int dataSize;
    vector<P> value;
    vector<Q> lazy;
    function<P(P, P)> f;
    function<P(P, Q)> g;
    function<Q(Q, Q)> h;

    LazySegTree(vector<P> dat,
        function<P(P, P)> f_, function<P(P, Q)> g_, function<Q(Q, Q)> h_){
        f = f_;
        g = g_;
        h = h_;
        dataSize = 1;
        int n = (int)dat.size();
        while (dataSize < n)dataSize *= 2;
        int treeSize = 2 * dataSize;
        value = vector<P>(treeSize, P());
        lazy = vector<Q>(treeSize, Q());
        for (int i = 0; i < n; ++i) {
            value[dataSize + i] = dat[i];
        }
        for (int i = dataSize - 1; i >= 0; --i) {
            value[i] = f(value[i * 2], value[i * 2 + 1]);
        }
    }

    void propagate(int index, int curL, int curR) {
        if (lazy[index] != Q()) {
            int left = index * 2, right = index * 2 + 1;
            value[index] = g(value[index], lazy[index]);
            if (curR - curL > 1) {
                lazy[left] = h(lazy[left], lazy[index]);
                lazy[right] = h(lazy[right], lazy[index]);
            }
            lazy[index] = Q();
        }
    }

    void update(int index, int curL, int curR, int givenL, int givenR, Q x) {
        propagate(index, curL, curR);

        if (curR <= givenL || givenR <= curL)return;

        if (givenL <= curL && curR <= givenR) {
            lazy[index] = h(lazy[index], x);
            propagate(index, curL, curR);
        } else {
            int mid = (curL + curR) / 2;
            update(index * 2, curL, mid, givenL, givenR, x);
            update(index * 2 + 1, mid, curR, givenL, givenR, x);
            value[index] = f(value[index * 2], value[index * 2 + 1]);
        }
    }

    void update(int l, int r, Q x) {
        update(1, 0, dataSize, l, r, x);
    }

    P query(int l, int r) {
        return query(1, 0, dataSize, l, r);
    }

    P query(int index, int curL, int curR, int givenL, int givenR) {
        if (curR <= givenL || givenR <= curL)return P();

        propagate(index, curL, curR);

        if (givenL <= curL && curR <= givenR) {
            return value[index];
        } else {
            int mid = (curL + curR) / 2;
            P resL = query(index * 2, curL, mid, givenL, givenR);
            P resR = query(index * 2 + 1, mid, curR, givenL, givenR);
            return f(resL, resR);
        }
    }
};

template<class Value>
class SegmentTree {
    int n;
    Value init;
    vector<Value> dat;
    function<Value(Value, Value)> calc;

    inline Value query(int a, int b, int k, int l, int r) {
        if (r <= a || b <= l) return init;
        if (a <= l && r <= b) return dat[k];
        else {
            Value vl, vr;
            vl = query(a, b, k << 1, l, (l + r) >> 1);
            vr = query(a, b, (k << 1) | 1, (l + r) >> 1, r);
            return calc(vl, vr);
        }
    }

public:
    SegmentTree() {}

    SegmentTree(int n_, Value init_, function<Value(Value, Value)> calc_)
        :n(1), init(init_), calc(calc_) {
        for (; n<n_; n <<= 1);
        dat = vector<Value>(n << 1, init);
    }

    void update(int k, Value a) {
        k += n;
        dat[k] = a;
        while (k>1) {
            k >>= 1;
            dat[k] = calc(dat[k << 1], dat[(k << 1) | 1]);
        }
    }

    Value query(int a, int b) {
        return query(a, b, 1, 0, n);
    }
};

struct P {
    ll mi, ma, mm;
    P() {
        mi = LLONG_MAX / 3;
        ma = LLONG_MIN / 3;
        mm = LLONG_MIN / 3;
    }
    P(ll a, ll b, ll c):mi(a),ma(b),mm(c){}
};

P f(P l, P r) {
    return P(min(l.mi, r.mi), max(l.ma, r.ma), max(r.ma - l.mi, max(l.mm, r.mm)));
}

P g(P p, ll q) {
    p.mi += q;
    p.ma += q;
    p.mm += q;
    return p;
}

void solve() {
    int N, Q;
    cin >> N >> Q;
    vll a(N), sm(N + 1);
    rep(i, N) {
        cin >> a[i];
        sm[i + 1] = sm[i] + a[i];
    }
    
    vector<P> ps(N + 1);
    rep(i, N + 1)ps[i] = P(sm[i], sm[i], LLONG_MIN/3);

    LazySegTree<P, ll> lst(ps, f, g, plus<ll>());
    rep(hoge, Q) {
        string s;
        cin >> s;
        if (s == "max") {
            int L1, L2, R1, R2;
            cin >> L1 >> L2 >> R1 >> R2;
            ll ans = LLONG_MIN;
            if (L2 < R1) {
                ans = lst.query(R1, R2 + 1).ma - lst.query(L1 - 1, L2).mi;
            } else {
                if (L2 <= R2) {
                    smax(ans, lst.query(L2, R2 + 1).ma - lst.query(L1 - 1, L2).mi);
                }
                if (L1 <= R1) {
                    smax(ans, lst.query(R1, R2 + 1).ma - lst.query(L1 - 1, R1).mi);
                }
                // smax(ans, lst.query(R1, L2).mm);
            }
            cout << ans << endl;
        } else {
            int i, x;
            cin >> i >> x;
            ll d = x - a[i - 1];
            lst.update(i, N + 1, d);
            a[i - 1] = x;
        }
    }
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout << fixed << setprecision(15);
	solve();
	return 0;
}
0