結果

問題 No.876 Range Compress Query
ユーザー snow39snow39
提出日時 2019-09-06 22:41:11
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 319 ms / 2,000 ms
コード長 2,562 bytes
コンパイル時間 1,053 ms
コンパイル使用メモリ 100,360 KB
実行使用メモリ 16,712 KB
最終ジャッジ日時 2023-09-07 01:35:15
合計ジャッジ時間 4,726 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 3 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 3 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 3 ms
4,376 KB
testcase_07 AC 3 ms
4,380 KB
testcase_08 AC 3 ms
4,380 KB
testcase_09 AC 3 ms
4,380 KB
testcase_10 AC 3 ms
4,380 KB
testcase_11 AC 319 ms
16,188 KB
testcase_12 AC 260 ms
15,372 KB
testcase_13 AC 260 ms
15,432 KB
testcase_14 AC 314 ms
16,180 KB
testcase_15 AC 217 ms
15,424 KB
testcase_16 AC 302 ms
16,292 KB
testcase_17 AC 301 ms
16,308 KB
testcase_18 AC 318 ms
16,712 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cmath>
#include <map>
#include <queue>
#include <iomanip>
#include <set>
#include <tuple>
#define mkp make_pair
#define mkt make_tuple
#define rep(i,n) for(int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
const ll MOD=1e9+7;

class LazySegmentTree{
private:
  int n;
  vector<ll> node,lazy;

public:
  LazySegmentTree(vector<ll> v){
      n=1;
      while(n<v.size()) n*=2;
      node.resize(2*n-1,0);
      lazy.resize(2*n-1,0);
      
      for(int i=0;i<v.size();i++) node[i+n-1]=v[i];
      for(int i=n-2;i>=0;i--) node[i]=node[2*i+1]+node[2*i+2];
  }

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

  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]+=(r-l)*x;
          eval(k,l,r);
      }else{
          add(a,b,x,2*k+1,l,(r+l)/2);
          add(a,b,x,2*k+2,(r+l)/2,r);
          node[k]=node[2*k+1]+node[2*k+2];
      }
  }

  ll getSum(int a,int b,int k=0,int l=0,int r=-1){
      if(r<0) r=n;
      eval(k,l,r);
      if(b<=l||r<=a) return 0;
      if(a<=l&&r<=b) return node[k];

      ll vl,vr;
      vl=getSum(a,b,2*k+1,l,(l+r)/2);
      vr=getSum(a,b,2*k+2,(l+r)/2,r);
      return vl+vr;
  }
};

int N,Q;
vector<ll> A;
vector<ll> T,L,R,X;

int main(){
  cin>>N>>Q;
  A.resize(N);
  rep(i,N) cin>>A[i];
  L.resize(Q);
  R.resize(Q);
  T.resize(Q);
  X.resize(Q,0);
  rep(i,Q){
    cin>>T[i];
    if(T[i]==1) cin>>L[i]>>R[i]>>X[i];
    else cin>>L[i]>>R[i];
    L[i]--;R[i]--;
  }

  vector<ll> v(N,0);
  for(int i=0;i+1<N;i++) if(A[i]!=A[i+1]) v[i]=1;

  LazySegmentTree val(A),ind(v);
  for(int i=0;i<Q;i++){
    if(T[i]==1){
      ll prel=0,prea=0;
      ll prer=0,preb=0;
      if(L[i]-1>=0){
        prel=val.getSum(L[i],L[i]+1);
        prea=val.getSum(L[i]-1,L[i]);

        if(prel==prea) ind.add(L[i]-1,L[i],1);
        else if(prel!=prea&&prea==prel+X[i]) ind.add(L[i]-1,L[i],-1);
      }
      if(R[i]+1<N){
        prer=val.getSum(R[i],R[i]+1);
        preb=val.getSum(R[i]+1,R[i]+2);
        if(prer==preb) ind.add(R[i],R[i]+1,1);
        else if(prer!=preb&&preb==prer+X[i]) ind.add(R[i],R[i]+1,-1);
      }

      val.add(L[i],R[i]+1,X[i]);
    }else{
      int ans=ind.getSum(L[i],R[i])+1;
      cout<<ans<<endl;
    }
  }

  return 0;
}
0