結果

問題 No.776 A Simple RMQ Problem
ユーザー goodbatongoodbaton
提出日時 2018-12-25 11:04:29
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 351 ms / 3,000 ms
コード長 3,243 bytes
コンパイル時間 1,296 ms
コンパイル使用メモリ 107,620 KB
実行使用メモリ 11,880 KB
最終ジャッジ日時 2024-04-08 22:21:24
合計ジャッジ時間 8,908 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,548 KB
testcase_01 AC 2 ms
6,548 KB
testcase_02 AC 35 ms
6,548 KB
testcase_03 AC 137 ms
11,880 KB
testcase_04 AC 136 ms
6,548 KB
testcase_05 AC 253 ms
11,880 KB
testcase_06 AC 74 ms
7,552 KB
testcase_07 AC 181 ms
11,880 KB
testcase_08 AC 186 ms
7,552 KB
testcase_09 AC 98 ms
11,880 KB
testcase_10 AC 116 ms
7,680 KB
testcase_11 AC 228 ms
11,880 KB
testcase_12 AC 277 ms
11,880 KB
testcase_13 AC 276 ms
11,880 KB
testcase_14 AC 285 ms
11,880 KB
testcase_15 AC 278 ms
11,880 KB
testcase_16 AC 291 ms
11,880 KB
testcase_17 AC 351 ms
11,880 KB
testcase_18 AC 219 ms
11,880 KB
testcase_19 AC 314 ms
11,880 KB
testcase_20 AC 316 ms
11,880 KB
testcase_21 AC 323 ms
11,880 KB
testcase_22 AC 316 ms
11,880 KB
testcase_23 AC 319 ms
11,880 KB
testcase_24 AC 313 ms
11,880 KB
testcase_25 AC 35 ms
6,548 KB
testcase_26 AC 214 ms
11,880 KB
testcase_27 AC 195 ms
11,880 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>

#include <iostream>
#include <complex>
#include <string>
#include <algorithm>
#include <numeric>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>

#include <functional>
#include <cassert>

typedef long long ll;
using namespace std;

#ifdef LOCAL
#define debug(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl;
#else
#define debug(x) ;
#endif

#define mod 1000000007 //1e9+7(prime number)
#define INF 1000000000 //1e9
#define LLINF 2000000000000000000LL //2e18
#define SIZE 200010

/* Starry Sky Tree */
//0-index

struct StarrySkyTree{
  typedef pair<pair<ll,ll>, ll> Type;
  int segn2;
  vector<Type> data; // {{min, max}, diff}
  vector<ll> s_data;
  
  StarrySkyTree(int n)
  {
    for(segn2=1; segn2<n; segn2*=2);
    data.assign(segn2*2, {{0, 0}, -LLINF});
    s_data.assign(segn2*2, 0);
  }

  Type merge(Type a, Type b) {
    Type res;
    res.first.first = min(a.first.first, b.first.first);
    res.first.second = max(a.first.second, b.first.second);
    res.second = max({a.second, b.second, b.first.second - a.first.first});
    return res;
  }

  Type add(Type a, ll x) {
    a.first.first += x;
    a.first.second += x;
    return a;
  }
  
  //get value of [a,b)
  Type query(int a, int b, int l = 0, int r = -1, int k = 0){
    //debug(a); debug(b);
    if(r == -1) r = segn2;
    if(r <= a || b <= l) return {{LLINF, -LLINF}, -LLINF}; //大きさに注意
    if(a <= l && r <= b) return add(data[k], s_data[k]);
    
    return add(merge(query(a, b, l, (l+r)/2, k*2+1), query(a, b, (l+r)/2 , r, k*2+2)), s_data[k]);
  }
  
  //add x to [a,b)
  Type add(int a, int b, ll x, int l = 0, int r = -1, int k = 0){
    if(r == -1) r = segn2;
    if(a <= l && r <= b)
      s_data[k] += x;
    else if(a < r && l < b)
      data[k] = merge(add(a, b, x, l, (l+r)/2, k*2+1), add(a, b, x, (l+r)/2, r, k*2+2));
    
    return add(data[k], s_data[k]);
  }
  
};


int main(){
  int n, q;
  int a[SIZE];

  scanf("%d%d", &n, &q);

  StarrySkyTree seg(n+1);

  seg.add(n+1, INF, -LLINF);
  
  for(int i=0;i<n;i++){
    scanf("%d", a+i);
    seg.add(i+1, n+1, a[i]);
  }

  for(int i=0;i<q;i++){
    char s[5];
    scanf("%s", s);

    if(s[0] == 's'){
      //set
      int k, x;
      scanf("%d%d", &k, &x);
      seg.add(k, n+1, x - a[k-1]);
      a[k-1] = x;
    }else{
      //max
      int l1, l2, r1, r2;
      scanf("%d%d%d%d", &l1, &l2, &r1, &r2);
      l1--; l2--;

      ll ans = -LLINF;
      
      auto res1a = seg.query(l1, min(l2+1, r1));
      auto res1b = seg.query(r1, r2+1);
      ans = max(ans, res1b.first.second - res1a.first.first);

      debug(res1a.first.first);
      debug(res1b.first.second);
      
      auto res2a = seg.query(l1, l2+1);
      auto res2b = seg.query(max(r1, l2+1), r2+1);
      ans = max(ans, res2b.first.second - res2a.first.first);

      debug(res2a.first.first);
      debug(res2b.first.second);
      
      if(r1 <= l2){
        auto res3a = seg.query(max(r1-1, l1), min(r2, l2+1)+1);
        debug(res3a.second);
        ans = max(ans, res3a.second);
      }

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


0