Submission #961369

#TimeUsernameProblemLanguageResultExecution timeMemory
961369abczzPort Facility (JOI17_port_facility)C++14
22 / 100
2552 ms1048576 KiB
#include <iostream> #include <vector> #include <array> #include <map> #define ll long long using namespace std; ll n, s, f = 1, X[1000000], F[1000000], M = 1e9 + 7; vector <ll> V, U, adj[1000000]; map <ll, ll> mp; bool B[1000000]; array <ll, 2> A[1000000], R[1000000]; void dfs(ll u, ll w) { F[u] = w; for (auto v : adj[u]) { if (F[v] == -1) { dfs(v, w^1); } else if (F[u] == F[v]) { cout << "0\n"; exit(0); } } } int main() { cin.tie(0); ios::sync_with_stdio(0); cin >> n; for (int i=0; i<n; ++i) { F[i] = -1; cin >> R[i][0] >> R[i][1]; --R[i][0], --R[i][1]; A[R[i][0]] = {i, 0}, A[R[i][1]] = {i, 1}; } for (int i=0; i<2*n; ++i) { if (!A[i][1]) { X[A[i][0]] = -1e18; auto it = mp.lower_bound(R[A[i][0]][1]); if (it != mp.begin()) { it = prev(it); X[A[i][0]] = it->first; } ++mp[R[A[i][0]][1]]; } } mp.clear(); for (int i=2*n-1; i>=0; --i) { if (A[i][1]) { auto it = mp.lower_bound(R[A[i][0]][0]); if (it != mp.end() && it->first < X[A[i][0]]) { cout << "0\n"; return 0; } ++mp[R[A[i][0]][0]]; } } for (int i=0; i<2*n; ++i) { if (A[i][1]) { B[A[i][0]] = 1; while (!V.empty()) { auto u = V.back(); if (R[u][0] < R[A[i][0]][0]) break; V.pop_back(); if (!B[u]) { //cout << u << " " << A[i][0] << endl; U.push_back(u); adj[u].push_back(A[i][0]); adj[A[i][0]].push_back(u); } } while (!U.empty()) { auto u = U.back(); U.pop_back(); V.push_back(u); } } else V.push_back(A[i][0]); } for (int i=0; i<n; ++i) { if (F[i] == -1) { f *= 2; f %= M; dfs(i, 0); } } cout << f << '\n'; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...