結果

問題 No.776 A Simple RMQ Problem
ユーザー やむなくやむなく
提出日時 2019-01-14 20:12:25
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 1,260 ms / 3,000 ms
コード長 4,202 bytes
コンパイル時間 2,194 ms
コンパイル使用メモリ 166,760 KB
実行使用メモリ 13,744 KB
最終ジャッジ日時 2023-09-05 04:01:30
合計ジャッジ時間 21,823 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 81 ms
5,768 KB
testcase_03 AC 333 ms
13,172 KB
testcase_04 AC 446 ms
5,764 KB
testcase_05 AC 772 ms
13,148 KB
testcase_06 AC 204 ms
8,040 KB
testcase_07 AC 471 ms
13,340 KB
testcase_08 AC 597 ms
8,052 KB
testcase_09 AC 168 ms
13,272 KB
testcase_10 AC 327 ms
8,044 KB
testcase_11 AC 620 ms
13,328 KB
testcase_12 AC 801 ms
13,596 KB
testcase_13 AC 799 ms
13,744 KB
testcase_14 AC 802 ms
13,584 KB
testcase_15 AC 793 ms
13,588 KB
testcase_16 AC 803 ms
13,740 KB
testcase_17 AC 1,260 ms
13,592 KB
testcase_18 AC 445 ms
13,692 KB
testcase_19 AC 1,067 ms
13,596 KB
testcase_20 AC 984 ms
13,636 KB
testcase_21 AC 1,065 ms
13,592 KB
testcase_22 AC 980 ms
13,552 KB
testcase_23 AC 1,018 ms
13,672 KB
testcase_24 AC 962 ms
13,588 KB
testcase_25 AC 103 ms
4,376 KB
testcase_26 AC 362 ms
13,552 KB
testcase_27 AC 283 ms
13,688 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define abs(a) max((a),-(a))
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define repe(i,n) rep(i,(n)+1)
#define per(i,n) for(int i=(int)(n)-1;i>=0;i--)
#define pere(i,n) rep(i,(n)+1)
#define all(x) (x).begin(),(x).end()
#define SP <<" "<<
#define RET return 0
#define MOD 1000000007
#define INF 1000000000000000

typedef long long LL;
typedef long double LD;

struct LazySegmentTree {
private:
  int n;
  vector<LL> maxnode, minnode, maxrange, lazy, a;

public:
  LazySegmentTree(vector<LL> v) {
    a=v;
    int sz = (int)v.size()+1;
    n = 1; while(n < sz) n *= 2;
    maxnode.resize(2*n-1,-INF);
    minnode.resize(2*n-1,INF);
    maxrange.resize(2*n-1,-INF);
    lazy.resize(2*n-1, 0);
    maxnode[n-1]=minnode[n-1]=0;
    for(int i=1; i<sz; i++){
      maxnode[i+n-1]=minnode[i+n-1]=maxnode[i+n-2]+v[i-1];
    }
    for(int i=n-2; i>=0; i--){
      maxnode[i] = max(maxnode[i*2+1],maxnode[i*2+2]);
      minnode[i] = min(minnode[i*2+1],minnode[i*2+2]);
      maxrange[i] = max(max(maxrange[i*2+1],maxrange[i*2+2]),maxnode[i*2+2]-minnode[i*2+1]);
    }
  }

  void disp(){
    for(int i=0;i<maxnode.size();i++){
      cout << maxnode[i] << " ";
    }
    cout << endl;
    for(int i=0;i<maxnode.size();i++){
      cout << minnode[i] << " ";
    }
    cout << endl;
    for(int i=0;i<maxnode.size();i++){
      cout << maxrange[i] << " ";
    }
    cout << endl;
  }

  void eval(int k, int l, int r) {
    if(lazy[k] != 0) {
      maxnode[k] += lazy[k];
      minnode[k] += lazy[k];
      if(r - l > 1) {
        lazy[2*k+1] += lazy[k];
        lazy[2*k+2] += lazy[k];
      }
      lazy[k] = 0;
    }
  }

  void set(int id, LL x){
    add(id+1,n,x-a[id]);
    a[id]=x;
  }

  void add(int a, int b, LL x, int k=0, int l=0, int r=-1) {
    if(r < 0) r = n;
    eval(k, l, r);
    if(b <= l || r <= a) return;
    if(a <= l && r <= b) {
      lazy[k] += x;
      eval(k, l, r);
    }else{
      add(a, b, x, 2*k+1, l, (l+r)/2);
      add(a, b, x, 2*k+2, (l+r)/2, r);
      maxnode[k]=max(maxnode[2*k+1],maxnode[2*k+2]);
      minnode[k]=min(minnode[2*k+1],minnode[2*k+2]);
      maxrange[k]=max(max(maxrange[k*2+1],maxrange[k*2+2]),maxnode[k*2+2]-minnode[k*2+1]);
    }
  }

  LL getmin(int a, int b, int k=0, int l=0, int r=-1) {
    if(r < 0) r = n;
    if(b <= l || r <= a) return INF;
    eval(k, l, r);
    if(a <= l && r <= b) return minnode[k];
    LL vl = getmin(a, b, 2*k+1, l, (l+r)/2);
    LL vr = getmin(a, b, 2*k+2, (l+r)/2, r);
    return min(vl,vr);
  }

  LL getmax(int a, int b, int k=0, int l=0, int r=-1) {
    if(r < 0) r = n;
    if(b <= l || r <= a) return -INF;
    eval(k, l, r);
    if(a <= l && r <= b) return maxnode[k];
    LL vl = getmax(a, b, 2*k+1, l, (l+r)/2);
    LL vr = getmax(a, b, 2*k+2, (l+r)/2, r);
    return max(vl,vr);
  }

  LL getmaxrange(int a, int b, int k=0, int l=0, int r=-1){
    if(r < 0) r = n;
    if(b <= l || r <= a) return -INF;
    eval(k, l, r);
    if(a <= l && r <= b) return maxrange[k];
    LL vl = getmaxrange(a, b, 2*k+1, l, (l+r)/2);
    LL vr = getmaxrange(a, b, 2*k+2, (l+r)/2, r);
    return max(max(vl,vr),getmax(a, b, 2*k+2, (l+r)/2, r)-getmin(a, b, 2*k+1, l, (l+r)/2));
  }
};

int main(){
  int n,q;
  cin >> n >> q;
  vector<LL> a(n);
  for(int i=0;i<n;i++) cin >> a[i];
  LazySegmentTree seg(a);
  // seg.disp();
  string s;
  for(int t=0;t<q;t++){
    cin >> s;
    if(s=="set"){
      int id,x;
      cin >> id >> x;
      id--;
      seg.set(id,x);
    }else{
      int l1,l2,r1,r2;
      cin >> l1 >> l2 >> r1 >> r2;
      l1--,l2--,r1--,r2--;
      r1=max(r1,l1);
      l2=min(l2,r2);
      // cout << l1 SP l2 SP r1 SP r2 << endl;
      if(l2<r1){
        cout << seg.getmax(r1+1,r2+2)-seg.getmin(l1,l2+1) << endl;
      }else{
        // cout << seg.getmax(r1+1,r2+2) SP seg.getmin(l1,r1) << endl;
        // cout << seg.getmax(l2+2,r2+2) SP seg.getmin(l1,l2+1) << endl;
        // cout << seg.getmaxrange(r1,l2+2) << endl;
        cout << max(max(seg.getmax(r1+1,r2+2)-seg.getmin(l1,r1),seg.getmax(l2+2,r2+2)-seg.getmin(l1,l2+1)),seg.getmaxrange(r1,l2+2)) << endl;
      }
    }
  }
  return 0;
}
0