結果

問題 No.776 A Simple RMQ Problem
ユーザー beetbeet
提出日時 2018-12-25 15:02:35
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 184 ms / 3,000 ms
コード長 2,853 bytes
コンパイル時間 2,463 ms
コンパイル使用メモリ 215,004 KB
実行使用メモリ 14,568 KB
最終ジャッジ日時 2024-04-08 22:31:48
合計ジャッジ時間 7,860 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 21 ms
6,676 KB
testcase_03 AC 73 ms
13,776 KB
testcase_04 AC 77 ms
6,676 KB
testcase_05 AC 149 ms
13,928 KB
testcase_06 AC 45 ms
8,384 KB
testcase_07 AC 104 ms
14,080 KB
testcase_08 AC 97 ms
8,536 KB
testcase_09 AC 57 ms
14,232 KB
testcase_10 AC 61 ms
8,688 KB
testcase_11 AC 122 ms
14,384 KB
testcase_12 AC 146 ms
14,568 KB
testcase_13 AC 153 ms
14,568 KB
testcase_14 AC 154 ms
14,568 KB
testcase_15 AC 142 ms
14,568 KB
testcase_16 AC 146 ms
14,568 KB
testcase_17 AC 184 ms
14,568 KB
testcase_18 AC 131 ms
14,568 KB
testcase_19 AC 158 ms
14,568 KB
testcase_20 AC 168 ms
14,568 KB
testcase_21 AC 170 ms
14,568 KB
testcase_22 AC 161 ms
14,568 KB
testcase_23 AC 171 ms
14,568 KB
testcase_24 AC 161 ms
14,568 KB
testcase_25 AC 31 ms
6,676 KB
testcase_26 AC 123 ms
14,568 KB
testcase_27 AC 121 ms
14,568 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using Int = long long;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}

template <typename T,typename E>
struct SegmentTree{
  using F = function<T(T,T)>;
  using G = function<T(T,E)>;
  Int n;
  F f;
  G g;
  T ti;
  vector<T> dat;
  SegmentTree(){};
  SegmentTree(F f,G g,T ti):f(f),g(g),ti(ti){}

  void init(Int n_){    
    n=1;
    while(n<n_) n<<=1;
    dat.assign(n<<1,ti);
  }
  void build(const vector<T> &v){
    Int n_=v.size();
    init(n_);
    for(Int i=0;i<n_;i++) dat[n+i]=v[i];
    for(Int i=n-1;i;i--)
      dat[i]=f(dat[(i<<1)|0],dat[(i<<1)|1]);
  }
  void update(Int k,E a){
    k+=n;
    dat[k]=g(dat[k],a);
    while(k>>=1)
      dat[k]=f(dat[(k<<1)|0],dat[(k<<1)|1]);
  }
  void set_val(Int k,T x){
    dat[k+=n]=x;
    while(k>>=1)
      dat[k]=f(dat[(k<<1)|0],dat[(k<<1)|1]);    
  }
  T query(Int a,Int b){
    T vl=ti,vr=ti;
    for(Int l=a+n,r=b+n;l<r;l>>=1,r>>=1) {
      if(l&1) vl=f(vl,dat[l++]);
      if(r&1) vr=f(dat[--r],vr);
    }
    return f(vl,vr);
  }
};


struct FastIO{
  FastIO(){
    cin.tie(0);
    ios::sync_with_stdio(0);
  }
}fastio_beet;

//INSERT ABOVE HERE

signed main(){
  Int n,q;
  cin>>n>>q;

  struct T{
    Int va,vi,vl,vr;
    T(){}
    T(Int va,Int vi,Int vl,Int vr):va(va),vi(vi),vl(vl),vr(vr){}
    bool operator==(const T&a)const{
      return va==a.va&&vi==a.vi&&vl==a.vl&&vr==a.vr;
    }
  };
  
  const Int INF = 1e15;
  T ti(-INF,-INF,-INF,-INF);
  auto f=[&](T a,T b){
           if(a==ti) return b;
           if(b==ti) return a;
           Int cva=a.va+b.va,cvi=max(a.vi,b.vi),cvl=a.vl,cvr=b.vr;
           chmax(cvi,a.vr+b.vl);
           chmax(cvl,a.va+b.vl);
           chmax(cvr,a.vr+b.va);
           return T(cva,cvi,cvl,cvr);
         };
  
  auto g=[&](T a,Int b){a.va++;return T(b,b,b,b);};

  SegmentTree<T, Int> seg(f,g,ti);  
  seg.build(vector<T>(n,ti));

  vector<Int> a(n);
  for(Int i=0;i<n;i++) cin>>a[i];
  for(Int i=0;i<n;i++) seg.update(i,a[i]);

  for(Int i=0;i<q;i++){
    string s;
    cin>>s;
    if(s=="set"s){
      Int k,x;
      cin>>k>>x;
      k--;
      seg.update(k,x);
    }
    if(s=="max"s){
      Int l1,l2,r1,r2;
      cin>>l1>>l2>>r1>>r2;
      l1--;r1--;
      chmin(l2,r2);
      chmax(r1,l1);
      if(l2<=r1){
        auto x=seg.query(l1,l2);
        auto y=seg.query(r1,r2);
        auto z=seg.query(l2,r1);
        Int ans=x.vr+(l2==r1?0:z.va)+y.vl; 
        cout<<ans<<"\n";
      }else{
        auto x=seg.query(l1,r1);
        auto y=seg.query(l2,r2);
        auto z=seg.query(r1,l2);
        Int ans=x.vr+z.va+y.vl;
        chmax(ans,z.vi);
        chmax(ans,z.vr+y.vl);
        chmax(ans,x.vr+z.vl);        
        cout<<ans<<"\n";
      }
    }
  }
  cout<<flush;
  return 0;
}
0