제출 #31098

#제출 시각아이디문제언어결과실행 시간메모리
31098RezwanArefin01Port Facility (JOI17_port_facility)C++14
22 / 100
6000 ms53280 KiB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

const int maxn = 1e6 + 10;
vector<int> adj[maxn]; 
int col[maxn], n; 

struct range{
	int l, r; 
	bool operator < (const range &p) const {
		return l == p.l ? r < p.r : l < p.l;
	}
} v[maxn];

bool cant(int i, int j) {
	return (v[i].l < v[j].l && v[j].l < v[i].r && v[j].r > v[i].r);
}

bool bfs(int r) {
	col[r] = 0; 
	queue<int> Q;
	Q.push(r); 
	while(!Q.empty()) {
		int u = Q.front(); Q.pop(); 
		for(int v : adj[u]) {
			if(col[v] == -1) {
				col[v] = col[u] ^ 1; 
				Q.push(v);
			} else if(col[v] == col[u]) return 0;
		}
	} return 1; 
}
void construct() {
	sort(v, v+n);
	range q[n]; 
	for(int i = 0; i < n; i++) {
		q[i] = {v[i].l, i}; 
	}
	for(int i = 0; i < n; i++) {
		range xx = {v[i].r, 0}; 
		int x = lower_bound(v, v+n, v[i]) - v; 
		int y = upper_bound(v, v+n, xx) - v;
		for(int j = x; j <= y; j++) {
			if(cant(i, j) || cant(j, i)) {
				adj[i].push_back(j);
				adj[j].push_back(i); 
				//cout << i << " " << v[j].r << endl;
			}
		}	
	}
}
int main(int argc, char const *argv[]) {
#ifdef LOCAL_TESTING
	freopen("in", "r", stdin);
#endif
	cin >> n; 
	for(int i = 0; i < n; i++) 
		cin >> v[i].l >> v[i].r; 

	construct(); 
	int comp = 0;
	memset(col, -1, sizeof col);
	for(int i = 0; i < n; i++) {
		if(col[i] == -1) {
			comp++;
			if(!bfs(i)) return cout << 0 << endl, 0; 
			//for(int j=0; j < n; j++) cout << col[i] <<" "; cout << endl;
		}
	}
	int ans = 1; 
	for(int i = 0; i < comp; i++) {
		ans = (ans * 2) % ll(1e9 + 7); 
	} 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...