이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define LL long long
#define PII pair<int, int>
const LL MOD = (LL) 1e9 + 7;
const int MAXN = 2000005;
using namespace std;
 
struct Interval {
	int l, r;
};
 
struct DSU {
	int pa[MAXN];
	void init(int n) {
		for (int i = 0; i <= n; i++) {
			pa[i] = i;
		}
	}
	int find(int x) {
		return x == pa[x] ? x : pa[x] = find(pa[x]);
	}
	int merge(int x, int y) {
		x = find(x); y = find(y);
		if (x == y) return 0;
		pa[x] = y;
		return 1;
	}
} U;
 
int n, col[MAXN], id[MAXN];
vector<int> G[MAXN];
Interval I[MAXN];
 
void add_edge(int u, int v) {
	G[u].push_back(v);
	G[v].push_back(u);
}
 
int dfs(int v, int c) {
	int ok = 1;
	col[v] = c;
	for (int to : G[v]) {
		if (col[to] == -1) {
			ok &= dfs(to, c ^ 1);
		}
		else {
			ok &= (col[to] != col[v]);
		}
	}
	return ok;
}
 
int main() {
	ios_base::sync_with_stdio(0); cin.tie(0);
	cin >> n;
	U.init(2 * n);
	set<int> rights;
	for (int i = 1; i <= n; i++) {
		cin >> I[i].l >> I[i].r;
		rights.insert(I[i].r);
		U.merge(I[i].l, I[i].l + 1);
		id[I[i].l] = id[I[i].r] = i;
	}
	sort(I + 1, I + 1 + n, [&] (Interval a, Interval b) {
		return a.l < b.l;
	});
	
	for (int i = n; i >= 1; i--) {
		auto it = rights.upper_bound(I[i].l);
		if (it == rights.end()) {
			continue;
		}
		int pre = -1, cur = *it;
		while (cur < I[i].r) {
			add_edge(id[cur], i);
			if (pre != -1) {
				U.merge(pre, pre + 1);
			}
			pre = cur;
			cur = U.find(cur + 1);
		}
		U.merge(I[i].r, I[i].r + 1);
		rights.erase(I[i].r);
	}
	
	int cc = 0;
	fill(col + 1, col + 1 + n, -1);
	for (int i = 1; i <= n; i++) {
		if (col[i] == -1) {
			if (!dfs(i, 0)) {
				cout << "0\n";
				return 0;
			}
			cc++;
		}
	}
	LL ans = 1;
	for (int i = 0; i < cc; i++) {
		ans = ans * 2 % MOD;
	}
	cout << ans << '\n';
	return 0;
}
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |