Submission #671861

#TimeUsernameProblemLanguageResultExecution timeMemory
671861tengiz05Port Facility (JOI17_port_facility)C++17
22 / 100
4716 ms1048576 KiB
#include <bits/stdc++.h> using i64 = long long; constexpr int P = 1E9 + 7; int main() { std::ios::sync_with_stdio(false); std::cin.tie(NULL); int n; std::cin >> n; std::vector<std::pair<int, int>> a(n); std::vector<int> p(2 * n), id(2 * n); for (int i = 0; i < n; i++) { std::cin >> a[i].first >> a[i].second; a[i].first--; a[i].second--; p[a[i].first] = a[i].second; p[a[i].second] = a[i].first; id[a[i].first] = id[a[i].second] = i; } int m = 2 * n; std::vector mx(21, std::vector<int>(m)); for (int i = 0; i < m; i++) { mx[0][i] = p[i]; } for (int j = 0; (1 << (j + 1)) <= m; j++) { for (int i = 0; i + (1 << (j + 1)) <= m; i++) { if (mx[j][i] > mx[j][i + (1 << j)]) { mx[j + 1][i] = mx[j][i]; } else { mx[j + 1][i] = mx[j][i + (1 << j)]; } } } auto query = [&](int l, int r) { int k = std::__lg(r - l); if (mx[k][l] > mx[k][r - (1 << k)]) { return mx[k][l]; } else { return mx[k][r - (1 << k)]; } }; std::vector<bool> vis(n); std::vector<std::vector<int>> adj(n); for (int i = 0; i < m; i++) { if (!vis[id[i]]) { std::function<void(int)> dfs = [&](int u) { vis[id[u]] = true; int l = u + 1; int r = p[u]; while (l < r) { int k = query(l, r); k = p[k]; if (p[k] > r) { if (l < k && query(l, k) > r) { std::cout << "0\n"; std::exit(0); } adj[id[u]].push_back(id[k]); adj[id[k]].push_back(id[u]); dfs(k); l = k + 1; } else { break; } } }; dfs(i); } } std::vector<int> color(n); vis.assign(n, false); int c = 0; for (int i = 0; i < n; i++) { if (!vis[i]) { c++; std::function<void(int)> dfs = [&](int u) { vis[u] = true; for (int v : adj[u]) { if (!vis[v]) { color[v] = color[u] ^ 1; dfs(v); } } }; dfs(i); } } std::stack<int> st[2]; for (int i = 0; i < m; i++) { if (p[i] > i) { st[color[id[i]]].push(id[i]); } else { if (st[color[id[i]]].top() != id[i]) { std::cout << "0\n"; return 0; } st[color[id[i]]].pop(); } } int ans = 1; for (int i = 0; i < c; i++) { ans = 2 * ans % P; } std::cout << ans << "\n"; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...