結果
問題 | No.776 A Simple RMQ Problem |
ユーザー |
![]() |
提出日時 | 2019-01-02 18:23:13 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 260 ms / 3,000 ms |
コード長 | 3,619 bytes |
コンパイル時間 | 1,952 ms |
コンパイル使用メモリ | 173,264 KB |
実行使用メモリ | 12,088 KB |
最終ジャッジ日時 | 2024-11-21 10:19:57 |
合計ジャッジ時間 | 9,466 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 26 |
ソースコード
#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);}}}