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;
#ifdef LOCAL
#include <debug.h>
#else
#define debug(...)
#endif
#define ft front
#define bk back
#define st first
#define nd second
#define ins insert
#define ers erase
#define pb push_back
#define pf push_front
#define _pb pop_back
#define _pf pop_front
#define lb lower_bound
#define ub upper_bound
#define mtp make_tuple
#define bg begin
#define ed end
#define all(x) (x).bg(), (x).ed()
#define sz(x) (int)(x).size()
typedef long long ll; typedef unsigned long long ull;
typedef double db; typedef long double ldb;
typedef pair<int, int> pi; typedef pair<ll, ll> pll;
typedef vector<int> vi; typedef vector<ll> vll; typedef vector<pi> vpi; typedef vector<pll> vpll;
typedef string str;
template<typename T> T gcd(T a, T b) { return (b == 0? a : gcd(b, a % b)); }
template<typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
#define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
#define FOS(i, r, l) for (int (i) = (r); (i) >= (l); --(i))
#define FRN(i, n) for (int (i) = 0; (i) < (n); ++(i))
#define FSN(i, n) for (int (i) = (n) - 1; (i) >= 0; --(i))
#define EACH(i, x) for (auto &(i) : (x))
#define WHILE while
#define file "TEST"
mt19937 rd(chrono::steady_clock::now().time_since_epoch().count());
ll rand(ll l, ll r) { return uniform_int_distribution<ll>(l, r)(rd); }
/*
----------------------------------------------------------------
END OF TEMPLATE
----------------------------------------------------------------
Tran The Bao - ghostwriter
Training for VOI23 gold medal
----------------------------------------------------------------
GOAT
----------------------------------------------------------------
*/
const int M = 1e9 + 7;
const int oo = 1e9 + 5;
const int N = 1e6 + 1;
const int T = 8e6 + 1;
int n, a[N], b[N], a1[N], b1[N], a2[N], c[N], c1[N], rs = 1;
pi tr[T], tr1[T];
void upd(int i, int l, int r, int q, pi v) {
if (r < q || l > q) return;
if (l == r) {
tr[i] = v;
return;
}
int mid = l + (r - l) / 2;
upd(i * 2, l, mid, q, v);
upd(i * 2 + 1, mid + 1, r, q, v);
tr[i] = max(tr[i * 2], tr[i * 2 + 1]);
}
pi get(int i, int l, int r, int ql, int qr) {
if (r < ql || l > qr) return {-oo, -oo};
if (ql <= l && r <= qr) return tr[i];
int mid = l + (r - l) / 2;
return max(get(i * 2, l, mid, ql, qr), get(i * 2 + 1, mid + 1, r, ql, qr));
}
void upd1(int i, int l, int r, int q, pi v) {
if (r < q || l > q) return;
if (l == r) {
tr1[i] = v;
return;
}
int mid = l + (r - l) / 2;
upd1(i * 2, l, mid, q, v);
upd1(i * 2 + 1, mid + 1, r, q, v);
tr1[i] = min(tr1[i * 2], tr1[i * 2 + 1]);
}
pi get1(int i, int l, int r, int ql, int qr) {
if (r < ql || l > qr) return {oo, oo};
if (ql <= l && r <= qr) return tr1[i];
int mid = l + (r - l) / 2;
return min(get1(i * 2, l, mid, ql, qr), get1(i * 2 + 1, mid + 1, r, ql, qr));
}
void dfs(int u, int v) {
c[u] = 1;
c1[u] = v;
upd(1, 1, 2 * n, a[u], {-oo, -oo});
upd1(1, 1, 2 * n, b[u], {oo, oo});
WHILE(1) {
bool ok = 0;
pi tmp = get(1, 1, 2 * n, a[u], b[u]);
if (tmp.st > b[u]) {
dfs(tmp.nd, v ^ 1);
ok = 1;
}
tmp = get1(1, 1, 2 * n, a[u], b[u]);
if (tmp.st < a[u]) {
dfs(tmp.nd, v ^ 1);
ok = 1;
}
if (!ok) break;
}
}
signed main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
// freopen(file".inp", "r", stdin);
// freopen(file".out", "w", stdout);
cin >> n;
FOR(i, 1, n) {
cin >> a[i] >> b[i];
a1[i] = a[i];
b1[i] = b[i];
a2[i] = i;
}
auto cmp = [&](int f, int s) -> bool {
return b[f] < b[s];
};
sort(a2 + 1, a2 + 1 + n, cmp);
FOR(i, 1, n) tie(a[i], b[i]) = mtp(a1[a2[i]], b1[a2[i]]);
FOR(i, 1, n << 3) tr[i] = {-oo, -oo};
FOR(i, 1, n << 3) tr1[i] = {oo, oo};
FOR(i, 1, n) {
upd(1, 1, 2 * n, a[i], {b[i], i});
upd1(1, 1, 2 * n, b[i], {a[i], i});
}
FOR(i, 1, n) {
if (c[i]) continue;
dfs(i, 0);
rs = 2LL * rs % M;
}
FOR(i, 1, n << 3) tr1[i] = {oo, oo};
FOR(i, 1, n) {
if (c1[i]) continue;
pi tmp = get1(1, 1, 2 * n, a[i], b[i]);
if (tmp.st < a[i]) rs = 0;
upd1(1, 1, 2 * n, b[i], {a[i], i});
}
FOR(i, 1, n << 3) tr1[i] = {oo, oo};
FOR(i, 1, n) {
if (!c1[i]) continue;
pi tmp = get1(1, 1, 2 * n, a[i], b[i]);
if (tmp.st < a[i]) rs = 0;
upd1(1, 1, 2 * n, b[i], {a[i], i});
}
cout << rs;
return 0;
}
/*
3
2 5
1 4
3 6
----------------------------------------------------------------
From Benq:
stuff you should look for
* int overflow, array bounds
* special cases (n=1?)
* do smth instead of nothing and stay organized
* WRITE STUFF DOWN
* DON'T GET STUCK ON ONE APPROACH
----------------------------------------------------------------
*/
Compilation message (stderr)
port_facility.cpp: In function 'int main()':
port_facility.cpp:32:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
| ^
port_facility.cpp:116:5: note: in expansion of macro 'FOR'
116 | FOR(i, 1, n) {
| ^~~
port_facility.cpp:32:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
| ^
port_facility.cpp:126:5: note: in expansion of macro 'FOR'
126 | FOR(i, 1, n) tie(a[i], b[i]) = mtp(a1[a2[i]], b1[a2[i]]);
| ^~~
port_facility.cpp:32:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
| ^
port_facility.cpp:127:5: note: in expansion of macro 'FOR'
127 | FOR(i, 1, n << 3) tr[i] = {-oo, -oo};
| ^~~
port_facility.cpp:32:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
| ^
port_facility.cpp:128:5: note: in expansion of macro 'FOR'
128 | FOR(i, 1, n << 3) tr1[i] = {oo, oo};
| ^~~
port_facility.cpp:32:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
| ^
port_facility.cpp:129:5: note: in expansion of macro 'FOR'
129 | FOR(i, 1, n) {
| ^~~
port_facility.cpp:32:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
| ^
port_facility.cpp:133:5: note: in expansion of macro 'FOR'
133 | FOR(i, 1, n) {
| ^~~
port_facility.cpp:32:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
| ^
port_facility.cpp:138:5: note: in expansion of macro 'FOR'
138 | FOR(i, 1, n << 3) tr1[i] = {oo, oo};
| ^~~
port_facility.cpp:32:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
| ^
port_facility.cpp:139:5: note: in expansion of macro 'FOR'
139 | FOR(i, 1, n) {
| ^~~
port_facility.cpp:32:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
| ^
port_facility.cpp:145:5: note: in expansion of macro 'FOR'
145 | FOR(i, 1, n << 3) tr1[i] = {oo, oo};
| ^~~
port_facility.cpp:32:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
| ^
port_facility.cpp:146:5: note: in expansion of macro 'FOR'
146 | FOR(i, 1, n) {
| ^~~
# | 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... |