結果

問題 No.776 A Simple RMQ Problem
ユーザー 👑 はまやんはまやんはまやんはまやん
提出日時 2019-01-02 18:23:13
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 269 ms / 3,000 ms
コード長 3,619 bytes
コンパイル時間 1,717 ms
コンパイル使用メモリ 173,584 KB
実行使用メモリ 12,104 KB
最終ジャッジ日時 2024-05-01 07:36:44
合計ジャッジ時間 10,350 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
11,568 KB
testcase_01 AC 5 ms
11,556 KB
testcase_02 AC 28 ms
11,768 KB
testcase_03 AC 94 ms
11,880 KB
testcase_04 AC 111 ms
11,804 KB
testcase_05 AC 191 ms
11,888 KB
testcase_06 AC 58 ms
11,752 KB
testcase_07 AC 129 ms
12,104 KB
testcase_08 AC 145 ms
11,840 KB
testcase_09 AC 68 ms
12,020 KB
testcase_10 AC 89 ms
11,680 KB
testcase_11 AC 166 ms
11,884 KB
testcase_12 AC 211 ms
11,984 KB
testcase_13 AC 213 ms
12,088 KB
testcase_14 AC 208 ms
11,944 KB
testcase_15 AC 207 ms
12,012 KB
testcase_16 AC 206 ms
12,012 KB
testcase_17 AC 269 ms
11,924 KB
testcase_18 AC 148 ms
11,940 KB
testcase_19 AC 235 ms
12,008 KB
testcase_20 AC 241 ms
12,052 KB
testcase_21 AC 244 ms
12,012 KB
testcase_22 AC 240 ms
11,952 KB
testcase_23 AC 241 ms
12,084 KB
testcase_24 AC 228 ms
12,040 KB
testcase_25 AC 47 ms
11,564 KB
testcase_26 AC 150 ms
12,028 KB
testcase_27 AC 129 ms
12,004 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