結果

問題 No.879 Range Mod 2 Query
ユーザー risujirohrisujiroh
提出日時 2019-09-06 23:17:29
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 872 ms / 3,000 ms
コード長 2,528 bytes
コンパイル時間 1,976 ms
コンパイル使用メモリ 171,048 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-07 03:46:29
合計ジャッジ時間 10,503 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 4 ms
4,380 KB
testcase_02 AC 5 ms
4,376 KB
testcase_03 AC 4 ms
4,380 KB
testcase_04 AC 5 ms
4,384 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 5 ms
4,376 KB
testcase_08 AC 4 ms
4,380 KB
testcase_09 AC 3 ms
4,376 KB
testcase_10 AC 3 ms
4,376 KB
testcase_11 AC 867 ms
4,380 KB
testcase_12 AC 482 ms
4,380 KB
testcase_13 AC 637 ms
4,376 KB
testcase_14 AC 553 ms
4,380 KB
testcase_15 AC 598 ms
4,380 KB
testcase_16 AC 809 ms
4,380 KB
testcase_17 AC 848 ms
4,376 KB
testcase_18 AC 872 ms
4,380 KB
testcase_19 AC 426 ms
4,380 KB
testcase_20 AC 425 ms
4,380 KB
testcase_21 AC 466 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using lint = long long;
template<class T = int> using V = vector<T>;
template<class T = int> using VV = V< V<T> >;

int main() {
  cin.tie(nullptr); ios::sync_with_stdio(false);
  int n, q; cin >> n >> q;
  V<lint> a(n); for (auto&& e : a) cin >> e;

  constexpr int B = 512;
  V<lint> sum((n + B - 1) / B);
  V<lint> oc((n + B - 1) / B);
  V<lint> ec((n + B - 1) / B);
  bitset<1000> bs;
  V<lint> ol((n + B - 1) / B, 1);
  V<lint> el((n + B - 1) / B);
  V<lint> laz((n + B - 1) / B);
  auto push = [&](int k) -> void {
    for (int i = k * B; i < min((k + 1) * B, n); ++i) {
      if (bs[k]) {
        a[i] = a[i] & 1 ? ol[k] : el[k];
      } else {
        a[i] += laz[k];
      }
    }
    bs[k] = false;
    ol[k] = 1;
    el[k] = 0;
    laz[k] = 0;
  };
  auto build = [&](int k) -> void {
    sum[k] = oc[k] = ec[k] = 0;
    for (int i = k * B; i < min((k + 1) * B, n); ++i) {
      sum[k] += a[i];
      oc[k] += a[i] & 1;
      ec[k] += ~a[i] & 1;
    }
  };
  auto act = [&](int l, int r, lint x) -> void {
    if (l % B) {
      push(l / B);
      while (l < r and l % B) {
        if (x == -1) a[l] &= 1;
        else a[l] += x;
        ++l;
      }
      build((l - 1) / B);
    }
    if (r % B) {
      push(r / B);
      while (l < r and r % B) {
        --r;
        if (x == -1) a[r] &= 1;
        else a[r] += x;
      }
      build(r / B);
    }
    while (l < r) {
      if (x == -1) {
        sum[l / B] = oc[l / B];
        bs[l / B] = true;
        ol[l / B] &= 1;
        el[l / B] &= 1;
      } else {
        sum[l / B] += (oc[l / B] + ec[l / B]) * x;
        ol[l / B] += x;
        el[l / B] += x;
        if (x & 1) {
          swap(oc[l / B], ec[l / B]);
        }
        laz[l / B] += x;
      }
      l += B;
    }
  };
  auto acc = [&](int l, int r) -> lint {
    lint res = 0;
    if (l % B) {
      push(l / B);
      while (l < r and l % B) {
        res += a[l];
        ++l;
      }
      build((l - 1) / B);
    }
    if (r % B) {
      push(r / B);
      while (l < r and r % B) {
        --r;
        res += a[r];
      }
      build(r / B);
    }
    while (l < r) {
      res += sum[l / B];
      l += B;
    }
    return res;
  };
  
  for (int k = 0; k < (n + B - 1) / B; ++k) {
    build(k);
  }
  while (q--) {
    int tp; cin >> tp;
    int l, r; cin >> l >> r, --l;
    if (tp == 1) {
      act(l, r, -1);
    } else if (tp == 2) {
      int x; cin >> x;
      act(l, r, x);
    } else {
      cout << acc(l, r) << '\n';
    }
  }
}
0