제출 #550584

#제출 시각아이디문제언어결과실행 시간메모리
550584brunnorezendesPort Facility (JOI17_port_facility)C++17
0 / 100
1 ms340 KiB
#include <bits/stdc++.h> #define f first #define s second #define maxn 1000001 #define mod 1000000007 using namespace std; typedef pair<int,int> ii; typedef vector<ii> vii; typedef set<ii> sii; typedef long long int ll; int dsu[maxn], flag=0, opo[maxn], vis[maxn]; sii ini, fim; sii::iterator initial, last; vii truck, ordered; ll exp(ll a, int b){ ll x; if(b==1) return a; if(b&1) return (a*exp(a, b-1))%(ll)mod; else{ x = exp(a, b/2); return (x*x)%(ll)mod; } } int find(int x){ if(dsu[x]==x) return x; else{ dsu[x] = find(dsu[x]); return dsu[x]; } } void merge(int a, int b){ a = find(a); b = find(b); if(a == b) flag=1; else{ if(opo[a]==-1 && opo[b]==-1){ opo[a] = b; opo[b] = a; } else if(opo[a]==-1){ dsu[a] = opo[b]; } else if(opo[b]==-1){ dsu[b] = opo[a]; } else{ dsu[b] = opo[a]; dsu[opo[b]] = a; } } } int main(){ int n, i, a, b, u, tam=0; cin >> n; for(i=0;i<n;i++){ cin >> a >> b; a--;b--; truck.push_back({a, b}); ordered.push_back({b-a, i}); ini.insert({a, i}); fim.insert({b, i}); dsu[i]=i; opo[i]=-1; } sort(ordered.begin(), ordered.end()); for(i=0;i<n;i++){ u = ordered[i].s; initial = ini.upper_bound({truck[u].f, u}); last = ini.upper_bound({truck[u].s, u}); while(initial!=last){ if((*initial).s!=u) merge(u, (*initial).s); if(flag) break; initial++; } initial = fim.upper_bound({truck[u].f, u}); last = fim.upper_bound({truck[u].s, u}); while(initial!=last){ if((*initial).s!=u) merge(u, (*initial).s); if(flag) break; initial++; } if(flag) break; ini.erase({truck[u].f, u}); fim.erase({truck[u].s, u}); } if(flag) cout << "0"; else{ for(i=0;i<n;i++){ a = find(i); if(opo[a]==-1 || !vis[opo[a]]) tam++; vis[a]=1; } cout << exp(2, tam); } 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...