Submission #373093

#TimeUsernameProblemLanguageResultExecution timeMemory
373093Kevin_Zhang_TWPort Facility (JOI17_port_facility)C++17
22 / 100
31 ms1900 KiB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define pb emplace_back
#define AI(i) begin(i), end(i)
template<class T> bool chmin(T &a, T b) { return b < a && (a = b, true); }
template<class T> bool chmax(T &a, T b) { return a < b && (a = b, true); }
#ifdef KEV
#define DE(args...) kout("[ " + string(#args) + " ] = ", args)
void kout() { cerr << endl; }
template<class T, class ...U> void kout(T a, U ...b) { cerr << a << ' ', kout(b...); }
template<class T> void debug(T L, T R) { while (L != R) cerr << *L << " \n"[next(L) == R], ++L; }
#else
#define DE(...) 0
#define debug(...) 0
#endif
 
const int MAX_N = 200010, p = 1e9 + 7, inf = p;
 
int n;
pair<int,int> sh[MAX_N];
 
bool valid (int i, int j) {
	if (i > j) swap(i, j);
	return sh[i].second < sh[j].first || sh[j].second < sh[i].second;
}
bool vis[MAX_N];
int at[MAX_N];

struct sgtmn {
	int n;
	vector<int> mn;
	sgtmn (int n) : n(n) {
		mn.resize(n<<1, inf);
	}
	void modify(int i, int v) {
		mn[i + n] = v, i += n;
		while (i>>=1)
			mn[i] = min(mn[i<<1], mn[i<<1|1]);
	}
};
 
ll trykill(int x) {
	vector<int> gp[2];
	queue<pair<int,int>> q;
	q.emplace(x, 0);
	while (q.size()) {
		auto [x, g] = q.front(); q.pop();
		gp[g].pb(x);

		for (int i = 0;i < n;++i) 
			if (!vis[i] && !valid(i, x)) {
				vis[i] = true;
				q.emplace(i, g^1);
			}
	}
	auto check = [&](vector<int> sid) {
		sort(AI(sid));
		vector<int> stk;
		for (int x : sid) {
			auto [l, r] = sh[x];
			while (stk.size()) {
				int id = stk.back();
				auto [cl, cr] = sh[id];
				if (l < cr && r > cr) return false;
				if (r < cr) break;
				stk.pop_back();
			}
			stk.pb(x);
		}
		return true;
	};
	for (int i = 0;i < 2;++i)
		if (!check(gp[i]))
			return 0;
	return 2;
}
int32_t main() {
	ios_base::sync_with_stdio(0), cin.tie(0);
	cin >> n;
	for (int i = 0;i < n;++i)  {
		cin >> sh[i].first >> sh[i].second;
		auto &[l, r] = sh[i];
		--l, --r;
	}
	sort(sh, sh + n);
	for (int i = 0;i < n;++i) {
		auto [l, r] = sh[i];
		at[l] = at[r] = i;
	}
 
	if (n > 2000) return -1;
 
	ll res = 1;
	for (int i = 0;i < n;++i) if (!vis[i]) {
		vis[i] = true;
		res = res * trykill(i) % p;
	}
 
	cout << res << '\n';
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...