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 pii = pair<int, int>;
#define x first
#define y second
const int N = 1<<20;
const int MOD = 1e9+7;
const int inf = 1e9;
int n;
pii A[N];
pii ev[N<<1];
int col[N];
set<pii> seg[N<<2];
void add(int i, pii x, int p=1, int b=0, int e=2*n-1) {
seg[p].insert(x);
if (b == e)
return;
int m = (b+e)/2;
if (i <= m)
add(i, x, p<<1, b, m);
else
add(i, x, p<<1|1, m+1, e);
}
void remove(int i, pii x, int p=1, int b=0, int e=2*n-1) {
seg[p].erase(x);
if (b == e)
return;
int m = (b+e)/2;
if (i <= m)
remove(i, x, p<<1, b, m);
else
remove(i, x, p<<1|1, m+1, e);
}
pii get(int l, int r, int x, int p=1, int b=0, int e=2*n-1) {
if (l > r || b > e || b > r || e < l)
return pii(inf, inf);
if (b >= l && e <= r) {
auto ptr = seg[p].lower_bound(pii(x, 0));
return ptr == seg[p].end() ? pii(inf, inf) : *ptr;
}
int m = (b+e)/2;
return min(get(l, r, x, p<<1, b, m), get(l, r, x, p<<1|1, m+1, e));
}
bool intersect(int al, int ar, int bl, int br)
{
if (bl > ar || br < al) return false;
if (bl >= al && br <= ar) return false;
if (al >= bl && ar <= br) return false;
return true;
}
void solve(int i, int lv=0)
{
remove(A[i].x, pii(A[i].y, i));
//for (int j = 0; j < lv; ++j)
//printf("| ");
//printf("solve %d, color = %d\n", i, col[i]);
while (true) {
pii ans = get(A[i].x, A[i].y, A[i].y);
if (ans == pii(inf, inf))
ans = get(0, A[i].x, A[i].x);
int j = ans.y;
if (ans == pii(inf, inf) || !intersect(A[i].x, A[i].y, A[j].x, A[j].y))
break;
col[j] = col[i] == 1 ? 2 : 1;
solve(j, lv+1);
}
}
int main()
{
scanf("%d", &n);
for (int i = 0; i < n; ++i) {
scanf("%d%d", &A[i].x, &A[i].y);
ev[--A[i].x] = pii(i, 1);
ev[--A[i].y] = pii(i, 2);
add(A[i].x, pii(A[i].y, i));
}
int ans = 1;
for (int i = 0; i < n; ++i) {
if (!col[i]) {
col[i] = 1;
solve(i);
ans = (ans*2)%MOD;
}
}
stack<int> S[2];
for (int i = 0; i < 2*n; ++i) {
int x = ev[i].x, t = ev[i].y;
int c = col[x]-1;
if (t == 1)
S[c].push(x);
else {
if (S[c].top() != x) {
ans = 0;
break;
}
S[c].pop();
}
}
printf("%d\n", ans);
return 0;
}
Compilation message (stderr)
port_facility.cpp: In function 'int main()':
port_facility.cpp:80:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &n);
~~~~~^~~~~~~~~~
port_facility.cpp:82:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &A[i].x, &A[i].y);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |