Submission #566366

#TimeUsernameProblemLanguageResultExecution timeMemory
566366hoanghq2004Alternating Current (BOI18_alternating)C++14
100 / 100
180 ms16568 KiB
#include <bits/stdc++.h> #pragma GCC optimize ("O3") #pragma GCC optimize ("unroll-loops") #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; using namespace std; template <typename T> using ordered_set = tree <T, null_type, less <T>, rb_tree_tag, tree_order_statistics_node_update>; const int N = 1e5 + 10; int n, m, k; vector <int> s[N], g[N]; int color[N]; int main() { ios :: sync_with_stdio(0); cin.tie(0); cin >> n >> m; vector <tuple <int, int, int>> events; set <int> s; for (int i = 1; i <= m; ++i) { int L, R; cin >> L >> R; if (L <= R) { events.push_back({L, -1, i}); events.push_back({R, 1, i}); } else { events.push_back({L, -1, i}); events.push_back({n, 1, i}); events.push_back({1, -1, i}); events.push_back({R, 1, i}); } } for (int i = 1; i <= n; ++i) events.push_back({i, 0, i}); sort(events.begin(), events.end()); set <int> c0, c1; for (auto [x, sign, i]: events) { if (sign == -1) { s.insert(i); } else if (sign == 1) { s.erase(i); } else { if (s.size() < 2) { cout << "impossible\n"; exit(0); } if (s.size() == 2) { int u = *s.begin(); int v = *(--s.end()); g[u].push_back(v); g[v].push_back(u); } } } function<int(int, int)> dfs = [&](int u, int c) { if (color[u] != -1) return (int)(color[u] == c); color[u] = c; if (s.find(u) != s.end()) { s.erase(u); if (c == 0) c0.insert(u); else c1.insert(u); } for (auto v: g[u]) if (!dfs(v, c ^ 1)) return 0; return 1; }; memset(color, -1, sizeof(color)); for (auto [x, sign, i]: events) { if (sign == -1) { if (color[i] == -1) s.insert(i); else if (color[i] == 0) c0.insert(i); else c1.insert(i); } else if (sign == 1) { s.erase(i); c0.erase(i); c1.erase(i); } else { if (c0.size() && c1.size()) continue; if (c0.empty()) { int u = *s.begin(); if (!dfs(u, 0)) { cout << "impossible"; exit(0); } } if (c1.empty()) { int u = *s.begin(); if (!dfs(u, 1)) { cout << "impossible"; exit(0); } } } } for (int i = 1; i <= m; ++i) if (color[i] == -1) color[i] = 0; for (int i = 1; i <= m; ++i) cout << color[i]; }

Compilation message (stderr)

alternating.cpp: In function 'int main()':
alternating.cpp:42:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   42 |     for (auto [x, sign, i]: events) {
      |               ^
alternating.cpp:75:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   75 |     for (auto [x, sign, i]: events) {
      |               ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...