結果

問題 No.876 Range Compress Query
ユーザー tnakao0123tnakao0123
提出日時 2019-09-07 19:23:32
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 66 ms / 2,000 ms
コード長 2,306 bytes
コンパイル時間 591 ms
コンパイル使用メモリ 84,972 KB
実行使用メモリ 5,388 KB
最終ジャッジ日時 2023-09-09 08:47:48
合計ジャッジ時間 2,719 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,408 KB
testcase_01 AC 2 ms
4,528 KB
testcase_02 AC 2 ms
4,480 KB
testcase_03 AC 2 ms
4,540 KB
testcase_04 AC 2 ms
4,420 KB
testcase_05 AC 2 ms
4,496 KB
testcase_06 AC 3 ms
4,536 KB
testcase_07 AC 2 ms
4,408 KB
testcase_08 AC 3 ms
4,512 KB
testcase_09 AC 2 ms
4,452 KB
testcase_10 AC 2 ms
4,484 KB
testcase_11 AC 65 ms
5,072 KB
testcase_12 AC 54 ms
5,104 KB
testcase_13 AC 54 ms
5,284 KB
testcase_14 AC 64 ms
5,164 KB
testcase_15 AC 46 ms
5,352 KB
testcase_16 AC 62 ms
5,208 KB
testcase_17 AC 63 ms
5,244 KB
testcase_18 AC 66 ms
5,388 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 876.cc:  No.876 Range Compress Query - yukicoder
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_N = 100000;
const int MAX_E2 = 1 << 18; // = 262144

/* typedef */

typedef long long ll;

template <typename T, const int MAX_E2>
struct SegTreeSum {
  int n, e2;
  T nodes[MAX_E2];
  SegTreeSum() {}

  void init(int _n) {
    n = _n;
    for (e2 = 1; e2 < n; e2 <<= 1);
    memset(nodes, 0, sizeof(nodes));
  }

  T &get(int i) { return nodes[e2 - 1 + i]; }

  void set(int i, T v) {
    int j = e2 - 1 + i;
    nodes[j] = v;
    while (j > 0) {
      j = (j - 1) / 2;
      nodes[j] = nodes[j * 2 + 1] + nodes[j * 2 + 2];
    }
  }

  void setall() {
    for (int j = e2 - 2; j >= 0; j--)
      nodes[j] = nodes[j * 2 + 1] + nodes[j * 2 + 2];
  }

  T sum_range(int r0, int r1, int k, int i0, int i1) {
    if (r1 <= i0 || i1 <= r0) return 0;
    if (r0 <= i0 && i1 <= r1) return nodes[k];

    int im = (i0 + i1) / 2;
    T v0 = sum_range(r0, r1, k * 2 + 1, i0, im);
    T v1 = sum_range(r0, r1, k * 2 + 2, im, i1);
    return v0 + v1;
  }
  T sum_range(int r0, int r1) { return sum_range(r0, r1, 0, 0, e2); }
};

/* global variables */

SegTreeSum<int,MAX_E2> st;
ll ds[MAX_N];

/* subroutines */

inline int g(ll d) { return (d == 0) ? 0 : 1; }

/* main */

int main() {
  int n, qn;
  scanf("%d%d", &n, &qn);

  st.init(n - 1);

  int pa;
  scanf("%d", &pa);

  for (int i = 0; i + 1 < n; i++) {
    int ai;
    scanf("%d", &ai);
    ds[i] = ai - pa;
    st.get(i) = g(ds[i]);
    pa = ai;
  }
  st.setall();

  while (qn--) {
    int op, l, r;
    scanf("%d%d%d", &op, &l, &r);
    l--, r--;

    if (op == 1) {
      int x;
      scanf("%d", &x);

      if (l > 0) {
	ds[l - 1] += x;
	int vl = g(ds[l - 1]);
	if (st.get(l - 1) != vl) st.set(l - 1, vl);
      }

      if (r < n - 1) {
	ds[r] -= x;
	int vr = g(ds[r]);
	if (st.get(r) != vr) st.set(r, vr);
      }
    }
    else {
      printf("%d\n", st.sum_range(l, r) + 1);
    }
  }
  return 0;
}
0