제출 #656097

#제출 시각아이디문제언어결과실행 시간메모리
656097mosaevPort Facility (JOI17_port_facility)C++17
0 / 100
56 ms122344 KiB
#include <bits/stdc++.h> #include <set> #include <map> using namespace std; #define ll long long #define N (ll)(1e6+5) #define NP (2097155ll) #define M ((ll)1e9+7) #define fi first #define se second ll ans; int n, ft[N], s[N], t[N * 2], grp[N], p[N], cnt; vector<int> v[N]; map<int, int> seg[NP], st; int fent[3][N]; bool seen[N]; ll gp(ll x) { return x == p[x] ? p[x] : p[x] = gp(p[x]); } inline void get(ll l, ll r) { l += 1048575ll, r += 1048575ll; st.clear(); while (l <= r) { if (l & 1) { for (auto x : seg[l]) { st[p[x.first]] |= x.second; }l++; } if (!(r & 1)) { for (auto x : seg[r]) { st[p[x.first]] |= x.second; }r--; } l >>= 1, r >>= 1; } } inline void upd(ll x, ll a, ll b, ll c) { x += 1048575ll; while (x > 0) { seg[x].erase(a); seg[x][b] |= c; x >>= 1; } } inline void uf(ll wef, ll x, ll y) { while (x < N) fent[wef][x] += y, x += x & (-x); } inline void merge(ll a, ll b, ll c) { c &= grp[a]; a = p[a]; if (v[a].size() < v[b].size()) swap(a, b); for (auto x : v[b]) { p[x] = a; v[a].push_back(x); if (c) uf(grp[x], ft[x], -1), grp[x] ^= 3, uf(grp[x], ft[x], +1); }v[b].clear(); cnt--; } ll gf(ll wef, ll x, ll y) { ll ret = 0; x--; while (x > 0) ret -= fent[wef][x], x -= x & (-x); while (y > 0) ret += fent[wef][y], y -= y & (-y); return ret; } int main() { #define int ll ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); ll a, b; cin >> n; for (int i = 1; i <= n; i++) cin >> a >> b, ft[i] = b, t[a] = t[b] = i, p[i] = i, v[i].push_back(i), grp[i] = 1; cnt = n; b = 1; for (int i = 1; i <= 2 * n; i++) { a = t[i]; if (ft[a] == i) ft[a] = b++; else s[a] = b; } for (int i = 1; i <= 2 * n; i++) { a = t[i]; if (seen[a]) continue; seen[a] = true; get(s[a], ft[a]); if (gf(1, s[a], ft[a]) > 0 && gf(2, s[a], ft[a]) > 0) return cout << 0 << '\n', 0; uf(grp[a], ft[a], +1); for (auto x : st) { merge(a, x.first, x.second); } upd(ft[a], 0, p[a], grp[a]); } ans = 1; for (int i = 1; i <= cnt; i++) ans <<= 1, ans %= M; cout << ans; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...