結果

問題 No.876 Range Compress Query
ユーザー Imperi_NightImperi_Night
提出日時 2019-11-10 15:44:36
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 63 ms / 2,000 ms
コード長 3,262 bytes
コンパイル時間 948 ms
コンパイル使用メモリ 102,584 KB
実行使用メモリ 5,940 KB
最終ジャッジ日時 2023-10-13 07:46:40
合計ジャッジ時間 3,051 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,372 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,372 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,372 KB
testcase_07 AC 2 ms
4,372 KB
testcase_08 AC 2 ms
4,372 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,372 KB
testcase_11 AC 59 ms
5,604 KB
testcase_12 AC 50 ms
5,616 KB
testcase_13 AC 51 ms
5,664 KB
testcase_14 AC 59 ms
5,624 KB
testcase_15 AC 45 ms
5,752 KB
testcase_16 AC 60 ms
5,880 KB
testcase_17 AC 59 ms
5,940 KB
testcase_18 AC 63 ms
5,864 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <assert.h>
#include <limits.h>
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
 
using ll = long long;
using P = std::pair<ll, ll>;
 
#define rep(i, a, b) for (ll(i) = (a); i < (b); i++)
#define all(i) i.begin(), i.end()
#define debug(i) std::cerr << "debug " << #i<<" "<<i << std::endl
 
template <typename T1, typename T2>
std::ostream& operator<<(std::ostream& os, std::pair<T1, T2> pa) {
  return os << pa.first << " " << pa.second;
}
 
template <typename T>
std::ostream& operator<<(std::ostream& os, std::vector<T> vec) {
  for (int i = 0; i < vec.size(); i++)os << vec[i] << (i + 1 == vec.size() ? "" : " ");
  return os;
}
 
template<typename T1,typename T2>
inline bool chmax(T1& a,T2 b){return a<b && (a=b,true);}
 
template<typename T1,typename T2>
inline bool chmin(T1& a,T2 b){return a>b && (a=b,true);}
 
ll pow_mod(ll a, ll b, ll mod=-1) {
  if ((a == 0)||(mod!=-1&&a%mod==0)) {
    return 0;
  }
 
  ll x = 1;
 
  while (b > 0) {
    if (b & 1) {
      x = (mod!=-1)?(x * a) % mod:x*a;
    }
    a = (mod!=-1)?(a * a) % mod:a*a;
    b >>= 1;
  }
  return x;
}
 
// const ll MOD = 998244353;
const ll MOD = 1e9 + 7;

//非再帰抽象セグ木 0-indexed 単位元はiで指定
template <typename T,typename F>
class SegmentTree {
 private:
  long long n;
  T init;
  std::vector<T> dat;
  F fn;

 public:
  SegmentTree(long long i, T para, F fun)
      : init(para), fn(fun) {
    n = 1;
    while (n < i) {
      n *= 2;
    }
    dat = std::vector<T>(2 * n - 1, init);
  }

  // k番目(0-indexed)を値aで更新,dest=trueのときは更新前を破壊する
  void update(long long k, T a, bool dest) {
    k += n - 1;
    if (dest)
      dat[k] = a;
    else
      dat[k] = fn(dat[k], a);

    while (k > 0) {
      k = (k - 1) / 2;
      dat[k] = fn(dat[k * 2 + 1], dat[k * 2 + 2]);
    }
  }

  T query(long long a, long long b){
    assert(0<=a&&b<=n);
    T val=init;

    long long l=a+n-1,r=b+n-1;
    for(;l<r;l=(l>>1),r=(r>>1)){
      if(!(r&1)){
        r--;
        val=fn(val,dat[r]);
      }
      if(!(l&1)){
        val=fn(val,dat[l]);
        l++;
      }
    }

    return val;
  }
};
 
int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);

  ll n,q;
  std::cin>>n>>q;

  auto f=[](ll x,ll y){return x+y;};

  SegmentTree<ll,decltype(f)> a(n+2,0,f);

  std::vector<ll> diff(n);

  ll prev;
  std::cin>>prev;

  rep(i,0,n-1){
    ll temp;
    std::cin>>temp;
    diff[i]=temp-prev;
    a.update(i,(temp==prev)?0:1,true);
    prev=temp;
  }

  a.update(n-1,0,true);

  rep(i,0,q){
    ll query;
    std::cin>>query;
    if(query==1){
      ll l,r,x;
      std::cin>>l>>r>>x;
      l--;r--;
      if(l!=0){
        diff[l-1]+=x;
        a.update(l-1,(diff[l-1]!=0)?1:0,true);
      }
      if(r!=n-1){
        diff[r]-=x;
        a.update(r,(diff[r]!=0)?1:0,true);
      }

    }else{
      ll l,r;
      std::cin>>l>>r;
      l--;r--;
      if(l==r)std::cout<<1<<"\n";
      else std::cout<<a.query(l,r)+1<<"\n";
    }
  }

  return 0;
}
0