제출 #975388

#제출 시각아이디문제언어결과실행 시간메모리
975388LCJLYPort Facility (JOI17_port_facility)C++14
22 / 100
6067 ms422340 KiB
#include <bits/stdc++.h> using namespace std; #define int long long #define ld long double #define show(x,y) cout << y << " " << #x << endl; #define show2(x,y,i,j) cout << y << " " << #x << " " << j << " " << #i << endl; #define show3(x,y,i,j,p,q) cout << y << " " << #x << " " << j << " " << #i << " " << q << " " << #p << endl; #define show4(x,y) for(auto it:y) cout << it << " "; cout << #x << endl; typedef pair<long long,long long>pii; typedef array<int,5>pi2; mt19937_64 rng(chrono::system_clock::now().time_since_epoch().count()); inline pii combine(pii a, pii b){ if(a.first>b.first){ return a; } else return b; } struct node{ int s,e,m; node *l,*r; pii v; node(int ss, int ee):s(ss),e(ee),m((s+e)>>1){ if(s!=e){ l=new node(s,m); r=new node(m+1,e); v=combine(l->v,r->v); } else{ v={0,s}; } } void upd(int x, pii y){ if(s==e){ v=y; return; } if(x<=m) l->upd(x,y); else r->upd(x,y); v=combine(l->v,r->v); } pii query(int x, int y){ if(x<=s&&y>=e){ return v; } if(y<=m) return l->query(x,y); if(x>m) return r->query(x,y); return combine(l->query(x,m),r->query(m+1,y)); } }; vector<int>adj[1000005]; int visited[1000005]; bool amos=true; const int mod=1e9+7; void dfs(int index, int col){ visited[index]=col; for(auto it:adj[index]){ if(visited[it]&&visited[it]==visited[index]) amos=false; else if(!visited[it]){ dfs(it,3-col); } } } struct DSU{ vector<int>e; void init(int n){ e=vector<int>(n,-1); } int get(int x){ return e[x]<0?x:e[x]=get(e[x]); } bool sameSet(int x, int y){ return get(x)==get(y); } bool unite(int x, int y){ x=get(x); y=get(y); if(x==y) return 0; if(e[x]>e[y]) swap(x,y); e[x]+=e[y]; e[y]=x; return 1; } }; void solve(){ int n; cin >> n; pii arr[n]; for(int x=0;x<n;x++){ cin >> arr[x].first >> arr[x].second; } sort(arr,arr+n); DSU dsu; dsu.init(2*n+5); node st(0,2*n+5); for(int x=0;x<n;x++){ st.upd(arr[x].first,{arr[x].second,x}); } for(int x=0;x<n;x++){ vector<pii>undo; st.upd(arr[x].first,{0,x}); while(1){ pii hold=st.query(arr[x].first,arr[x].second); if(hold.first<=arr[x].second) break; adj[x].push_back(hold.second); adj[hold.second].push_back(x); dsu.unite(x,hold.second+n); dsu.unite(x+n,hold.second); if(dsu.sameSet(x,x+n)||dsu.sameSet(hold.second,hold.second+n)){ cout << 0; return; } st.upd(arr[hold.second].first,{0,hold.second}); undo.push_back(hold); } for(auto it:undo){ st.upd(arr[it.second].first,{arr[it.second].second,it.second}); } } int counter=1; for(int x=0;x<n;x++){ if(!visited[x]){ dfs(x,1); counter=(counter*2)%mod; } } if(!amos){ cout << 0; } else cout << counter; } int32_t main(){ ios::sync_with_stdio(0); cin.tie(0); int t=1; //freopen("in.txt","r",stdin); //cin >> t; while(t--){ solve(); } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...