結果

問題 No.879 Range Mod 2 Query
ユーザー 0w10w1
提出日時 2019-10-11 06:12:38
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,904 bytes
コンパイル時間 2,392 ms
コンパイル使用メモリ 210,060 KB
実行使用メモリ 18,476 KB
最終ジャッジ日時 2023-08-15 10:39:24
合計ジャッジ時間 6,681 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 9 ms
17,896 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 244 ms
18,272 KB
testcase_20 AC 244 ms
18,136 KB
testcase_21 AC 263 ms
18,204 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

template<int n>
struct Segt {
  int psum[n << 2][2];
  int64_t vsum[n << 2];
  int64_t atag[n << 2];
  int ptag[n << 2];
  Segt(vector<int> a) {
    a.resize(n);
    function<void(int, int, int)> build = [&](int lb, int rb, int t) {
      atag[t] = 0;
      ptag[t] = 0;
      if (rb - lb == 1) {
        psum[t][a[lb] & 1] = 1;
        psum[t][~a[lb] & 1] = 0;
        vsum[t] = a[lb];
        return;
      }
      int mb = lb + rb >> 1;
      build(lb, mb, t << 1);
      build(mb, rb, t << 1 | 1);
      pull(t);
    };
    build(0, n, 1);
  }
  void pull(int t) {
    for (int i = 0; i < 2; ++i) {
      psum[t][i] = psum[t << 1][i] + psum[t << 1 | 1][i];
    }
    vsum[t] = vsum[t << 1] + vsum[t << 1 | 1];
  }
  void push(int lb, int rb, int t) {
    int mb = lb + rb >> 1;
    if (ptag[t]) {
      for (int c = 0; c < 2; ++c) {
        vsum[t << 1 | c] = psum[t << 1 | c][1];
        atag[t << 1 | c] = 0;
        ptag[t << 1 | c] = 1;
      }
      ptag[t] = 0;
    }
    if (atag[t]) {
      vector<int> len({ mb - lb, rb - mb });
      for (int c = 0; c < 2; ++c) {
        vsum[t << 1 | c] += 1LL * len[c] * atag[t];
        atag[t << 1 | c] += atag[t];
        if (atag[t] & 1) swap(psum[t << 1 | c][0], psum[t << 1 | c][1]);
      }
      atag[t] = 0;
    }
  }
  void mod2(int ql, int qr, int lb = 0, int rb = n, int t = 1) {
    if (qr <= lb || rb <= ql) return;
    if (ql <= lb && rb <= qr) {
      vsum[t] = psum[t][1];
      atag[t] = 0;
      ptag[t] = 1;
      return;
    }
    push(lb, rb, t);
    int mb = lb + rb >> 1;
    mod2(ql, qr, lb, mb, t << 1);
    mod2(ql, qr, mb, rb, t << 1 | 1);
    pull(t);
  }
  void add(int v, int ql, int qr, int lb = 0, int rb = n, int t = 1) {
    if (qr <= lb || rb <= ql) return;
    if (ql <= lb && rb <= qr) {
      vsum[t] += 1LL * (rb - lb) * v;
      atag[t] += v;
      if (v & 1) swap(psum[t][0], psum[t][1]);
      return;
    }
    push(lb, rb, t);
    int mb = lb + rb >> 1;
    add(v, ql, qr, lb, mb, t << 1);
    add(v, ql, qr, mb, rb, t << 1 | 1);
    pull(t);
  }
  int64_t query(int ql, int qr, int lb = 0, int rb = n, int t = 1) {
    if (qr <= lb || rb <= ql) return 0;
    if (ql <= lb && rb <= qr) return vsum[t];
    push(lb, rb, t);
    int mb = lb + rb >> 1;
    return query(ql, qr, lb, mb, t << 1) + query(ql, qr, mb, rb, t << 1 | 1);
  }
};

int main() {
  ios::sync_with_stdio(false);

  int N, Q;
  cin >> N >> Q;

  vector<int> A(N);
  for (int i = 0; i < N; ++i) {
    cin >> A[i];
  }

  static Segt<(1 << 17)> tree(A);

  while (Q--) {
    int P;
    cin >> P;

    if (P == 1) {
      int L, R;
      cin >> L >> R;
      tree.mod2(L - 1, R);
    } else if (P == 2) {
      int L, R, X;
      cin >> L >> R >> X;
      tree.add(X, L - 1, R);
    } else {
      int L, R;
      cin >> L >> R;
      cout << tree.query(L - 1, R) << endl;
    }
  }

  return 0;
}
0