結果

問題 No.776 A Simple RMQ Problem
ユーザー fumiphysfumiphys
提出日時 2019-03-12 23:36:16
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 479 ms / 3,000 ms
コード長 4,466 bytes
コンパイル時間 1,322 ms
コンパイル使用メモリ 125,748 KB
実行使用メモリ 18,508 KB
最終ジャッジ日時 2024-06-23 15:54:55
合計ジャッジ時間 11,466 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 34 ms
6,912 KB
testcase_03 AC 135 ms
16,744 KB
testcase_04 AC 179 ms
7,168 KB
testcase_05 AC 285 ms
17,024 KB
testcase_06 AC 82 ms
9,676 KB
testcase_07 AC 179 ms
17,312 KB
testcase_08 AC 229 ms
9,856 KB
testcase_09 AC 69 ms
17,664 KB
testcase_10 AC 129 ms
10,264 KB
testcase_11 AC 231 ms
18,048 KB
testcase_12 AC 304 ms
18,304 KB
testcase_13 AC 295 ms
18,304 KB
testcase_14 AC 299 ms
18,304 KB
testcase_15 AC 301 ms
18,508 KB
testcase_16 AC 300 ms
18,304 KB
testcase_17 AC 479 ms
18,304 KB
testcase_18 AC 165 ms
18,432 KB
testcase_19 AC 392 ms
18,432 KB
testcase_20 AC 399 ms
18,432 KB
testcase_21 AC 418 ms
18,284 KB
testcase_22 AC 383 ms
18,308 KB
testcase_23 AC 380 ms
18,280 KB
testcase_24 AC 388 ms
18,304 KB
testcase_25 AC 51 ms
5,376 KB
testcase_26 AC 154 ms
18,432 KB
testcase_27 AC 111 ms
18,304 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// includes
#include <cstdio>
#include <cstdint>
#include <iostream>
#include <iomanip>
#include <string>
#include <queue>
#include <stack>
#include <vector>
#include <set>
#include <map>
#include <unordered_map>
#include <algorithm>
#include <utility>
#include <functional>
#include <cmath>
#include <climits>
#include <bitset>
#include <list>
#include <random>

// macros
#define ll long long int
#define pb emplace_back
#define mk make_pair
#define pq priority_queue
#define FOR(i, a, b) for(int i=(a);i<(b);++i)
#define rep(i, n) FOR(i, 0, n)
#define rrep(i, n) for(int i=((int)(n)-1);i>=0;i--)
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())

using namespace std;

//  types
typedef pair<int, int> P;
typedef pair<ll, int> Pl;
typedef pair<ll, ll> Pll;
typedef pair<double, double> Pd;
 
// constants
const int inf = 1e9;
const ll linf = 1LL << 50;
const double EPS = 1e-10;
const int mod = 1e9 + 7;

// solve
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(a > b){a = b; return 1;} return 0;}

template<typename T, typename E>
struct SegmentTree_ {
  function<T(T, T)> f;
  function<T(T, E)> g;
  int n;
  T def;
  vector<T> vec;
  SegmentTree_(){}
  SegmentTree_(int n_, function<T(T, T)> f_, function<T(T, E)> g_, T def_, vector<T> v=vector<T>()){
    f = f_;
    g = g_;
    def = def_;

    // initialize vector
    n = 1;
    while(n < n_){
      n *= 2;
    }
    vec = vector<T>(2*n -1, def);

    // initialize segment tree
    for(int i = 0; i < v.size(); i++){
      vec[i + n - 1] = v[i];
    }
    for(int i = n - 2; i >= 0; i--){
      vec[i] = f(vec[2*i+1], vec[2*i+2]);
    }
  }
  void update(int k, E val){
    k = k + n - 1;
    vec[k] = g(vec[k], val);
    while(k > 0){
      k = (k - 1) / 2;
      vec[k] = f(vec[2*k+1], vec[2*k+2]);
    }
  }
  // [l, r) -> [a, b) (at k)
  T query(int a, int b, int k, int l, int r){
    if(r <= a || b <= l)return def;
    if(a <= l && r <= b)return vec[k];
    T ld = query(a, b, 2*k+1, l, (l+r)/2);
    T rd = query(a, b, 2*k+2, (l+r)/2, r);
    return f(ld, rd);
  }
  T query(int a, int b){
    return query(a, b, 0, 0, n);
  }
};

template<typename T, typename E>
using SegmentTree = struct SegmentTree_<T, E>;
using SegmentTreeI = SegmentTree<int, int>;

struct section{
  ll sum, lsum, rsum, msum;
  section(): section(0, -linf, -linf, -linf){}
  section(ll sum, ll lsum, ll rsum, ll msum): sum(sum), lsum(lsum), rsum(rsum), msum(msum){}
  section operator+(const section &right) const{
    section res;
    res.sum = sum + right.sum;
    res.lsum = max(lsum, sum + right.lsum);
    res.rsum = max(right.rsum, right.sum + rsum);
    res.msum = max(msum, max(right.msum, rsum + right.lsum));
    return res;
  }
};

int main(int argc, char const* argv[])
{
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n, q;
  cin >> n >> q;
  vector<ll> a(n);
  rep(i, n)cin >> a[i];

  vector<section> vec(n);
  for(int i = 0; i < n; i++){
    vec[i] = section(a[i], a[i], a[i], a[i]);
  }

  SegmentTree<section, section> seg = SegmentTree<section, section>(n, [](section a, section b){return a + b;},
      [](section a, section b){return b;}, section(), vec);

  rep(i_, q){
    string s;
    cin >> s;
    if(s == "set"){
      int i;
      ll x;
      cin >> i >> x;
      i--;
      seg.update(i, section(x, x, x, x));
    }else{
      int l1, l2, r1, r2;
      cin >> l1 >> l2 >> r1 >> r2;
      l1--, l2--, r1--, r2--;
      ll res = 0;
      if(l2 < r1){
        res += seg.query(l1, l2+1).rsum;
        res += seg.query(l2+1, r1).sum;
        res += seg.query(r1, r2+1).lsum;
      }else{
        r1 = max(r1, l1);
        l2 = min(l2, r2);
        res = -linf;
        ll tmp = 0;
        tmp += seg.query(l1, r1).rsum;
        tmp += seg.query(r1, l2+1).sum;
        tmp += seg.query(l2+1, r2+1).lsum;
        res = max(res, tmp);
        tmp = 0;
        tmp += seg.query(r1, l2+1).rsum;
        tmp += seg.query(l2+1, r2+1).lsum;
        res = max(res, tmp);
        tmp = 0;
        tmp += seg.query(l1, r1).rsum;
        tmp += seg.query(r1, l2+1).lsum;
        res = max(res, tmp);
        tmp = 0;
        tmp += seg.query(r1, l2+1).msum;
        res = max(res, tmp);
      }
      cout << res << endl;
    }
    //for(int i = 0; i < n; i++)cout << seg.query(i, i+1).rsum << "\n "[i + 1 != n];
  }
	return 0;
}
0