結果

問題 No.876 Range Compress Query
ユーザー tonetone
提出日時 2019-09-27 18:13:02
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 263 ms / 2,000 ms
コード長 2,108 bytes
コンパイル時間 804 ms
コンパイル使用メモリ 82,772 KB
実行使用メモリ 8,288 KB
最終ジャッジ日時 2023-10-25 01:09:58
合計ジャッジ時間 3,891 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 3 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 3 ms
4,348 KB
testcase_04 AC 2 ms
4,348 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 3 ms
4,348 KB
testcase_09 AC 3 ms
4,348 KB
testcase_10 AC 3 ms
4,348 KB
testcase_11 AC 256 ms
8,024 KB
testcase_12 AC 216 ms
8,024 KB
testcase_13 AC 217 ms
8,024 KB
testcase_14 AC 255 ms
8,024 KB
testcase_15 AC 181 ms
8,024 KB
testcase_16 AC 251 ms
8,288 KB
testcase_17 AC 250 ms
8,288 KB
testcase_18 AC 263 ms
8,288 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