結果

問題 No.230 Splarraay スプラレェーイ
ユーザー beetbeet
提出日時 2018-11-09 13:36:01
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 284 ms / 5,000 ms
コード長 2,719 bytes
コンパイル時間 2,250 ms
コンパイル使用メモリ 210,424 KB
実行使用メモリ 17,112 KB
最終ジャッジ日時 2023-08-13 10:53:51
合計ジャッジ時間 5,181 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 3 ms
4,376 KB
testcase_06 AC 13 ms
4,376 KB
testcase_07 AC 3 ms
4,376 KB
testcase_08 AC 4 ms
4,376 KB
testcase_09 AC 122 ms
9,960 KB
testcase_10 AC 102 ms
4,380 KB
testcase_11 AC 73 ms
6,636 KB
testcase_12 AC 124 ms
9,856 KB
testcase_13 AC 19 ms
4,376 KB
testcase_14 AC 92 ms
16,864 KB
testcase_15 AC 188 ms
17,108 KB
testcase_16 AC 246 ms
16,904 KB
testcase_17 AC 284 ms
16,904 KB
testcase_18 AC 159 ms
16,944 KB
testcase_19 AC 185 ms
17,112 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)>;
  using H = function<E(E,E)>;
  Int n,height;
  F f;
  G g;
  H h;
  T ti;
  E ei;
  vector<T> dat;
  vector<E> laz;
  SegmentTree(F f,G g,H h,T ti,E ei):
    f(f),g(g),h(h),ti(ti),ei(ei){}
  
  void init(Int n_){
    n=1;height=0;
    while(n<n_) n<<=1,height++;
    dat.assign(2*n,ti);
    laz.assign(2*n,ei);
  }
  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]);
  }
  inline T reflect(Int k){
    return laz[k]==ei?dat[k]:g(dat[k],laz[k]);
  }
  inline void eval(Int k){
    if(laz[k]==ei) return;
    laz[(k<<1)|0]=h(laz[(k<<1)|0],laz[k]);
    laz[(k<<1)|1]=h(laz[(k<<1)|1],laz[k]);
    dat[k]=reflect(k);
    laz[k]=ei;
  }
  inline void thrust(Int k){
    for(Int i=height;i;i--) eval(k>>i);
  }
  inline void recalc(Int k){    
    while(k>>=1)
      dat[k]=f(reflect((k<<1)|0),reflect((k<<1)|1));
  }
  void update(Int a,Int b,E x){
    thrust(a+=n);
    thrust(b+=n-1);
    for(Int l=a,r=b+1;l<r;l>>=1,r>>=1){
      if(l&1) laz[l]=h(laz[l],x),l++;
      if(r&1) --r,laz[r]=h(laz[r],x);
    }
    recalc(a);
    recalc(b);
  }
  void set_val(Int a,T x){
    thrust(a+=n);
    dat[a]=x;laz[a]=ei;
    recalc(a);
  }
  T query(Int a,Int b){
    thrust(a+=n);
    thrust(b+=n-1);
    T vl=ti,vr=ti;
    for(Int l=a,r=b+1;l<r;l>>=1,r>>=1) {
      if(l&1) vl=f(vl,reflect(l++));
      if(r&1) vr=f(reflect(--r),vr);
    }
    return f(vl,vr);
  }
};

//INSERT ABOVE HERE
signed main(){
  Int n,q;
  cin>>n>>q;
  
  using P = pair<Int, Int>;
  auto f=[](P a,P b){return P(a.first+b.first,a.second+b.second);};
  auto g=[](P a,Int b){return P(b*a.second,a.second);};
  auto h=[](Int a,Int b){a++;return b;};

  P ti(0,0);
  Int ei=-1;
  SegmentTree<P, Int> segA(f,g,h,ti,ei);
  SegmentTree<P, Int> segB(f,g,h,ti,ei);

  vector<P> vp(n,P(0,1));
  segA.build(vp);
  segB.build(vp);

  Int sa=0,sb=0;
  for(Int i=0;i<q;i++){
    Int x,l,r;
    cin>>x>>l>>r;
    r++;
    if(x==0){
      Int a=segA.query(l,r).first;
      Int b=segB.query(l,r).first;
      if(a<b) sb+=b;
      if(a>b) sa+=a;
    }
    if(x==1){
      segA.update(l,r,1);
      segB.update(l,r,0);
    }
    if(x==2){
      segA.update(l,r,0);
      segB.update(l,r,1);
    }
  }
  
  sa+=segA.query(0,n).first;
  sb+=segB.query(0,n).first;
  cout<<sa<<" "<<sb<<endl;  
  return 0;
}
0