#include <bits/stdc++.h> using std::cin, std::cout, std::vector; void init_io() { cin.tie(nullptr); std::ios::sync_with_stdio(false); } struct Tree { bool leaf = false; const Tree *left = nullptr; const Tree *right = nullptr; }; const Tree the_leaf{true, nullptr, nullptr}; std::map<std::pair<const Tree*, const Tree*>, const Tree*> make_tree_cache; const Tree *make_tree(const Tree *const left, const Tree *const right) { if (left == nullptr && right == nullptr) return nullptr; const std::pair<const Tree*, const Tree*> key(left, right); const auto it = make_tree_cache.find(key); if (it != make_tree_cache.end()) return it->second; const Tree *const result = new Tree{false, left, right}; make_tree_cache[key] = result; return result; } std::map<std::pair<const Tree*, unsigned>, std::pair<const Tree*, const Tree*>> split_tree_cache; std::pair<const Tree*, const Tree*> split_tree(const Tree *const tree, unsigned depth) { if (tree == nullptr) return {nullptr, nullptr}; const std::pair<const Tree*, unsigned> key(tree, depth); const auto it = split_tree_cache.find(key); if (it != split_tree_cache.end()) return it->second; assert(!tree->leaf); std::pair<const Tree*, const Tree*> result; if (depth == 0) { result.first = tree->left; result.second = tree->right; } else { const auto left = split_tree(tree->left, depth-1); const auto right = split_tree(tree->right, depth-1); result.first = make_tree(left.first, right.first); result.second = make_tree(left.second, right.second); } split_tree_cache[key] = result; return result; } std::map<std::pair<const Tree*, const Tree*>, const Tree*> cross_product_tree_cache; const Tree* cross_product_tree(const Tree *const a, const Tree *const b) { if (a == nullptr || b == nullptr) return nullptr; const std::pair<const Tree*, const Tree*> key(a, b); const auto it = cross_product_tree_cache.find(key); if (it != cross_product_tree_cache.end()) return it->second; const Tree *result; if (a->leaf) { result = b; } else { result = make_tree(cross_product_tree(a->left, b), cross_product_tree(a->right, b)); } cross_product_tree_cache[key] = result; return result; } std::map<std::pair<const Tree*, const Tree*>, const Tree*> xor_tree_cache; const Tree* xor_tree(const Tree *const a, const Tree *const b) { if (a == b) return nullptr; if (a == nullptr) return b; if (b == nullptr) return a; const std::pair<const Tree*, const Tree*> key(a, b); const auto it = xor_tree_cache.find(key); if (it != xor_tree_cache.end()) return it->second; const Tree *result; if (a->leaf) { assert(b->leaf); result = nullptr; } else { assert(!b->leaf); result = make_tree(xor_tree(a->left, b->left), xor_tree(a->right, b->right)); } xor_tree_cache[key] = result; return result; } struct Component { vector<unsigned> indices; const Tree *tree = nullptr; bool contains(const unsigned a) const { return std::find(indices.begin(), indices.end(), a) != indices.end(); } std::pair<Component, Component> split(const unsigned a) const { vector<unsigned> new_indices = indices; const auto it = std::find(new_indices.begin(), new_indices.end(), a); assert(it != new_indices.end()); const unsigned depth = it - new_indices.begin(); new_indices.erase(it); const auto [left, right] = split_tree(tree, depth); return { Component{new_indices, left}, Component{new_indices, right}, }; } bool matches(const unsigned from, const unsigned to) const { const Tree *p = tree; if (p == nullptr) return false; for (const unsigned index : indices) { assert(!p->leaf); if (index >= from && index < to) { p = p->right; } else { p = p->left; } if (p == nullptr) return false; } assert(p->leaf); return true; } }; Component cross_product(const Component &a, const Component &b) { vector<unsigned> new_indices = a.indices; new_indices.insert(new_indices.end(), b.indices.begin(), b.indices.end()); return Component { new_indices, cross_product_tree(a.tree, b.tree) }; } Component component_xor(const Component &a, const Component &b) { assert(a.indices == b.indices); return Component{a.indices, xor_tree(a.tree, b.tree)}; } Component combine(const Component &a0, const Component &a1, const unsigned a) { assert(a0.indices == a1.indices); vector<unsigned> new_indices = a0.indices; new_indices.insert(new_indices.begin(), a); return Component{new_indices, make_tree(a0.tree, a1.tree)}; } class Desant { public: Desant(const unsigned n_): n(n_) { const Tree *const t = make_tree(&the_leaf, &the_leaf); for (unsigned i=0; i<n; ++i) { components.push_back(Component{{i}, t}); } } void process_order(const unsigned a, const unsigned b) { const Component ac = extract_component(a); Component ab00, ab01, ab10, ab11; if (ac.contains(b)) { const auto [a0, a1] = ac.split(a); const auto a0split = a0.split(b); ab00 = a0split.first; ab01 = a0split.second; const auto a1split = a1.split(b); ab10 = a1split.first; ab11 = a1split.second; } else { const Component bc = extract_component(b); const auto [a0, a1] = ac.split(a); const auto [b0, b1] = bc.split(b); ab00 = cross_product(a0, b0); ab01 = cross_product(a0, b1); ab10 = cross_product(a1, b0); ab11 = cross_product(a1, b1); } const Component new01 = component_xor(ab01, ab10); Component new10; new10.indices = ab00.indices; components.push_back(combine(combine(ab00, new01, b), combine(new10, ab11, b), a)); } Component extract_component(const unsigned a) { for (auto it = components.begin(); it != components.end(); ++it) { if (it->contains(a)) { Component result = *it; components.erase(it); return result; } } throw 0; } bool answer(const unsigned k) const { bool result = false; for (unsigned i = 0; i + k <= n; ++i) { result ^= answer_single_set(i, i+k); } return result; } bool answer_single_set(const unsigned from, const unsigned to) const { for (const Component &component: components) { if (!component.matches(from, to)) return false; } return true; } private: vector<Component> components; unsigned n; }; int main() { init_io(); unsigned n, num_orders; cin >> n >> num_orders; Desant desant(n); for (unsigned i=0; i<num_orders; ++i) { unsigned a, b; cin >> a >> b; --a; --b; desant.process_order(a, b); } for (unsigned k=1; k<=n; ++k) { const bool res = desant.answer(k); cout << (res ? '1' : '0') << ' '; } cout << "\n"; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 | #include <bits/stdc++.h> using std::cin, std::cout, std::vector; void init_io() { cin.tie(nullptr); std::ios::sync_with_stdio(false); } struct Tree { bool leaf = false; const Tree *left = nullptr; const Tree *right = nullptr; }; const Tree the_leaf{true, nullptr, nullptr}; std::map<std::pair<const Tree*, const Tree*>, const Tree*> make_tree_cache; const Tree *make_tree(const Tree *const left, const Tree *const right) { if (left == nullptr && right == nullptr) return nullptr; const std::pair<const Tree*, const Tree*> key(left, right); const auto it = make_tree_cache.find(key); if (it != make_tree_cache.end()) return it->second; const Tree *const result = new Tree{false, left, right}; make_tree_cache[key] = result; return result; } std::map<std::pair<const Tree*, unsigned>, std::pair<const Tree*, const Tree*>> split_tree_cache; std::pair<const Tree*, const Tree*> split_tree(const Tree *const tree, unsigned depth) { if (tree == nullptr) return {nullptr, nullptr}; const std::pair<const Tree*, unsigned> key(tree, depth); const auto it = split_tree_cache.find(key); if (it != split_tree_cache.end()) return it->second; assert(!tree->leaf); std::pair<const Tree*, const Tree*> result; if (depth == 0) { result.first = tree->left; result.second = tree->right; } else { const auto left = split_tree(tree->left, depth-1); const auto right = split_tree(tree->right, depth-1); result.first = make_tree(left.first, right.first); result.second = make_tree(left.second, right.second); } split_tree_cache[key] = result; return result; } std::map<std::pair<const Tree*, const Tree*>, const Tree*> cross_product_tree_cache; const Tree* cross_product_tree(const Tree *const a, const Tree *const b) { if (a == nullptr || b == nullptr) return nullptr; const std::pair<const Tree*, const Tree*> key(a, b); const auto it = cross_product_tree_cache.find(key); if (it != cross_product_tree_cache.end()) return it->second; const Tree *result; if (a->leaf) { result = b; } else { result = make_tree(cross_product_tree(a->left, b), cross_product_tree(a->right, b)); } cross_product_tree_cache[key] = result; return result; } std::map<std::pair<const Tree*, const Tree*>, const Tree*> xor_tree_cache; const Tree* xor_tree(const Tree *const a, const Tree *const b) { if (a == b) return nullptr; if (a == nullptr) return b; if (b == nullptr) return a; const std::pair<const Tree*, const Tree*> key(a, b); const auto it = xor_tree_cache.find(key); if (it != xor_tree_cache.end()) return it->second; const Tree *result; if (a->leaf) { assert(b->leaf); result = nullptr; } else { assert(!b->leaf); result = make_tree(xor_tree(a->left, b->left), xor_tree(a->right, b->right)); } xor_tree_cache[key] = result; return result; } struct Component { vector<unsigned> indices; const Tree *tree = nullptr; bool contains(const unsigned a) const { return std::find(indices.begin(), indices.end(), a) != indices.end(); } std::pair<Component, Component> split(const unsigned a) const { vector<unsigned> new_indices = indices; const auto it = std::find(new_indices.begin(), new_indices.end(), a); assert(it != new_indices.end()); const unsigned depth = it - new_indices.begin(); new_indices.erase(it); const auto [left, right] = split_tree(tree, depth); return { Component{new_indices, left}, Component{new_indices, right}, }; } bool matches(const unsigned from, const unsigned to) const { const Tree *p = tree; if (p == nullptr) return false; for (const unsigned index : indices) { assert(!p->leaf); if (index >= from && index < to) { p = p->right; } else { p = p->left; } if (p == nullptr) return false; } assert(p->leaf); return true; } }; Component cross_product(const Component &a, const Component &b) { vector<unsigned> new_indices = a.indices; new_indices.insert(new_indices.end(), b.indices.begin(), b.indices.end()); return Component { new_indices, cross_product_tree(a.tree, b.tree) }; } Component component_xor(const Component &a, const Component &b) { assert(a.indices == b.indices); return Component{a.indices, xor_tree(a.tree, b.tree)}; } Component combine(const Component &a0, const Component &a1, const unsigned a) { assert(a0.indices == a1.indices); vector<unsigned> new_indices = a0.indices; new_indices.insert(new_indices.begin(), a); return Component{new_indices, make_tree(a0.tree, a1.tree)}; } class Desant { public: Desant(const unsigned n_): n(n_) { const Tree *const t = make_tree(&the_leaf, &the_leaf); for (unsigned i=0; i<n; ++i) { components.push_back(Component{{i}, t}); } } void process_order(const unsigned a, const unsigned b) { const Component ac = extract_component(a); Component ab00, ab01, ab10, ab11; if (ac.contains(b)) { const auto [a0, a1] = ac.split(a); const auto a0split = a0.split(b); ab00 = a0split.first; ab01 = a0split.second; const auto a1split = a1.split(b); ab10 = a1split.first; ab11 = a1split.second; } else { const Component bc = extract_component(b); const auto [a0, a1] = ac.split(a); const auto [b0, b1] = bc.split(b); ab00 = cross_product(a0, b0); ab01 = cross_product(a0, b1); ab10 = cross_product(a1, b0); ab11 = cross_product(a1, b1); } const Component new01 = component_xor(ab01, ab10); Component new10; new10.indices = ab00.indices; components.push_back(combine(combine(ab00, new01, b), combine(new10, ab11, b), a)); } Component extract_component(const unsigned a) { for (auto it = components.begin(); it != components.end(); ++it) { if (it->contains(a)) { Component result = *it; components.erase(it); return result; } } throw 0; } bool answer(const unsigned k) const { bool result = false; for (unsigned i = 0; i + k <= n; ++i) { result ^= answer_single_set(i, i+k); } return result; } bool answer_single_set(const unsigned from, const unsigned to) const { for (const Component &component: components) { if (!component.matches(from, to)) return false; } return true; } private: vector<Component> components; unsigned n; }; int main() { init_io(); unsigned n, num_orders; cin >> n >> num_orders; Desant desant(n); for (unsigned i=0; i<num_orders; ++i) { unsigned a, b; cin >> a >> b; --a; --b; desant.process_order(a, b); } for (unsigned k=1; k<=n; ++k) { const bool res = desant.answer(k); cout << (res ? '1' : '0') << ' '; } cout << "\n"; } |