結果

問題 No.776 A Simple RMQ Problem
ユーザー 👑 はまやんはまやんはまやんはまやん
提出日時 2019-01-02 18:44:43
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 212 ms / 3,000 ms
コード長 3,618 bytes
コンパイル時間 2,046 ms
コンパイル使用メモリ 177,848 KB
実行使用メモリ 11,660 KB
最終ジャッジ日時 2023-08-13 15:19:59
合計ジャッジ時間 7,869 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
11,096 KB
testcase_01 AC 5 ms
11,224 KB
testcase_02 AC 23 ms
11,216 KB
testcase_03 AC 75 ms
11,528 KB
testcase_04 AC 85 ms
11,096 KB
testcase_05 AC 146 ms
11,600 KB
testcase_06 AC 45 ms
11,272 KB
testcase_07 AC 100 ms
11,480 KB
testcase_08 AC 111 ms
11,060 KB
testcase_09 AC 50 ms
11,584 KB
testcase_10 AC 66 ms
11,532 KB
testcase_11 AC 123 ms
11,648 KB
testcase_12 AC 157 ms
11,528 KB
testcase_13 AC 155 ms
11,476 KB
testcase_14 AC 155 ms
11,484 KB
testcase_15 AC 157 ms
11,484 KB
testcase_16 AC 156 ms
11,660 KB
testcase_17 AC 212 ms
11,480 KB
testcase_18 AC 113 ms
11,520 KB
testcase_19 AC 179 ms
11,528 KB
testcase_20 AC 184 ms
11,588 KB
testcase_21 AC 181 ms
11,536 KB
testcase_22 AC 184 ms
11,604 KB
testcase_23 AC 182 ms
11,576 KB
testcase_24 AC 183 ms
11,480 KB
testcase_25 AC 42 ms
11,100 KB
testcase_26 AC 117 ms
11,548 KB
testcase_27 AC 97 ms
11,544 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({ x.lft, x.sm + y.lft, x.sm });
    res.rht = max({ y.rht, x.rht + y.sm, y.sm });
    res.opt = 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