Submission #113635

#TimeUsernameProblemLanguageResultExecution timeMemory
113635aminraPort Facility (JOI17_port_facility)C++14
0 / 100
160 ms196088 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; const int MOD = (int)1e9 + 7; const int MAXN = (int)2e6 + 3; const int infint = (int)2e9; const ll inf = (ll)2e18; int a[MAXN], b[MAXN], id[MAXN], n, par[MAXN], sz; int get(int u) { return par[u] < 0 ? u : par[u] = get(par[u]); } void merge(int u, int v) { if((u = get(u)) == (v = get(v))) return; sz--; if(par[u] > par[v]) swap(u, v); par[u] += par[v]; par[v] = u; } vector<int> seg[4 * MAXN]; bool cyc; bool cmp(int u, int v) { return a[u] < a[v]; } void add(int node, int st, int en, int idx, int val) { for (auto u : seg[node]) merge(u, val); bool flg = seg[node].size() > 1; seg[node].clear(); if(flg) seg[node].push_back(val); if(en - st < 2) return; int mid = (st + en) >> 1; if(idx < mid) add(node << 1, st, mid, idx, val); else add(node << 1 | 1, mid, en, idx, val); return; } void ask(int node, int st, int en, int l, int r, int val) { if(l >= en || st >= r) return; if(l <= st && en <= r) { seg[node].push_back(val); return; } int mid = (st + en) >> 1; ask(node << 1, st, mid, l, r, val); ask(node << 1 | 1, mid, en, l, r, val); return; } int pwr(int a, int b) { if(b == 0) return 1; int c = pwr(a, b >> 1); c = 1LL * c * c % MOD; if(b & 1) c = 1LL * c * a % MOD; return c; } map<int, int> pos; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n; sz = n; for (int i = 0; i < n; i++) cin >> a[i] >> b[i]; memset(par, -1, sizeof par); for (int i = 0; i < n; i++) id[i] = i, a[i]--, b[i]--; sort(id, id + n, cmp); //check if there's odd cycle for (int i = 0; i < n; i++) { auto st = pos.lower_bound(a[id[i]]), ft = pos.lower_bound(b[id[i]]); while(st != ft) { merge(id[i], st -> second + n); merge(id[i] + n, st -> second); if(get(st -> second) == get((--ft) -> second)) break; ft++; st++; } pos[b[id[i]]] = id[i]; } for (int i = 0; i < n; i++) if(get(i) == get(i + n)) return cout << 0, 0; sz = n; memset(par, -1, sizeof par); for (int i = n - 1; i >= 0; i--) { add(1, 0, 2 * n, b[id[i]], id[i]); ask(1, 0, 2 * n, a[id[i]], b[id[i]] + 1, id[i]); } cout << pwr(2, sz); }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...