이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
#define x first
#define y second
#define all(x) x.begin(), x.end()
const int N = 1<<20;
const int MOD = 1e9+7;
inline int read_char() {
static char buf[1 << 24], *ptr = buf, *end_ptr = buf;
if (ptr == end_ptr) {
int len = fread(buf, 1, sizeof(buf), stdin);
if (len <= 0) return EOF;
ptr = buf;
end_ptr = buf + len;
}
return *ptr++;
}
inline int read_int() {
#undef getchar_unlocked
#define getchar_unlocked read_char
int sg = +1, ch;
do {
ch = getchar_unlocked();
if (ch == '-') sg = -1;
} while (ch < '0' || ch > '9');
int x = ch - '0';
while (true) {
ch = getchar_unlocked();
if (ch < '0' || ch > '9') break;
x = x * 10 + ch - '0';
}
return sg*x;
}
int n;
pii A[N];
pii ev[N<<1];
int col[N];
struct data {
vector<int> v;
int ptr;
} seg[N<<2], seg2[N<<2];
void build(int p=1, int b=0, int e=2*n-1)
{
if (b == e) {
if (ev[b].y == 1)
seg[p].v.push_back(A[ev[b].x].y);
} else {
int m = (b+e)/2;
build(p<<1, b, m);
build(p<<1|1, m+1, e);
seg[p].v.reserve(seg[p<<1].v.size() + seg[p<<1|1].v.size());
merge(all(seg[p<<1].v), all(seg[p<<1|1].v), back_inserter(seg[p].v));
}
seg[p].ptr = seg[p].v.size()-1;
}
inline int ix(int t) {
return t < 0 ? -1 : ev[t].x;
}
inline int amort(int p)
{
while (seg[p].ptr >= 0 && col[ix(seg[p].v[seg[p].ptr])])
--seg[p].ptr;
return seg[p].ptr == -1 ? -1 : seg[p].v[seg[p].ptr];
}
int get(int l, int r, int p=1, int b=0, int e=2*n-1)
{
if (b > r || e < l)
return -1;
if (b >= l && e <= r)
return amort(p);
int m = (b+e)/2;
return max(get(l, r, p<<1, b, m), get(l, r, p<<1|1, m+1, e));
}
inline int ix2(int t) {
return t < 0 ? -1 : ev[2*n-t-1].x;
}
void build2(int p=1, int b=0, int e=2*n-1)
{
if (b == e) {
int t = 2*n-b-1;
if (ev[t].y == 2)
seg2[p].v.push_back(2*n-A[ev[t].x].x-1);
} else {
int m = (b+e)/2;
build2(p<<1, b, m);
build2(p<<1|1, m+1, e);
merge(all(seg2[p<<1].v), all(seg2[p<<1|1].v), back_inserter(seg2[p].v));
}
seg2[p].ptr = seg2[p].v.size()-1;
}
inline int amort2(int p)
{
while (seg2[p].ptr >= 0 && col[ix2(seg2[p].v[seg2[p].ptr])])
--seg2[p].ptr;
return seg2[p].ptr == -1 ? -1 : seg2[p].v[seg2[p].ptr];
}
int get2(int l, int r, int p=1, int b=0, int e=2*n-1)
{
if (b > r || e < l)
return -1;
if (b >= l && e <= r)
return amort2(p);
int m = (b+e)/2;
return max(get2(l, r, p<<1, b, m), get2(l, r, p<<1|1, m+1, e));
}
inline 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)
{
//for (int j = 0; j < lv; ++j)
//printf("| ");
//printf("solve %d, color = %d\n", i, col[i]);
while (true) {
int ans = get(A[i].x, A[i].y);
int j = ix(ans);
if (ans == -1 || !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);
}
while (true) {
int ans = get2(2*n-A[i].y-1, 2*n-A[i].x-1);
int j = ix2(ans);
if (ans == -1 || !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()
{
n = read_int();
for (int i = 0; i < n; ++i) {
A[i].x = read_int();
A[i].y = read_int();
ev[--A[i].x] = pii(i, 1);
ev[--A[i].y] = pii(i, 2);
//printf("%d: %d %d\n", i, A[i].x, A[i].y);
}
build();
build2();
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;
}
# | 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... |