結果

問題 No.876 Range Compress Query
ユーザー beetbeet
提出日時 2019-08-06 19:11:13
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 1,864 bytes
コンパイル時間 2,117 ms
コンパイル使用メモリ 205,200 KB
実行使用メモリ 6,064 KB
最終ジャッジ日時 2023-08-25 23:53:29
合計ジャッジ時間 4,366 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 RE -
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 RE -
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 RE -
testcase_11 AC 63 ms
5,684 KB
testcase_12 AC 52 ms
5,648 KB
testcase_13 AC 53 ms
5,392 KB
testcase_14 AC 61 ms
5,744 KB
testcase_15 AC 44 ms
5,836 KB
testcase_16 AC 61 ms
6,064 KB
testcase_17 AC 63 ms
5,860 KB
testcase_18 AC 65 ms
6,008 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;}


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


template <typename T>
struct SegmentTree{
  using F = function<T(T,T)>;
  int n;
  F f;
  T ti;
  vector<T> dat;
  SegmentTree(){};
  SegmentTree(F f,T ti):f(f),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 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);
  }
};

//INSERT ABOVE HERE
signed main(){
  using ll = long long;

  int n,q;
  cin>>n>>q;
  vector<ll> as(n);
  for(int i=0;i<n;i++) cin>>as[i];

  vector<ll> bs(n-1);
  for(int i=0;i+1<n;i++) bs[i]=as[i]-as[i+1];

  auto f=[](int a,int b){return a+b;};
  SegmentTree<int> seg(f,0);
  vector<int> vs(n-1,0);
  for(int i=0;i+1<n;i++) vs[i]=bs[i]!=0;
  seg.build(vs);

  for(int i=0;i<q;i++){
    int t;
    cin>>t;
    if(t==1){
      int l,r,x;
      cin>>l>>r>>x;
      l--;
      if(l>0){
        bs[l-1]-=x;
        vs[l-1]=bs[l-1]!=0;
        seg.set_val(l-1,vs[l-1]);
      }
      {
        bs[r-1]+=x;
        vs[r-1]=bs[r-1]!=0;
        seg.set_val(r-1,vs[r-1]);
      }
    }
    if(t==2){
      int l,r;
      cin>>l>>r;
      l--;
      cout<<seg.query(l,r-1)+1<<"\n";
    }
  }
  cout<<flush;
  return 0;
}
0