Submission #953958

#TimeUsernameProblemLanguageResultExecution timeMemory
953958PringPort Facility (JOI17_port_facility)C++17
10 / 100
35 ms68188 KiB
#include <bits/stdc++.h> using namespace std; #ifdef MIKU string dbmc = "\033[1;38;2;57;197;187m", dbrs = "\033[0m"; #define debug(x...) cout << dbmc << "[" << #x << "]: ", dout(x) void dout() { cout << dbrs << endl; } template <typename T, typename ...U> void dout(T t, U ...u) { cout << t << (sizeof...(u) ? ", " : ""); dout(u...); } #else #define debug(...) 39 #endif #define int long long #define fs first #define sc second #define mp make_pair #define FOR(i, j, k) for (int i = j, Z = k; i < Z; i++) typedef pair<int, int> pii; const int MXN = 2000005, mod = 1000000007; int n, a[MXN]; pii p[MXN]; int N; bitset<MXN> b; vector<pii> st; void PCOUT(priority_queue<pii> &pq) { vector<pii> v; cout << "PQ: "; while (pq.size()) { cout << '<' << pq.top().fs << ' ' << pq.top().sc << "> "; v.push_back(pq.top()); pq.pop(); } cout << endl; for (auto &i : v) pq.push(i); } void MERGE(priority_queue<pii> &a, priority_queue<pii> &b) { if (a.size() < b.size()) swap(a, b); while (b.size()) { a.push(b.top()); b.pop(); } } void UPDATE(priority_queue<pii> &pq) { while (pq.size() && !b[pq.top().sc]) pq.pop(); } struct DSU { int p[MXN], nc; priority_queue<pii> pq[MXN]; void init(int n) { fill(p + 1, p + n + 1, -1); nc = n; } int find(int x) { return (p[x] < 0 ? x : p[x] = find(p[x])); } bool onion(int x, int y) { // debug("onion", x, y); x = find(x); y = find(y); // debug("onion", x, y); if (x == y) { // cout << "PRING" << '\n'; return false; } if (p[x] > p[y]) swap(x, y); p[x] += p[y]; p[y] = x; // debug("MERGE", x, y); MERGE(pq[x], pq[y]); nc--; return true; } } dsu; void ADD(int l, int id) { // debug("ADD", id); st.push_back(mp(l, id)); dsu.pq[id].push(mp(l, id)); b[id] = true; } void REMOVE(int l, int r, int id) { // debug("REM", id); while (st.size() && st.back().fs >= l) { auto [l2, id2] = st.back(); if (id2 != id) { dsu.onion(id, id2 + n); dsu.onion(id + n, id2); } st.pop_back(); } b[id] = false; UPDATE(dsu.pq[dsu.find(id)]); if (dsu.pq[dsu.find(id)].size()) st.push_back(dsu.pq[dsu.find(id)].top()); UPDATE(dsu.pq[dsu.find(id + n)]); if (dsu.pq[dsu.find(id + n)].size()) st.push_back(dsu.pq[dsu.find(id + n)].top()); } int POW(int a, int b) { int ans = 1; for (int i = 1 << 20; i > 0; i >>= 1) { ans = ans * ans % mod; if (i & b) ans = ans * a % mod; } return ans; } int miku() { cin >> n; N = n * 2; FOR(i, 1, n + 1) { cin >> p[i].fs >> p[i].sc; a[p[i].fs] = i; a[p[i].sc] = -i; } dsu.init(N); FOR(i, 1, N + 1) { if (a[i] > 0) { ADD(i, a[i]); } else { REMOVE(p[-a[i]].fs, p[-a[i]].sc, -a[i]); } // for (auto &i : st) cout << i.fs << ' ' << i.sc << endl; } FOR(i, 1, n) if (dsu.find(i) == dsu.find(i + n)) return 0; return POW(2, dsu.nc / 2); } int32_t main() { cin.tie(0) -> sync_with_stdio(false); cin.exceptions(cin.failbit); cout << miku() << '\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...