結果

問題 No.876 Range Compress Query
ユーザー tonegawatonegawa
提出日時 2020-08-20 14:26:11
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 259 ms / 2,000 ms
コード長 2,108 bytes
コンパイル時間 999 ms
コンパイル使用メモリ 89,024 KB
実行使用メモリ 8,276 KB
最終ジャッジ日時 2024-04-21 10:43:46
合計ジャッジ時間 4,278 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 3 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 4 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 3 ms
5,376 KB
testcase_07 AC 3 ms
5,376 KB
testcase_08 AC 3 ms
5,376 KB
testcase_09 AC 3 ms
5,376 KB
testcase_10 AC 3 ms
5,376 KB
testcase_11 AC 241 ms
7,892 KB
testcase_12 AC 202 ms
8,024 KB
testcase_13 AC 198 ms
7,892 KB
testcase_14 AC 240 ms
8,020 KB
testcase_15 AC 173 ms
8,020 KB
testcase_16 AC 243 ms
8,016 KB
testcase_17 AC 230 ms
8,276 KB
testcase_18 AC 259 ms
8,020 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
#include <vector>
#include <deque>
#include <queue>
#include <algorithm>
#include <set>
#include <map>
#include <bitset>
#define vv(a, b, c, d) vector<vector<d> >(a, vector<d>(b, c))
#define vvi std::vector<std::vector<int> >
#define vvl std::vector<std::vector<ll> >
#define MODs 1000000007;
typedef long long int ll;
using namespace std;
//-------------------------------------------------------------------
ll M=1;
vector<ll> seg, ad;
void init(ll N){
  while(M<N) M*=2;
  M=M*2-1;
  for(int i=0;i<M;i++) {
    seg.push_back(0);
    ad.push_back(0);
  }
}
void update(ll l, ll r, ll x, ll k){
  k+=(M+1)/2-1;
  seg[k] = x;
  while(k>0){
    k = (k-1)/2;
    seg[k] = seg[k*2+1] + seg[k*2+2];
  }
}
void add(ll a, ll b, ll l, ll r, ll x, ll k){
  if(r<=a||b<=l) return;
  if(a<=l&&r<=b) ad[k]+=x;
  else{
    add(a, b, l, (l+r)/2, x, k*2+1);
    add(a, b, (l+r)/2, r, x, k*2+2);
  }
}
ll get_add(ll k){
  k+=(M+1)/2-1;
  ll ret = ad[k];
  while(k>0){
    k = (k-1)/2;
    ret += ad[k];
  }
  return ret;
}
ll query(ll a, ll b, ll l, ll r, ll k){
  if(r<=a || b<=l) return 0;
  if(a<=l && r<=b) return seg[k];
  ll A = query(a, b, l, (l+r)/2, k*2+1);
  ll B = query(a, b, (l+r)/2, r, k*2+2);
  return A + B;
}
//-------------------------------------------------------------------

int main(int argc, char const *argv[]) {
  ll N, Q, a, b, c, d;
  std::cin >> N >> Q;
  init(N);
  std::vector<ll> A(N);
  for(int i=0;i<N;i++)  std::cin >> A[i];
  for(int i=0;i<N-1;i++) if(A[i]!=A[i+1]) update(0, (M+1)/2, 1, i);

  for(int i=0;i<Q;i++){
    std::cin >> a;
    if(a == 2) {
      std::cin >> b >> c;
      if(b!=c){
        std::cout << query(b-1, c-1, 0, (M+1)/2, 0) + 1<< '\n';
      }else std::cout << 1 << '\n';
    }else {
      std::cin >> b >> c >> d;
      add(b-1, c, 0, (M+1)/2, d, 0);
      if(b-1!=0&&get_add(b-2)+A[b-2]!=get_add(b-1)+A[b-1]) update(0, (M+1)/2, 1, b-2);
      else update(0, (M+1)/2, 0, b-2);
      if(c-1!=N-1&&get_add(c-1)+A[c-1]!=get_add(c)+A[c]) update(0, (M+1)/2, 1, c-1);
      else update(0, (M+1)/2, 0, c-1);
    }
  }
  return 0;
}
0