結果

問題 No.776 A Simple RMQ Problem
ユーザー 👑 はまやんはまやんはまやんはまやん
提出日時 2019-01-02 18:43:50
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 187 ms / 3,000 ms
コード長 3,645 bytes
コンパイル時間 1,781 ms
コンパイル使用メモリ 177,192 KB
実行使用メモリ 12,124 KB
最終ジャッジ日時 2024-05-01 08:10:16
合計ジャッジ時間 6,777 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
11,564 KB
testcase_01 AC 4 ms
11,644 KB
testcase_02 AC 21 ms
11,680 KB
testcase_03 AC 66 ms
11,924 KB
testcase_04 AC 77 ms
11,620 KB
testcase_05 AC 130 ms
11,972 KB
testcase_06 AC 41 ms
11,688 KB
testcase_07 AC 87 ms
11,916 KB
testcase_08 AC 102 ms
11,756 KB
testcase_09 AC 44 ms
11,932 KB
testcase_10 AC 60 ms
11,760 KB
testcase_11 AC 113 ms
11,924 KB
testcase_12 AC 138 ms
12,000 KB
testcase_13 AC 134 ms
12,124 KB
testcase_14 AC 141 ms
12,016 KB
testcase_15 AC 137 ms
11,988 KB
testcase_16 AC 137 ms
11,944 KB
testcase_17 AC 187 ms
11,984 KB
testcase_18 AC 105 ms
12,036 KB
testcase_19 AC 159 ms
11,952 KB
testcase_20 AC 167 ms
11,980 KB
testcase_21 AC 156 ms
11,940 KB
testcase_22 AC 157 ms
12,020 KB
testcase_23 AC 165 ms
12,016 KB
testcase_24 AC 163 ms
11,940 KB
testcase_25 AC 40 ms
11,628 KB
testcase_26 AC 107 ms
12,040 KB
testcase_27 AC 96 ms
12,064 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rrep(i,a,b) for(int i=a;i>=b;i--)
#define fore(i,a) for(auto &i:a)
#define all(x) (x).begin(),(x).end()
#pragma GCC optimize ("-O3")
using namespace std; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); }
typedef long long ll; const int inf = INT_MAX / 2; const ll infl = 1LL << 60;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a = b; return 1; } return 0; }
//---------------------------------------------------------------------------------------------------
struct func {
    ll sm, lft, rht, opt;
    func(ll a = infl, ll b = 0, ll c = 0, ll d = 0) : sm(a), lft(b), rht(c), opt(d) {}
};
func operator*(func x, func y) {
    func res;

    if (x.sm == infl) return y;
    if (y.sm == infl) return x;

    res.sm = x.sm + y.sm;
    res.lft = max(max( x.lft, x.sm + y.lft), x.sm );
    res.rht = max(max( y.rht, x.rht + y.sm), y.sm );
    res.opt = max(max(max(max(max(max(x.opt, y.opt), x.rht + y.lft), x.rht), x.lft), y.rht), y.lft);

    return res;
}
//---------------------------------------------------------------------------------------------------
template<class V, int NV> struct SegTree { // [L,R)
    V comp(V l, V r) { return l * r; };
    vector<V> val; SegTree() { val = vector<V>(NV * 2); }
    V get(int x, int y, int l = 0, int r = NV, int k = 1) {
        if (r <= x || y <= l)return V(); if (x <= l && r <= y)return val[k]; auto A = get(x, y, l, (l + r) / 2, k * 2);
        auto B = get(x, y, (l + r) / 2, r, k * 2 + 1); return comp(A, B);
    }
    void update(int i, V v) { i += NV; val[i] = v; while (i > 1)i >>= 1, val[i] = comp(val[i * 2], val[i * 2 + 1]); }
};
/*---------------------------------------------------------------------------------------------------
            ∧_∧  
      ∧_∧  (´<_` )  Welcome to My Coding Space!
     ( ´_ゝ`) /  ⌒i     
    /   \     | |     
    /   / ̄ ̄ ̄ ̄/  |  
  __(__ニつ/     _/ .| .|____  
     \/____/ (u ⊃  
---------------------------------------------------------------------------------------------------*/





int N, Q, A[101010];
SegTree<func, 1 << 17> st;
//---------------------------------------------------------------------------------------------------
void upd(int i, ll v) {
    st.update(i, func(v, v, v, v));
}
//---------------------------------------------------------------------------------------------------
void _main() {
    cin >> N >> Q;
    rep(i, 0, N) {
        cin >> A[i];
        upd(i, A[i]);
    }
    rep(q, 0, Q) {
        string s; cin >> s;
        if (s == "set") {
            int i, x; cin >> i >> x; i--;
            upd(i, x);
        } else {
            int a, b, c, d; cin >> a >> b >> c >> d;
            a--; b--; c--; d--;

            c = max(c, a);
            b = min(b, d);

            ll ans = -infl;
            if (b < c) {
                ans = 0;
                ans += st.get(a, b + 1).rht;
                if(b + 1 < c) ans += st.get(b + 1, c).sm;
                ans += st.get(c, d + 1).lft;
            } else {
                if(c < b + 1) chmax(ans, st.get(c, b + 1).opt);
                if(a < c and c < d + 1) chmax(ans, st.get(a, c).rht + st.get(c, d + 1).lft);
                if(a < b + 1 and b + 1 < d + 1) chmax(ans, st.get(a, b + 1).rht + st.get(b + 1, d + 1).lft);
            }
            printf("%lld\n", ans);
        }
    }
}
0