結果

問題 No.879 Range Mod 2 Query
ユーザー 0w10w1
提出日時 2019-10-11 19:48:12
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 2,710 bytes
コンパイル時間 2,408 ms
コンパイル使用メモリ 218,468 KB
実行使用メモリ 65,496 KB
最終ジャッジ日時 2024-11-25 03:23:26
合計ジャッジ時間 13,940 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
権限があれば一括ダウンロードができます
コンパイルメッセージ
In file included from /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/x86_64-pc-linux-gnu/bits/stdc++.h:95,
                 from main.cpp:1:
In member function 'std::size_t std::valarray<_Tp>::size() const [with _Tp = long int]',
    inlined from 'std::size_t std::__detail::_BinBase<_Oper, _FirstArg, _SecondArg>::size() const [with _Oper = std::__plus; _FirstArg = std::valarray<long int>; _SecondArg = std::valarray<long int>]' at /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/bits/valarray_before.h:550:49,
    inlined from 'std::size_t std::_Expr<_Clos, _Tp>::size() const [with _Clos = std::__detail::_BinClos<std::__plus, std::_ValArray, std::_ValArray, long int, long int>; _Tp = long int]' at /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/bits/valarray_after.h:256:29,
    inlined from 'std::valarray<_Tp>::valarray(const std::_Expr<_Dom, _Tp>&) [with _Dom = std::__detail::_BinClos<std::__plus, std::_ValArray, std::_ValArray, long int, long int>; _Tp = long int]' at /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/valarray:711:23,
    inlined from 'constexpr std::enable_if_t<is_invocable_r_v<_Res, _Callable, _Args ...>, _Res> std::__invoke_r(_Callable&&, _Args&& ...) [with _Res = valarray<long int>; _Callable = main()::<lambda(E, E)>&; _Args = {valarray<long int>, valarray<long int>}]' at /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/bits/invoke.h:116:38,
    inlined from 'static _Res std::_Function_handler<_Res(_ArgTypes ...), _Functor>::_M_invoke(const std::_Any_data&, _ArgTypes&& ...) [with _Res = std::valarray<long int>; _Functor = main()::<lambda(E, E)>; _ArgTypes = {std::valarray<long int>, std::valarray<long int>}]' at /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/bits/std_function.h:291:44:
/home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/valarray:938:14: warning: '<unnamed>.std::valarray<long int>::_M_size' is used uninitialized [-Wuninitialized]
  9

ソースコード

diff #

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

template<int n, typename E, typename T, typename FTE, typename FTT, typename FEE>
struct Segt {
  E dat[n << 2], ei;
  T tag[n << 2], ti;
  FTE fte;
  FTT ftt;
  FEE fee;
  Segt(FTE a, FTT b, FEE c, E ei, T ti)
      : fte(a), ftt(b), fee(c), ei(ei), ti(ti) {
    fill(dat, dat + 4 * n, ei);
    fill(tag, tag + 4 * n, ti);
  }
  void build(vector<E> a) {
    a.resize(n, E(3));
    function<void(int, int, int)> rec = [&](int lb, int rb, int k) {
      if (rb - lb == 1) return void(dat[k] = a[lb]);
      rec(lb, lb + rb >> 1, k << 1);
      rec(lb + rb >> 1, rb, k << 1 | 1);
      dat[k] = fee(dat[k << 1], dat[k << 1 | 1]);
    };
    rec(0, n, 1);
  }
  void push(int k) {
    dat[k] = fte(tag[k], dat[k]);
    for (int c = 0; c < 2; ++c) {
      tag[k << 1 | c] = ftt(tag[k], tag[k << 1 | c]);
    }
    tag[k] = ti;
  }
  void update(T t, int ql, int qr, int lb = 0, int rb = n, int k = 1) {
    if (qr <= lb || rb <= ql) return;
    push(k);
    if (ql <= lb && rb <= qr) {
      tag[k] = ftt(t, tag[k]);
      dat[k] = fte(t, dat[k]);
      return;
    }
    int mb = lb + rb >> 1;
    update(t, ql, qr, lb, mb, k << 1);
    update(t, ql, qr, mb, rb, k << 1 | 1);
    dat[k] = fee(dat[k << 1], dat[k << 1 | 1]);
  }
  E query(int ql, int qr, int lb = 0, int rb = n, int k = 1) {
    if (qr <= lb || rb <= ql) return ei;
    push(k);
    if (ql <= lb && rb <= qr) return dat[k];
    int mb = lb + rb >> 1;
    return fee(query(ql, qr, lb, mb, k << 1), query(ql, qr, mb, rb, k << 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];
  }

  typedef valarray<int64_t> T; // (mtag, stag, atag)
  typedef valarray<int64_t> E; // (vsum, cnt0, cnt1)
  function<E(T, E)> fte = [](T f, E e) {
    if (f[1]) swap(e[1], e[2]);
    if (f[0]) e[0] = e[2];
    e[0] += f[2] * (e[1] + e[2]);
    if (f[2] & 1) swap(e[1], e[2]);
    return e;
  };
  function<T(T, T)> ftt = [](T f, T t) { return T({ f[0] | t[0], f[1] + t[1] & 1, f[2] + (f[0] ? 0 : t[2]) }); };
  function<E(E, E)> fee = [](E l, E r) { return l + r; };
  static Segt<(1 << 17), E, T, decltype(fte), decltype(ftt), decltype(fee)> tree(fte, ftt, fee, T(3), E(3));

  vector<E> ea(N);
  transform(A.begin(), A.end(), ea.begin(), [&](int v) { return E({ v, ~v & 1, v & 1 }); });
  tree.build(ea);

  while (Q--) {
    int P, L, R;
    cin >> P >> L >> R;
    --L;
    if (P == 1) {
      tree.update(T({ 1, 0, 0 }), L, R);
    } else if (P == 2) {
      int X;
      cin >> X;
      tree.update(T({ 0, 0, X }), L, R);
    } else {
      cout << tree.query(L, R)[0] << endl;
    }
  }

  return 0;
}
0