結果

問題 No.776 A Simple RMQ Problem
ユーザー りあん
提出日時 2018-12-07 17:05:18
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 233 ms / 3,000 ms
コード長 3,180 bytes
コンパイル時間 1,988 ms
コンパイル使用メモリ 200,892 KB
最終ジャッジ日時 2025-01-06 18:40:37
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

diff #
プレゼンテーションモードにする

#include<bits/stdc++.h>
using namespace std;
const long long LM = 1e18;
struct node {
long long lmax, rmax, inmax, sum;
node(long long lmax, long long rmax, long long inmax, long long sum) : lmax(lmax), rmax(rmax), inmax(inmax), sum(sum) {}
node(long long x) : node(x, x, x, x) {}
};
const node ex(-LM, -LM, -LM, 0);
const node compose(const node& l, const node& r) {
return node(max(l.lmax, l.sum + r.lmax),
max(r.rmax, l.rmax + r.sum),
max({ l.inmax, r.inmax, l.rmax + r.lmax }),
l.sum + r.sum);
}
class segtree {
private:
int n, s, t;
vector<node> tr;
const node q(int k, int l, int r) {
return r <= s || t <= l ? ex : s <= l && r <= t ? tr[k]
: compose(q(k << 1 | 1, l, (l + r) >> 1), q((k + 1) << 1, (l + r) >> 1, r));
}
public:
segtree() {}
segtree(int m) {
n = 1;
while (n < m) n <<= 1;
tr = vector<node>((n << 1) - 1, ex);
}
void assign(int j, const node& x) {
tr[j + n - 1] = x;
}
void update(int j, const node& x) {
assign(j, x);
update(j);
}
void update(int j) {
int i = j + n - 1;
while (i > 0) {
i = (i - 1) >> 1;
tr[i] = compose(tr[i << 1 | 1], tr[(i + 1) << 1]);
}
}
void all_update() {
for (int i = n - 2; i >= 0; i--)
tr[i] = compose(tr[i << 1 | 1], tr[i + 1 << 1]);
}
// [s, t)
const node run(int _s, int _t) {
s = _s;
t = _t;
return q(0, 0, n);
}
};
segtree sg;
long long calc(int l, int r) {
return sg.run(l, r).inmax;
}
// l1 < l2 <= r1 < r2
long long calc(int l1, int l2, int r1, int r2) {
return sg.run(l1, l2).rmax + sg.run(r1, r2).lmax + (l2 == r1 ? 0 : sg.run(l2, r1).sum);
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n, q;
cin >> n >> q;
assert(1 <= n && n <= 100000);
assert(1 <= q && q <= 100000);
sg = segtree(n);
for (int i = 0; i < n; ++i) {
long long a;
cin >> a;
assert(-1000000000 <= a && a <= 1000000000);
sg.assign(i, node(a));
}
sg.all_update();
for (int _ = 0; _ < q; ++_) {
string s;
cin >> s;
if (s == "set") {
int i;
long long x;
cin >> i >> x;
assert(1 <= i && i <= n);
assert(-1000000000 <= x && x <= 1000000000);
--i;
sg.update(i, node(x));
}
else if (s == "max") {
int l1, l2, r1, r2;
cin >> l1 >> l2 >> r1 >> r2;
assert(1 <= l1 && l1 <= n);
assert(1 <= l2 && l2 <= n);
assert(1 <= r1 && r1 <= n);
assert(1 <= r2 && r2 <= n);
--l1;
--r1;
assert(l1 < l2 && r1 < r2 && l1 < r2);
l2 = min(l2, r2);
r1 = max(l1, r1);
if (l2 <= r1)
cout << calc(l1, l2, r1, r2) << "\n";
else
cout << max({ calc(r1, l2), calc(l1, l2, l2, r2), calc(l1, r1, r1, r2) }) << "\n";
}
else assert(0);
}
return 0;
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0