結果
問題 | No.1673 Lamps on a line |
ユーザー | keijak |
提出日時 | 2021-09-10 21:31:57 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 81 ms / 2,000 ms |
コード長 | 6,394 bytes |
コンパイル時間 | 2,347 ms |
コンパイル使用メモリ | 208,068 KB |
実行使用メモリ | 31,488 KB |
最終ジャッジ日時 | 2024-06-11 22:37:52 |
合計ジャッジ時間 | 3,566 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,376 KB |
testcase_02 | AC | 14 ms
5,376 KB |
testcase_03 | AC | 13 ms
5,376 KB |
testcase_04 | AC | 23 ms
5,376 KB |
testcase_05 | AC | 3 ms
5,376 KB |
testcase_06 | AC | 20 ms
5,376 KB |
testcase_07 | AC | 69 ms
29,568 KB |
testcase_08 | AC | 15 ms
28,004 KB |
testcase_09 | AC | 61 ms
9,344 KB |
testcase_10 | AC | 81 ms
17,152 KB |
testcase_11 | AC | 17 ms
17,536 KB |
testcase_12 | AC | 69 ms
31,488 KB |
ソースコード
#include <bits/stdc++.h> #define REP_(i, a_, b_, a, b, ...) \ for (int i = (a), END_##i = (b); i < END_##i; ++i) #define REP(i, ...) REP_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__) #define ALL(x) std::begin(x), std::end(x) using i64 = long long; template<typename T, typename U> inline bool chmax(T &a, U b) { return a < b and ((a = std::move(b)), true); } template<typename T, typename U> inline bool chmin(T &a, U b) { return a > b and ((a = std::move(b)), true); } template<typename T> inline int ssize(const T &a) { return (int) std::size(a); } template<typename T> std::istream &operator>>(std::istream &is, std::vector<T> &a) { for (auto &x: a) is >> x; return is; } template<typename T, typename U> std::ostream &operator<<(std::ostream &os, const std::pair<T, U> &a) { return os << "(" << a.first << ", " << a.second << ")"; } template<typename Container> std::ostream &print_seq(const Container &a, std::string_view sep = " ", std::string_view ends = "\n", std::ostream &os = std::cout) { auto b = std::begin(a), e = std::end(a); for (auto it = std::begin(a); it != e; ++it) { if (it != b) os << sep; os << *it; } return os << ends; } template<typename T, typename = void> struct is_iterable : std::false_type {}; template<typename T> struct is_iterable<T, std::void_t<decltype(std::begin(std::declval<T>())), decltype(std::end(std::declval<T>()))>> : std::true_type { }; template<typename T, typename = std::enable_if_t< is_iterable<T>::value && !std::is_same<T, std::string_view>::value && !std::is_same<T, std::string>::value>> std::ostream &operator<<(std::ostream &os, const T &a) { return print_seq(a, ", ", "", (os << "{")) << "}"; } void print() { std::cout << "\n"; } template<class T> void print(const T &x) { std::cout << x << "\n"; } template<typename Head, typename... Tail> void print(const Head &head, Tail... tail) { std::cout << head << " "; print(tail...); } struct Input { template<typename T> operator T() const { T x; std::cin >> x; return x; } } in; #ifdef MY_DEBUG #include "debug_dump.hpp" #else #define DUMP(...) #endif using namespace std; template<typename LazyMonoid> struct LazySegTree { using T = typename LazyMonoid::T; using F = typename LazyMonoid::F; inline int size() const { return n_; } inline int offset() const { return offset_; } explicit LazySegTree(int n) : LazySegTree(std::vector<T>(n, LazyMonoid::id())) {} explicit LazySegTree(const std::vector<T> &v) : n_(int(v.size())) { offset_ = 1; for (bits_ = 0; offset_ < n_; ++bits_) { offset_ <<= 1; } data_.assign(2 * offset_, LazyMonoid::id()); lazy_ = std::vector<F>(offset_, LazyMonoid::f_id()); for (int i = 0; i < n_; i++) data_[offset_ + i] = v[i]; for (int i = offset_ - 1; i >= 1; i--) { update(i); } } void set(int p, T x) { assert(0 <= p && p < n_); p += offset_; // Update the leaf. for (int i = bits_; i >= 1; i--) push(p >> i); data_[p] = x; // Update its ancestors. for (int i = 1; i <= bits_; i++) update(p >> i); } const T &operator[](int p) const { assert(0 <= p && p < n_); p += offset_; for (int i = bits_; i >= 1; i--) push(p >> i); return data_[p]; } T fold(int l, int r) const { assert(0 <= l && l <= r && r <= n_); if (l == r) return LazyMonoid::id(); l += offset_; r += offset_; for (int i = bits_; i >= 1; i--) { if (((l >> i) << i) != l) push(l >> i); if (((r >> i) << i) != r) push(r >> i); } T sml = LazyMonoid::id(), smr = LazyMonoid::id(); while (l < r) { if (l & 1) sml = LazyMonoid::op(sml, data_[l++]); if (r & 1) smr = LazyMonoid::op(data_[--r], smr); l >>= 1; r >>= 1; } return LazyMonoid::op(sml, smr); } T fold_all() const { return data_[1]; } void apply(int p, F f) { assert(0 <= p && p < n_); p += offset_; for (int i = bits_; i >= 1; i--) push(p >> i); data_[p] = LazyMonoid::apply(f, data_[p]); for (int i = 1; i <= bits_; i++) update(p >> i); } void apply(int l, int r, F f) { assert(0 <= l && l <= r && r <= n_); if (l == r) return; l += offset_; r += offset_; for (int i = bits_; i >= 1; i--) { if (((l >> i) << i) != l) push(l >> i); if (((r >> i) << i) != r) push((r - 1) >> i); } { int l2 = l, r2 = r; while (l < r) { if (l & 1) apply_all(l++, f); if (r & 1) apply_all(--r, f); l >>= 1; r >>= 1; } l = l2; r = r2; } for (int i = 1; i <= bits_; i++) { if (((l >> i) << i) != l) update(l >> i); if (((r >> i) << i) != r) update((r - 1) >> i); } } friend std::ostream &operator<<(std::ostream &os, const LazySegTree &st) { os << "["; for (int i = 0; i < st.size(); ++i) { if (i != 0) os << ", "; const auto &x = st[i]; os << x; } return os << "]"; } void update(int k) { data_[k] = LazyMonoid::op(data_[2 * k], data_[2 * k + 1]); } void apply_all(int k, F f) const { data_[k] = LazyMonoid::f_apply(f, data_[k]); if (k < offset_) lazy_[k] = LazyMonoid::f_compose(f, lazy_[k]); } void push(int k) const { apply_all(2 * k, lazy_[k]); apply_all(2 * k + 1, lazy_[k]); lazy_[k] = LazyMonoid::f_id(); } int n_, offset_, bits_; mutable std::vector<T> data_; mutable std::vector<F> lazy_; }; struct XorSumOp { struct T { long long count; int width; // NOTE: Must be initialized with width=1! }; using F = long long; // Fold: Sum static T op(const T &x, const T &y) { return {x.count + y.count, x.width + y.width}; } static constexpr T id() { return {0, 0}; } // Update: Add static T f_apply(const F &f, const T &x) { if (f == 0) return x; return {x.width - x.count, x.width}; } static F f_compose(const F &f, const F &g) { return f ^ g; } static constexpr F f_id() { return 0; } }; int main() { ios_base::sync_with_stdio(false), cin.tie(nullptr); int n = in, Q = in; vector<XorSumOp::T> init(n); REP(i, n) { init[i] = {0, 1}; } LazySegTree<XorSumOp> seg(init); REP(qi, Q) { int l = in, r = in; --l; seg.apply(l, r, 1); print(seg.fold_all().count); } }