結果

問題 No.776 A Simple RMQ Problem
ユーザー HaarHaar
提出日時 2020-02-08 02:08:47
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 494 ms / 3,000 ms
コード長 3,851 bytes
コンパイル時間 3,848 ms
コンパイル使用メモリ 215,176 KB
実行使用メモリ 12,404 KB
最終ジャッジ日時 2023-10-26 00:34:42
合計ジャッジ時間 14,941 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 46 ms
5,744 KB
testcase_03 AC 164 ms
12,140 KB
testcase_04 AC 204 ms
5,744 KB
testcase_05 AC 332 ms
12,140 KB
testcase_06 AC 98 ms
7,788 KB
testcase_07 AC 218 ms
12,140 KB
testcase_08 AC 261 ms
7,788 KB
testcase_09 AC 100 ms
12,404 KB
testcase_10 AC 152 ms
7,788 KB
testcase_11 AC 271 ms
12,404 KB
testcase_12 AC 351 ms
12,404 KB
testcase_13 AC 353 ms
12,404 KB
testcase_14 AC 355 ms
12,404 KB
testcase_15 AC 357 ms
12,404 KB
testcase_16 AC 353 ms
12,404 KB
testcase_17 AC 494 ms
12,404 KB
testcase_18 AC 257 ms
12,404 KB
testcase_19 AC 421 ms
12,404 KB
testcase_20 AC 417 ms
12,404 KB
testcase_21 AC 417 ms
12,404 KB
testcase_22 AC 423 ms
12,404 KB
testcase_23 AC 420 ms
12,404 KB
testcase_24 AC 410 ms
12,404 KB
testcase_25 AC 104 ms
4,348 KB
testcase_26 AC 246 ms
12,404 KB
testcase_27 AC 217 ms
12,404 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>



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 MaxPartialSumMonoid{
  using sum = T;
  using left_max = T;
  using right_max = T;
  using partial_max = T;
  using value_type = std::tuple<sum, left_max, right_max, partial_max>;

  static T NINF;

  constexpr inline static value_type id(){
    return {
            0, NINF, NINF, NINF
    };
  }
  
  constexpr inline static value_type op(const value_type &a, const value_type &b){
    return {
            std::get<0>(a) + std::get<0>(b),
            std::max({std::get<1>(a), std::get<0>(a) + std::get<1>(b), std::get<0>(a) + std::get<0>(b)}),
            std::max({std::get<2>(b), std::get<0>(b) + std::get<2>(a), std::get<0>(a) + std::get<0>(b)}),
            std::max({std::get<3>(a), std::get<3>(b), std::get<2>(a) + std::get<1>(b)})
    };
  }
};

using Mon = MaxPartialSumMonoid<int64_t>;
template <> int64_t Mon::NINF = -(1LL<<50);


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, {a[i], a[i], a[i], 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, {x, x, x, 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);

        int64_t ans = LLONG_MIN;
        
        if(l2 <= r1){
          ans =
            std::get<2>(seg.get(l1,l2+1)) +
            std::get<0>(seg.get(l2,r1+1)) +
            std::get<1>(seg.get(r1,r2+1)) -
            a[r1] - a[l2];
        }else{
          chmax(ans, std::get<2>(seg.get(l1,r1+1)) + std::get<1>(seg.get(r1,r2+1)) - a[r1]);
          chmax(ans, std::get<2>(seg.get(l1,l2+1)) + std::get<1>(seg.get(l2,r2+1)) - a[l2]);
          chmax(ans, std::get<3>(seg.get(r1,l2+1)));
        }

        std::cout << ans << std::endl;
      }
    }

    std::cerr << std::endl;
  }


  return 0;
}
0