結果

問題 No.876 Range Compress Query
ユーザー cureskolcureskol
提出日時 2019-10-30 16:16:59
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 202 ms / 2,000 ms
コード長 3,422 bytes
コンパイル時間 1,658 ms
コンパイル使用メモリ 172,200 KB
実行使用メモリ 5,840 KB
最終ジャッジ日時 2023-10-12 23:49:59
合計ジャッジ時間 5,362 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 3 ms
4,352 KB
testcase_04 AC 2 ms
4,352 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 3 ms
4,348 KB
testcase_07 AC 3 ms
4,348 KB
testcase_08 AC 2 ms
4,352 KB
testcase_09 AC 3 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 198 ms
5,428 KB
testcase_12 AC 169 ms
5,292 KB
testcase_13 AC 165 ms
5,472 KB
testcase_14 AC 196 ms
5,276 KB
testcase_15 AC 139 ms
5,452 KB
testcase_16 AC 192 ms
5,688 KB
testcase_17 AC 191 ms
5,840 KB
testcase_18 AC 202 ms
5,472 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

template <typename T>
struct SegmentTree{
  using F = function<T(T,T)>;
  int n;  //セグ木の幅
  F f;  //モノイドの作用
  T ti;  //初期値
  vector<T> dat;  //セグ木で使う配列 一番上に使われるのがdat[1] 一番下の段の左端がdat[n]
  SegmentTree(){};
  SegmentTree(F f,T ti):f(f),ti(ti){}  //fがモノイドとしての作用、tiが単位元

  void init(int n_){  //ただの初期化
    n=1;
    while(n<n_) n<<=1;  //nが実際にとる2べきの幅
    dat.assign(n<<1,ti);  //dat配列は幅の2倍あってその全てにtiを代入
  }

  void build(const vector<T> &v){  //vector vに合わせた初期化
    int n_=v.size();
    init(n_);
    for(int i=0;i<n_;i++) dat[n+i]=v[i];  //v[i]を一番子の所に代入
    for(int i=n-1;i;i--)
      dat[i]=f(dat[(i<<1)|0],dat[(i<<1)|1]);  //iの子供は2*i、2*i+1
  }

  void set_val(int k,T x){  //(0-indexedで)kにxを代入
    dat[k+=n]=x;
    while(k>>=1)  //kがどんどん自分の親になっていく、0になったらおしまい
      dat[k]=f(dat[(k<<1)|0],dat[(k<<1)|1]);
  }

  T query(int a,int b){  //[a,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);  //右端〃偶数の時(開区間なのでr:奇数で反応)
    }
    return f(vl,vr);
  }

  template<typename C>
  int find(int st,C &check,T &acc,int k,int l,int r){  //dat[k]:[l,r)の間にいるenを返す
    //下のfind用なので先にそっち読むべき
    //accはst~lとかで確定している値
    if(l+1==r){  //答えがちゃんと存在するならこのlが答え
      acc=f(acc,dat[k]);
      return check(acc)?k-n:-1; //入るならk-n(==l)を、入らないなら-1を返す
    }
    int m=(l+r)>>1;  //にぶたんのそれ
    if(m<=st) return find(st,check,acc,(k<<1)|1,m,r);  //mがstより後ろなら明らかにl=m;
    if(st<=l&&!check(f(acc,dat[k]))){  //今見てるk全部でもダメ(mだと足りないのでl=mにする)
      acc=f(acc,dat[k]);  //kは全部accに加えた上で-1を返す
      return -1;
    }
    int vl=find(st,check,acc,(k<<1)|0,l,m);  //まず[l,m)の中にenがいるか探す
    if(~vl) return vl;  //いたならそれを返す(if(~vl)は-1以外にのみ反応する)
    return find(st,check,acc,(k<<1)|1,m,r);  //いなかったら[m,r)の中にenがいるのでそれを返す
    //acc=f(acc,dat[k]);がここで効いてる
  }

  template<typename C>
  int find(int st,C &check){
    //check(f(dat[st]~dat[en-1]))=false,check(f(dat[st]~dat[en]))=trueとなるenを返す
    T acc=ti;
    return find(st,check,acc,1,0,n);
  }
};

signed main(){
  int n,q;cin>>n>>q;
  vector<int> v(n),w(n-1),u(n-1);
  for(int i=0;i<n;i++)cin>>v[i];
  for(int i=0;i<n-1;i++)w[i]=v[i+1]-v[i];
  for(int i=0;i<n-1;i++)u[i]=!!w[i];
  auto f=[](int a,int b){return a+b;};
  SegmentTree<int> seg(f,0);
  seg.build(u);
  vector<int> ans;
  while(q--){
    int c;cin>>c;
    if(c==1){
      int l,r,x;cin>>l>>r>>x;
      if(l>1)w[l-2]+=x,seg.set_val(l-2,!!w[l-2]);
      if(r<n)w[r-1]-=x,seg.set_val(r-1,!!w[r-1]);
    }
    else{
      int l,r;cin>>l>>r;
      ans.push_back(seg.query(--l,--r)+1);
    }
  }
  for(int p:ans)cout<<p<<endl;
}
0