제출 #512608

#제출 시각아이디문제언어결과실행 시간메모리
512608fatemetmhrPort Facility (JOI17_port_facility)C++17
0 / 100
12 ms23884 KiB
// ~Be name khoda~ // #include<bits/stdc++.h> using namespace std; typedef long long ll; #define pb push_back #define mp make_pair #define all(x) x.begin(), x.end() #define fi first #define se second #define cl clear #define endll '\n' const int maxn = 1e6 + 10; const int maxn5 = 1e6 + 10; const int maxnt = 4e6 + 10; const int maxn3 = 1e3 + 10; const int mod = 1e9 + 7; const ll inf = 2e18; vector <int> have, adj2[maxn5]; int ty[maxn5], a[maxn5], b[maxn5]; int per1[maxn5], per2[maxn5]; int per1c[maxn5], per2c[maxn5]; bool mark[maxn5]; int mx[maxnt], mn[maxnt], val1[maxn5], val2[maxn5]; vector <pair<int, int>> av; set <pair<int, int>> seg; int n; inline bool cmp3(int i, int j){return a[i] < a[j];} inline bool cmp4(int i, int j){return b[i] < b[j];} inline int best(int i, int j){ if(i == -1) return j; if(j == -1) return i; if(val1[i] < val1[j]) return j; return i; } inline int worst(int i, int j){ if(i == -1) return j; if(j == -1) return i; if(val2[i] > val2[j]) return j; return i; } inline void build1(int l, int r, int v){ if(r - l == 1){ mx[v] = per1[l]; return; } int mid = (l + r) >> 1; build1(l, mid, v * 2); build1(mid, r, v * 2 + 1); mx[v] = best(mx[v * 2], mx[v * 2 + 1]); return; } inline void build2(int l, int r, int v){ if(r - l == 1){ mn[v] = per2[l]; return; } int mid = (l + r) >> 1; build2(l, mid, v * 2); build2(mid, r, v * 2 + 1); mn[v] = worst(mn[v * 2], mn[v * 2 + 1]); return; } inline int get_max(int l, int r, int lq, int rq, int v){ if(rq <= l || r <= lq) return -1; if(lq <= l && r <= rq) return mx[v]; int mid = (l + r) >> 1; int ans1 = get_max(l, mid, lq, rq, v * 2); int ans2 = get_max(mid, r, lq, rq, v * 2 + 1); //cout << "ok " << l << ' ' << r << ' ' << ans1 << ' ' << ans2 << endl; return best(ans1, ans2); } inline int get_min(int l, int r, int lq, int rq, int v){ if(rq <= l || r <= lq) return -1; if(lq <= l && r <= rq) return mn[v]; int mid = (l + r) >> 1; int ans1 = get_min(l, mid, lq, rq, v * 2); int ans2 = get_min(mid, r, lq, rq, v * 2 + 1); //cout << "comn " << l << ' ' << r << ' ' << ans1 << ' ' << ans2 << endl; return worst(ans1, ans2); } inline void upd1(int l, int r, int ind, int v){ if(r - l == 1) return; int mid = (l + r) >> 1; if(ind < mid) upd1(l, mid, ind, v * 2); else upd1(mid, r, ind, v * 2 + 1); mx[v] = best(mx[v * 2], mx[v * 2 + 1]); //cout << "aha " << l << ' ' << r << ' ' << ind << ' ' << mx[v] << endl; return; } inline void upd2(int l, int r, int ind, int v){ if(r - l == 1) return; int mid = (l + r) >> 1; if(ind < mid) upd2(l, mid, ind, v * 2); else upd2(mid, r, ind, v * 2 + 1); mn[v] = worst(mn[v * 2], mn[v * 2 + 1]); //cout << "aha " << l << ' ' << r << ' ' << ind << ' ' << mn[v] << endl; return; } inline int idof1(int val){ int p = lower_bound(per1c, per1c + n, val) - per1c; return p; } inline int idof2(int val){ int p = lower_bound(per2c, per2c + n, val) - per2c; return p; } inline void dfs2(int v){ mark[v] = true; //cout << "ahay " << v << endl; int l = idof1(a[v] + 1), r = idof1(b[v] + 1); while(true){ int p = get_max(0, n, l, r, 1); //cout <<"hey " << p << ' ' << val1[p] << ' ' << l << ' ' << r << endl; if(p == -1 || val1[p] < b[v]) break; assert(v != p); adj2[p].pb(v); adj2[v].pb(p); val1[p] = -1; upd1(0, n, idof1(a[p]), 1); } l = idof2(a[v]); r = idof2(b[v]); while(true){ int p = get_min(0, n, l, r, 1); //cout << "hey2 " << p << ' ' << val2[p] << ' ' << l << ' ' << r << endl; if(p == -1 || val2[p] > a[v]) break; assert(v != p); adj2[p].pb(v); adj2[v].pb(p); val2[p] = maxn5; upd2(0, n, idof2(b[p]), 1); } for(auto u : adj2[v]){ //cout << "ajab " << v << ' ' << u << endl; if(!mark[u]) dfs2(u); } return; } inline void dfs(int v){ mark[v] = true; for(auto u : adj2[v]) if(!mark[u]){ ty[u] = (ty[v] ^ 1); dfs(u); } return; } inline void check(){ av.clear(); //cout << "starting ... " << endl; for(auto i : have){ mark[i] = false; av.pb({a[i], i}); av.pb({b[i], i}); //cout << "aha " << i << endl; } sort(all(av)); seg.clear(); for(auto [u, i] : av){ if(u == a[i]){ seg.insert({a[i], i}); } else{ seg.erase({a[i], i}); auto it = seg.lower_bound(mp(a[i], 0)); if(it != seg.end()){ assert(true); cout << 0 << endl; exit(0); } } } return; } int main() { ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); cin >> n; for(int i = 0; i < n; i++){ cin >> a[i] >> b[i]; a[i]--; b[i]--; per1[i] = i; per2[i] = i; val1[i] = b[i]; val2[i] = a[i]; } sort(per1, per1 + n, cmp3); sort(per2, per2 + n, cmp4); for(int i = 0; i < n; i++){ per1c[i] = a[per1[i]]; per2c[i] = b[per2[i]]; } build1(0, n, 1); build2(0, n, 1); for(int i = 0; i < n; i++) if(!mark[i]){ dfs2(i); } fill(mark, mark + n + 10, false); int cnt = 0; for(int i = 0; i < n; i++) if(!mark[i]){ ty[i] = 0; dfs(i); cnt++; } for(int i = 0; i < n; i++) if(ty[i] == 0){ have.pb(i); } check(); have.clear(); for(int i = 0; i < n; i++) if(ty[i] == 1) have.pb(i); check(); ll ans = 1; while(cnt--){ ans *= 2; ans %= mod; } cout << ans << endl; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...