結果

問題 No.776 A Simple RMQ Problem
ユーザー HaarHaar
提出日時 2020-02-08 05:03:04
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 352 ms / 3,000 ms
コード長 3,878 bytes
コンパイル時間 2,203 ms
コンパイル使用メモリ 210,060 KB
実行使用メモリ 14,404 KB
最終ジャッジ日時 2023-10-26 00:41:48
合計ジャッジ時間 9,784 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 33 ms
6,284 KB
testcase_03 AC 121 ms
14,140 KB
testcase_04 AC 149 ms
6,228 KB
testcase_05 AC 249 ms
14,140 KB
testcase_06 AC 71 ms
8,800 KB
testcase_07 AC 160 ms
14,140 KB
testcase_08 AC 193 ms
8,800 KB
testcase_09 AC 73 ms
14,404 KB
testcase_10 AC 111 ms
8,800 KB
testcase_11 AC 200 ms
14,404 KB
testcase_12 AC 260 ms
14,404 KB
testcase_13 AC 263 ms
14,404 KB
testcase_14 AC 257 ms
14,404 KB
testcase_15 AC 263 ms
14,404 KB
testcase_16 AC 259 ms
14,404 KB
testcase_17 AC 352 ms
14,404 KB
testcase_18 AC 184 ms
14,404 KB
testcase_19 AC 305 ms
14,404 KB
testcase_20 AC 308 ms
14,404 KB
testcase_21 AC 302 ms
14,404 KB
testcase_22 AC 303 ms
14,404 KB
testcase_23 AC 313 ms
14,404 KB
testcase_24 AC 299 ms
14,404 KB
testcase_25 AC 70 ms
4,348 KB
testcase_26 AC 178 ms
14,404 KB
testcase_27 AC 154 ms
14,404 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>


#ifdef DEBUG
#include <Mylib/Debug/debug.cpp>
#else
#define dump(...)
#endif


template <typename Monoid>
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_left = Monoid::id();
    value_type ret_right = Monoid::id();
    
    int l = x + hsize, r = y + hsize;
    while(l < r){
      if(r & 1) ret_right = Monoid::op(data[--r], ret_right);
      if(l & 1) ret_left = Monoid::op(ret_left, data[l++]);
      l >>= 1, r >>= 1;
    }
    
    return Monoid::op(ret_left, ret_right);
  }

  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 MaxPartialSumMonoid{
  struct MaxPartialSum{
    T sum, left_max, right_max, partial_max;
    static auto make(T x){return MaxPartialSum({x, x, x, x});}
  };
  
  using value_type = std::optional<MaxPartialSum>;
  
  constexpr inline static value_type id(){
    return std::nullopt;
  }
  
  constexpr inline static value_type op(const value_type &a, const value_type &b){
    if(not a) return b;
    if(not b) return a;

    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; scanf("%d%d", &N, &Q);

  SegmentTree<Mon> seg(N);

  std::vector<int64_t> a(N);

  for(int i = 0; i < N; ++i){
    scanf("%lld", &a[i]);
    seg.update(i, Mon::MaxPartialSum::make(a[i]));
  }

  for(int i = 0; i < Q; ++i){
    std::string com; std::cin >> com;

    if(com == "set"){
      int i, x; scanf("%d%d", &i, &x);
      --i;
      seg.update(i, Mon::MaxPartialSum::make(x));
      a[i] = x;
    }else{
      int l1, l2, r1, r2; scanf("%d%d%d%d", &l1, &l2, &r1, &r2);
      --l1, --l2, --r1, --r2;

      r1 = std::max(l1,r1);
      l2 = std::min(l2,r2);
        
      int64_t ans = LLONG_MIN;
        
      auto f = [&](int L1, int L2, int R1, int R2){
                 auto ret =
                   seg.get(L1, L2+1).value_or(Mon::MaxPartialSum::make(0)).right_max +
                   seg.get(L2+1, R1).value_or(Mon::MaxPartialSum::make(0)).sum +
                   seg.get(R1, R2+1).value_or(Mon::MaxPartialSum::make(0)).left_max;

                 if(L2 == R1) ret -= a[L2];
                   
                 return ret;
               };

      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)->partial_max);
      }

      printf("%lld\n", ans);
    }
  }

  return 0;
}
0