This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 = 2000010, 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]);
}
int qry(int l, int r) {
int res = inf;
l += n, r += n;
for (;l < r;l>>=1, r>>=1) {
if (l&1) chmin(res, mn[l++]);
if (r&1) chmin(res, mn[--r]);
}
return res;
}
};
struct sgtmx {
int n;
vector<int> mx;
sgtmx (int n) : n(n) {
mx.resize(n<<1, -1);
}
void modify(int i, int v) {
mx[i + n] = v, i += n;
while (i>>=1)
mx[i] = max(mx[i<<1], mx[i<<1|1]);
}
int qry(int l, int r) {
int res = -1;
l += n, r += n;
for (;l < r;l>>=1, r>>=1) {
if (l&1) chmax(res, mx[l++]);
if (r&1) chmax(res, mx[--r]);
}
return res;
}
};
sgtmn mnt(0);
sgtmx mxt(0);
void remove(int id) {
auto [l, r] = sh[id];
mnt.modify(r, inf);
mxt.modify(l, -1);
vis[id] = true;
}
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);
auto [l, r] = sh[x];
int one;
while ((one = mnt.qry(l, r)) < l) {
int id = at[one];
remove(id);
q.emplace(id, g^1);
}
while ((one = mxt.qry(l, r)) > r) {
int id = at[one];
remove(id);
q.emplace(id, 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);
mnt = sgtmn(n * 2);
mxt = sgtmx(n * 2);
for (int i = 0;i < n;++i) {
auto [l, r] = sh[i];
DE(l, r);
at[l] = at[r] = i;
mnt.modify(r, l);
mxt.modify(l, r);
}
//if (n > 2000) return -1;
ll res = 1;
for (int i = 0;i < n;++i) if (!vis[i]) {
remove(i);
res = res * trykill(i) % p;
}
cout << res << '\n';
}
Compilation message (stderr)
port_facility.cpp: In function 'int32_t main()':
port_facility.cpp:14:17: warning: statement has no effect [-Wunused-value]
14 | #define DE(...) 0
| ^
port_facility.cpp:142:3: note: in expansion of macro 'DE'
142 | DE(l, r);
| ^~
# | 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... |