結果

問題 No.879 Range Mod 2 Query
ユーザー 👑 emthrmemthrm
提出日時 2019-09-08 18:15:32
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 389 ms / 3,000 ms
コード長 5,250 bytes
コンパイル時間 1,805 ms
コンパイル使用メモリ 134,472 KB
実行使用メモリ 14,824 KB
最終ジャッジ日時 2024-06-27 15:06:52
合計ジャッジ時間 6,714 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 3 ms
5,376 KB
testcase_02 AC 3 ms
5,376 KB
testcase_03 AC 3 ms
5,376 KB
testcase_04 AC 4 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 3 ms
5,376 KB
testcase_08 AC 3 ms
5,376 KB
testcase_09 AC 3 ms
5,376 KB
testcase_10 AC 3 ms
5,376 KB
testcase_11 AC 382 ms
13,796 KB
testcase_12 AC 213 ms
13,932 KB
testcase_13 AC 272 ms
13,928 KB
testcase_14 AC 245 ms
14,308 KB
testcase_15 AC 242 ms
9,448 KB
testcase_16 AC 356 ms
14,564 KB
testcase_17 AC 370 ms
14,824 KB
testcase_18 AC 389 ms
14,564 KB
testcase_19 AC 332 ms
14,436 KB
testcase_20 AC 335 ms
14,440 KB
testcase_21 AC 375 ms
14,440 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <chrono>
#define _USE_MATH_DEFINES
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;

#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()

const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-8;
const int MOD = 1000000007; // 998244353;
const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
/*-------------------------------------------------*/
template <typename Monoid, typename OperatorMonoid = Monoid>
struct LazySegTree {
  using MM = function<Monoid(Monoid, Monoid)>;
  using MOI = function<Monoid(Monoid, OperatorMonoid, int)>;
  using OO = function<OperatorMonoid(OperatorMonoid, OperatorMonoid)>;

  LazySegTree(int sz, const MM mm, const MOI moi, const OO oo, const Monoid &M_UNITY, const OperatorMonoid &O_UNITY)
    : mm(mm), moi(moi), oo(oo), M_UNITY(M_UNITY), O_UNITY(O_UNITY) {
    init(sz);
    dat.assign(n << 1, M_UNITY);
  }

  LazySegTree(const vector<Monoid> &a, const MM mm, const MOI moi, const OO oo, const Monoid &M_UNITY, const OperatorMonoid &O_UNITY)
    :  mm(mm), moi(moi), oo(oo), M_UNITY(M_UNITY), O_UNITY(O_UNITY) {
    int a_sz = a.size();
    init(a_sz);
    dat.resize(n << 1);
    REP(i, a_sz) dat[i + n] = a[i];
    for (int i = n - 1; i > 0; --i) dat[i] = mm(dat[i << 1], dat[(i << 1) + 1]);
  }

  void update(int a, int b, const OperatorMonoid &value) { update(a, b, value, 1, 0, n); }

  Monoid query(int a, int b) { return query(a, b, 1, 0, n); }

  Monoid operator[](const int idx) { return query(idx, idx + 1); }

private:
  int n = 1;
  const MM mm;
  const MOI moi;
  const OO oo;
  const Monoid M_UNITY;
  const OperatorMonoid O_UNITY;
  vector<Monoid> dat;
  vector<OperatorMonoid> lazy;
  vector<bool> need_to_be_eval;

  void init(int sz) {
    while (n < sz) n <<= 1;
    lazy.assign(n << 1, O_UNITY);
    need_to_be_eval.assign(n << 1, false);
  }

  inline void evaluate(int node, int len) {
    if (need_to_be_eval[node]) {
      dat[node] = moi(dat[node], lazy[node], len);
      if (node < n) {
        lazy[node << 1] = oo(lazy[node << 1], lazy[node]);
        need_to_be_eval[node << 1] = true;
        lazy[(node << 1) + 1] = oo(lazy[(node << 1) + 1], lazy[node]);
        need_to_be_eval[(node << 1) + 1] = true;
      }
      lazy[node] = O_UNITY;
      need_to_be_eval[node] = false;
    }
  }

  void update(int a, int b, const OperatorMonoid &value, int node, int left, int right) {
    evaluate(node, right - left);
    if (right <= a || b <= left) return;
    if (a <= left && right <= b) {
      lazy[node] = oo(lazy[node], value);
      need_to_be_eval[node] = true;
      evaluate(node, right - left);
    } else {
      update(a, b, value, node << 1, left, (left + right) >> 1);
      update(a, b, value, (node << 1) + 1, (left + right) >> 1, right);
      dat[node] = mm(dat[node << 1], dat[(node << 1) + 1]);
    }
  }

  Monoid query(int a, int b, int node, int left, int right) {
    evaluate(node, right - left);
    if (right <= a || b <= left) return M_UNITY;
    if (a <= left && right <= b) return dat[node];
    return mm(query(a, b, node << 1, left, (left + right) >> 1), query(a, b, (node << 1) + 1, (left + right) >> 1, right));
  }
};

int main() {
  cin.tie(nullptr); ios::sync_with_stdio(false);
  // freopen("input.txt", "r", stdin);

  struct Node {
    int even, odd;
    long long sum;
    Node() {}
    Node(int even, int odd, long long sum) : even(even), odd(odd), sum(sum) {}
  };
  struct Ope {
    long long pre, add;
    bool mod2;
    Ope() {}
    Ope(long long pre, bool mod2, long long add) : pre(pre), mod2(mod2), add(add) {}
  };
  auto mm = [](Node a, Node b) {
    return Node(a.even + b.even, a.odd + b.odd, a.sum + b.sum);
  };
  auto moi = [](Node a, Ope b, int len) {
    if (b.pre % 2 == 1) swap(a.even, a.odd);
    a.sum += b.pre * len;
    if (b.mod2) a.sum = a.odd;
    if (b.add % 2 == 1) swap(a.even, a.odd);
    a.sum += b.add * len;
    return a;
  };
  auto oo = [](Ope l, Ope v) {
    Ope res = l;
    if (v.mod2) {
      res.pre += res.add + v.pre;
      res.mod2 = true;
      res.add = v.add;
    } else {
      res.add += v.add;
    }
    return res;
  };

  int n, q; cin >> n >> q;
  vector<Node> a;
  REP(i, n) {
    int ai; cin >> ai;
    a.emplace_back(ai % 2 == 0, ai % 2 == 1, ai);
  }
  LazySegTree<Node, Ope> seg(a, mm, moi, oo, Node(0, 0, 0), Ope(0, false, 0));
  while (q--) {
    int query, l, r; cin >> query >> l >> r; --l; --r;
    if (query == 1) {
      seg.update(l, r + 1, Ope(0, true, 0));
    } else if (query == 2) {
      int x; cin >> x;
      seg.update(l, r + 1, Ope(0, false, x));
    } else {
      cout << seg.query(l, r + 1).sum << '\n';
    }
  }
  return 0;
}
0