Submission #512709

#TimeUsernameProblemLanguageResultExecution timeMemory
512709fatemetmhrPort Facility (JOI17_port_facility)C++17
100 / 100
3739 ms227624 KiB
// ~Be name khoda~ // #include<bits/stdc++.h> #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #pragma GCC target("avx,avx2,sse,sse2,fma") 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], adjj; 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 void get_max(int l, int r, int lq, int rq, int val, int v){ //cout << "aha " << l << ' ' << r << ' ' << lq << ' ' << rq << ' ' << val << endl; if(rq <= l || r <= lq || val1[mx[v]] < val) return; if(r - l == 1){ adjj.pb(per1[l]); val1[per1[l]] = -1; return; } int mid = (l + r) >> 1; get_max(l, mid, lq, rq, val, v * 2); get_max(mid, r, lq, rq, val, v * 2 + 1); mx[v] = best(mx[v * 2], mx[v * 2 + 1]); //cout << "ok " << l << ' ' << r << ' ' << ans1 << ' ' << ans2 << endl; return; } inline void get_min(int l, int r, int lq, int rq, int val, int v){ if(rq <= l || r <= lq || val2[mn[v]] > val) return; if(r - l == 1){ adjj.pb(per2[l]); val2[per2[l]] = 2 * maxn5; return; } int mid = (l + r) >> 1; get_min(l, mid, lq, rq, val, v * 2); get_min(mid, r, lq, rq, val, v * 2 + 1); mn[v] = worst(mn[v * 2], mn[v * 2 + 1]); //cout << "comn " << l << ' ' << r << ' ' << ans1 << ' ' << ans2 << 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); adjj.clear(); get_max(0, n, l, r, b[v], 1); for(auto u : adjj){ if(!mark[u]){ adj2[u].pb(v); adj2[v].pb(u); } } l = idof2(a[v]); r = idof2(b[v]); adjj.clear(); get_min(0, n, l, r, a[v], 1); for(auto u : adjj){ if(!mark[u]){ adj2[u].pb(v); adj2[v].pb(u); } } 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...