結果

問題 No.879 Range Mod 2 Query
ユーザー ngtkanangtkana
提出日時 2019-09-07 20:53:40
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 9,138 bytes
コンパイル時間 2,628 ms
コンパイル使用メモリ 213,284 KB
実行使用メモリ 11,776 KB
最終ジャッジ日時 2024-06-27 04:21:44
合計ジャッジ時間 7,973 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 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 294 ms
11,648 KB
testcase_20 AC 287 ms
11,648 KB
testcase_21 AC 317 ms
11,548 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define loop(n) for (auto ngtkana_is_genius = 0; ngtkana_is_genius < int(n); ngtkana_is_genius++)
#define rep(i, begin, end) for (auto i = int(begin); i < int(end); i++)
#define all(v) v.begin(), v.end()
#define lint long long
auto cmn = [](auto& a, auto b){if (a > b) {a = b; return true;} return false;};
auto cmx = [](auto& a, auto b){if (a < b) {a = b; return true;} return false;};
void debug_impl() { std::cerr << std::endl; }
template <typename Head, typename... Tail>
void debug_impl(Head head, Tail... tail){
  std::cerr << " " << head;
  debug_impl(tail...);
}
#define debug(...)\
  std::cerr << std::boolalpha << "[" << #__VA_ARGS__ << "]:";\
  debug_impl(__VA_ARGS__);\
  std::cerr << std::noboolalpha;

template<
  typename Value1,    typename Value2,
  typename BinaryOp1, typename BinaryOp2, typename BinaryOp3,
  typename UnaryOp1,  typename UnaryOp2
  >
class lazy_segment_tree {
    struct node {
      int id, l, r;
      node (int id, int l, int r):
        id(id), l(l), r(r)
        {};

      auto size () const {return r - l;}

      auto left_child  () const {
        assert(size() > 1);
        return node(id * 2, l, (l + r) / 2);
      }

      auto right_child () const {
        assert(size() > 1);
        return node(id * 2 + 1, (l + r) / 2, r);
      }
    };

    int                 sz, n, N;
    BinaryOp1           op1;
    BinaryOp2           op2;
    BinaryOp3           op3;
    Value1              id1;
    Value2              id2;
    UnaryOp1            expand;
    UnaryOp2            shrink;
    std::vector<Value1> table;
    std::vector<Value2> lazy;
    node                initial_node;

    auto& op1_eq (Value1& x, Value1 y) {return x = op1(x, y);}
    auto& op2_eq (Value1& x, Value2 y) {return x = op2(x, y);}
    auto& op3_eq (Value2& x, Value2 y) {return x = op3(x, y);}

    void merge (int u)
      { table.at(u) = op1(table.at(2 * u), table.at(2 * u + 1)); }

    auto prop (int u) {
      op2_eq(table.at(u), lazy.at(u));
      if (u < n) {
        op3_eq(lazy.at(2 * u),     shrink(lazy.at(u)));
        op3_eq(lazy.at(2 * u + 1), shrink(lazy.at(u)));
      }
      lazy.at(u) = id2;
      return table.at(u);
    }

    auto chain (int u) const {
      auto ret = std::vector<int>{};
      for (auto i = u; i > 0; i /= 2)
        { ret.emplace_back(i); }
      std::reverse(ret.begin(), ret.end());
      return ret;
    }

    auto query_base (int l, int r, Value2 val, const node& now) {
      prop(now.id);
      if (now.r <= l || r <= now.l) return id1;
      else if (l <= now.l && now.r <= r) {
        op3_eq(lazy.at(now.id), val);
        return prop(now.id);
      } else {
        auto ret =op1(
          query_base(l, r, shrink(val), now.left_child()),
          query_base(l, r, shrink(val), now.right_child())
        );
        merge(now.id);
        return ret;
      }
    }

  public:
    lazy_segment_tree(
      int sz,
      BinaryOp1  op1,
      BinaryOp2  op2,
      BinaryOp3  op3,
      Value1     id1,
      Value2     id2,
      UnaryOp1   expand,
      UnaryOp2   shrink
    ):
      sz    (sz),
      n     (std::pow(2, int(std::log2(sz)) + 1)),
      N     (n * 2),
      op1   (std::move(op1)),
      op2   (std::move(op2)),
      op3   (std::move(op3)),
      id1   (id1),
      id2   (id2),
      expand(std::move(expand)),
      shrink(std::move(shrink)),
      table (N, id1),
      lazy  (N, id2),
      initial_node(1, 0, n) {
        std::mt19937 mt(std::random_device{}());
        std::uniform_int_distribution<int> dist(1, 1'000'000);
        for (auto i = 0; i < 20; i++) {
          Value1 ex1 = {dist(mt), dist(mt), dist(mt)}, ex1_ = {dist(mt), dist(mt), dist(mt)};
          Value2 ex2 = {dist(mt), dist(mt)};
          assert(op1(ex1, id1)       == ex1);
          assert(op2(ex1, id2)       == ex1);
          assert(op3(ex2, id2)       == ex2);
          assert(shrink(expand(ex2)) == ex2);
          assert(op2(op1(ex1, ex1_), expand(ex2)) == op1(op2(ex1, ex2), op2(ex1_, ex2)));
        }
      }

    void set (int i, const Value1 x) {
      assert(0 <= i && i < sz);
      table.at(n + i) = x; }

    void set (const std::vector<Value1>& v) {
      assert((int)v.size() == sz);
      std::move(v.begin(), v.end(), table.begin() + n); }

    void build () {
      for (auto i = 1; i < n; i++)
        { prop(i); }
      for (auto i = n - 1; i >= 1; i--)
        { merge(i); }
    }

    void act (int l, int r, Value2 val) {
      for (auto i = 1; i < n; i *= 2)
        { val = expand(val); }
      query_base(l, r, val, initial_node);
    }

    auto query (int l, int r)
      { return query_base(l, r, id2, initial_node); }

    auto quiet_at (int i) const {
      i += n;
      auto actor = id2;
      for (auto j : chain(i)) {
        actor = shrink(actor);
        actor = op3(actor, lazy.at(j));
      }
      return op2(table.at(i), actor);
    }

    auto quiet_collect () const {
      auto ret = std::vector<Value1>(sz);
      for (auto i = 0; i < sz; i++)
        { ret.at(i) = quiet_at(i); }
      return ret;
    }

    auto& at (int i) {
      i += n;
      for (auto j : chain(i))
        { prop(j); }
      return table.at(i);
    }

    auto collect () {
      build();
      auto ret = std::vector<Value1>(sz);
      for (auto i = 0; i < sz; i++)
        { ret.at(i) = table.at(i + n); }
      return ret;
    }
};

template<
  typename Value1,    typename Value2,
  typename BinaryOp1, typename BinaryOp2, typename BinaryOp3,
  typename UnaryOp1,  typename UnaryOp2
  >
auto make_lazy_segment_tree(
  int        size,
  BinaryOp1  op1,
  BinaryOp2  op2,
  BinaryOp3  op3,
  Value1     id1,
  Value2     id2,
  UnaryOp1   expand,
  UnaryOp2   shrink
)
{
  return lazy_segment_tree<
    Value1,    Value2,
    BinaryOp1, BinaryOp2, BinaryOp3,
    UnaryOp1,  UnaryOp2
    >(
      size, std::move(op1), std::move(op2), std::move(op3),
      id1, id2, std::move(expand), std::move(shrink)
    );
}


template<
  typename Value1,    typename Value2,
  typename BinaryOp1, typename BinaryOp2, typename BinaryOp3
  >
auto make_lazy_segment_tree(
  int        size,
  BinaryOp1  op1,
  BinaryOp2  op2,
  BinaryOp3  op3,
  Value1     id1,
  Value2     id2
)
{
  auto f = [](auto x){ return x; };
  return make_lazy_segment_tree(
      size, std::move(op1), std::move(op2), std::move(op3),
      id1, id2, std::move(f), std::move(f)
    );
}

template <typename T>
std::istream& operator>> (std::istream& is, std::vector<T>& v) {
  for (auto & x : v) is >> x;
  return is;
}

template <typename T>
std::ostream& operator<< (std::ostream& os, const std::vector<T>& v) {
  auto n = v.size();
  os << "{";
  for (size_t i = 0; i < n; i++)
    {os << (i > 0 ? "," : "") << v.at(i);}
  return os << "}";
}

template <typename T, typename U>
std::ostream& operator<< (std::ostream& os, const std::pair<T, U>& pair){
  return os << "(" << pair.first << "," << pair.second << ")";
}

template <typename T, typename U>
std::istream& operator>> (std::iostream& is, std::pair<T, U>& pair) {
  return is >> pair.first >> pair.second;
}

struct node
  { long long sum; int even, odd;};

std::ostream& operator<< (std::ostream& os, node const& x)
  { return os << "{" << x.sum << "," << x.even << "," << x.odd << "}"; }

bool operator== (node const& x, node const& y)
  { return x.sum == y.sum && x.even == y.even && x.odd == y.odd; }

int main() {
  std::cin.tie(0); std::cin.sync_with_stdio(false);
  int n, q; std::cin >> n >> q;
  auto act = [&] (auto& x, auto y) {
    x.sum += (x.even + x.odd) * y;
    if (y % 2 == 1) std::swap(x.even, x.odd);
  };
  auto segtree = make_lazy_segment_tree<node, std::pair<lint, lint>>(
    n,
    [](auto x, auto y) { return node{x.sum + y.sum, x.even + y.even, x.odd + y.odd}; },
    [&act](auto x, auto p) {
      act(x, p.second);
      if (p.first == -1) return x;
      x.sum = x.odd;
      act(x, p.first);
      return x;
    },
    [](auto p, auto q) {
      lint a, b, c, d; std::tie(a, b) = p, std::tie(c, d) = q;
      if (a == -1 && c == -1) return std::make_pair(-1LL, b + d);
      if (a == -1 && c != -1) return std::make_pair(b + c, d);
      if (a != -1 && c == -1) return std::make_pair(a, b + d);
      if (a != -1 && c != -1) return std::make_pair(a, b + c + d);
      assert(false);
      return std::pair<lint, lint>{};
    },
    node{0LL, 0, 0},
    std::make_pair(-1LL, 0LL)
  );
  rep(i, 0, n) {
    int a; std::cin >> a;
    int odd = a % 2;
    segtree.set(i, node{a, 1 - odd, odd});
  }
  segtree.build();
  // debug(segtree.quiet_collect());
  loop(q) {
    int c; std::cin >> c;
    // debug("query", c);
    if (c == 1) {
      int l, r; std::cin >> l >> r;
      l--;
      // debug(l, r);
      segtree.act(l, r, std::make_pair(0LL, 0LL));
    } else if (c == 2) {
      int l, r; lint x;
      std::cin >> l >> r >> x;
      l--;
      // debug(l, r, x);
      segtree.act(l, r, std::make_pair(-1LL, x));
    } else {
      int l, r; std::cin >> l >> r;
      l--;
      // debug(l, r);
      auto ret = segtree.query(l, r);
      std::cout << ret.sum << std::endl;
    }
    // debug(segtree.quiet_collect());
  }
  return 0;
}
0