結果

問題 No.879 Range Mod 2 Query
ユーザー risujirohrisujiroh
提出日時 2019-09-06 22:53:46
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,268 bytes
コンパイル時間 1,721 ms
コンパイル使用メモリ 174,176 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-07 03:39:03
合計ジャッジ時間 7,569 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 4 ms
4,380 KB
testcase_02 AC 5 ms
4,376 KB
testcase_03 AC 5 ms
4,384 KB
testcase_04 AC 5 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 4 ms
4,376 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 4 ms
4,376 KB
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 WA -
testcase_20 WA -
testcase_21 WA -
権限があれば一括ダウンロードができます

ソースコード

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);
  V<> b((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 (b[k] == 1) a[i] &= 1;
      if (b[k] == 2) a[i] = ~a[i] & 1;
      a[i] += laz[k];
    }
    b[k] = laz[k] = 0;
  };
  auto build = [&](int k) -> void {
    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];
        b[l / B] = 1;
        laz[l / B] = 0;
      } else {
        sum[l / B] += (oc[l / B] + ec[l / B]) * x;
        if (x & 1) swap(oc[l / B], ec[l / B]);
        
      }
      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