結果
| 問題 |
No.776 A Simple RMQ Problem
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2020-02-08 03:37:38 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 3,830 bytes |
| コンパイル時間 | 2,108 ms |
| コンパイル使用メモリ | 203,256 KB |
| 最終ジャッジ日時 | 2025-01-08 22:54:20 |
|
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 1 WA * 25 |
ソースコード
#include <bits/stdc++.h>
#ifdef DEBUG
#include <Mylib/Debug/debug.cpp>
#else
#define dump(...)
#endif
template <typename Monoid, bool COMMUTATIVE = false>
class SegmentTree{
using value_type = typename Monoid::value_type;
protected:
const int depth, size, hsize;
std::vector<value_type> data;
public:
SegmentTree(int n):
depth(n > 1 ? 32-__builtin_clz(n-1) + 1 : 1),
size((1 << depth) - 1),
hsize(size / 2 + 1),
data(size + 1, Monoid::id())
{}
inline auto operator[](int i) const {return at(i);}
inline auto at(int i) const {return data[hsize + i];}
inline auto get(int x, int y) const { // [x,y)
std::vector<int> memo;
value_type ret = Monoid::id();
int l = x + hsize, r = y + hsize;
while(l < r){
if(r & 1){
if(COMMUTATIVE) ret = Monoid::op(ret, data[--r]);
else memo.push_back(--r);
}
if(l & 1) ret = Monoid::op(ret, data[l++]);
l >>= 1, r >>= 1;
}
if(not COMMUTATIVE){
std::reverse(memo.begin(), memo.end());
for(auto i : memo) ret = Monoid::op(ret, data[i]);
}
return ret;
}
inline void update(int i, const value_type &x){
i += hsize;
data[i] = x;
while(i > 1) i >>= 1, data[i] = Monoid::op(data[i << 1 | 0], data[i << 1 | 1]);
}
template <typename T>
inline void init_with_vector(const std::vector<T> &val){
data.assign(size + 1, Monoid::id());
for(int i = 0; i < (int)val.size(); ++i) data[hsize + i] = val[i];
for(int i = hsize-1; i >= 1; --i) data[i] = Monoid::op(data[i << 1 | 0], data[i << 1 | 1]);
}
template <typename T>
inline void init(const T &val){
init_with_vector(std::vector<value_type>(hsize, val));
}
auto debug(){
return data;
}
};
template <typename T>
struct MaxPartialSum{
T sum;
T left_max, right_max, partial_max;
static auto make(T x){return MaxPartialSum<T>({x, std::max<T>(x, 0), std::max<T>(x, 0), std::max<T>(x, 0)});}
};
template <typename T>
struct MaxPartialSumMonoid{
using value_type = MaxPartialSum<T>;
constexpr inline static value_type id(){
return {0, 0, 0, 0};
}
constexpr inline static value_type op(const value_type &a, const value_type &b){
return {
a.sum + b.sum,
std::max(a.left_max, a.sum + std::max(b.left_max, b.sum)),
std::max(b.right_max, b.sum + std::max(a.right_max, a.sum)),
std::max({a.partial_max, b.partial_max, a.right_max + b.left_max})
};
}
};
using Mon = MaxPartialSumMonoid<int64_t>;
template <typename T, typename U>
void chmax(T &a, const U &b){
a = std::max(a, b);
}
int main(){
int N,Q;
while(std::cin >> N >> Q){
SegmentTree<Mon, false> seg(N);
std::vector<int64_t> a(N);
for(int i = 0; i < N; ++i){
std::cin >> a[i];
seg.update(i, MaxPartialSum<int64_t>::make(a[i]));
}
for(int i = 0; i < Q; ++i){
std::string com; std::cin >> com;
if(com == "set"){
int i, x; std::cin >> i >> x;
--i;
seg.update(i, MaxPartialSum<int64_t>::make(x));
a[i] = x;
}else{
int l1, l2, r1, r2; std::cin >> l1 >> l2 >> r1 >> r2;
--l1, --l2, --r1, --r2;
r1 = std::max(l1,r1);
l2 = std::min(l2,r2);
//dump(a, std::make_pair(l1, l2), std::make_pair(r1, r2));
int64_t ans = LLONG_MIN;
auto f = [&](int L1, int L2, int R1, int R2){return seg.get(L1, L2).right_max + seg.get(L2, R1+1).sum + seg.get(R1+1, R2+1).left_max;};
if(l2 <= r1){
ans = f(l1, l2, r1, r2);
}else{
chmax(ans, f(l1, r1, r1, r2));
chmax(ans, f(l1, l2, l2, r2));
chmax(ans, seg.get(r1, l2+1).sum);
}
std::cout << ans << std::endl;
}
}
std::cerr << std::endl;
}
return 0;
}