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 <stdio.h>
#include <stdlib.h>
#define N 100002
#define N2 ((N + 1) * 2)
int abs_(int a) { return a > 0 ? a : -a; }
int min(int a, int b) { return a < b ? a : b; }
int max(int a, int b) { return a > b ? a : b; }
unsigned int Z = 12345;
int rand_() {
return (Z *= 3) >> 1;
}
int xx[N * 2], yy[N * 2], n, n2;
int compare_x(int i, int j) {
return xx[i] - xx[j];
}
int compare_y(int i, int j) {
return yy[i] - yy[j];
}
int (*compare)(int, int);
void sort(int *ii, int l, int r) {
while (l < r) {
int i = l, j = l, k = r, i_ = ii[l + rand_() % (r - l)], tmp;
while (j < k) {
int c = compare(ii[j], i_);
if (c == 0)
j++;
else if (c < 0) {
tmp = ii[i], ii[i] = ii[j], ii[j] = tmp;
i++, j++;
} else {
k--;
tmp = ii[j], ii[j] = ii[k], ii[k] = tmp;
}
}
sort(ii, l, i);
l = k;
}
}
int *kk, zz[N + 1], ll[N + 1], rr[N + 1], ii[N + 1], u_, l_, r_;
int node(int i) {
static int _ = 1;
zz[_] = rand_();
ii[_] = i;
return _++;
}
void split(int u, int i) {
int c;
if (u == 0) {
u_ = l_ = r_ = 0;
return;
}
c = kk[ii[u] << 1 | 0] - kk[i << 1 | 0];
if (c < 0) {
split(rr[u], i);
rr[u] = l_, l_ = u;
} else if (c > 0) {
split(ll[u], i);
ll[u] = r_, r_ = u;
} else {
u_ = u, l_ = ll[u], r_ = rr[u];
ll[u] = rr[u] = 0;
}
}
int merge(int u, int v) {
if (u == 0)
return v;
if (v == 0)
return u;
if (zz[u] < zz[v]) {
rr[u] = merge(rr[u], v);
return u;
} else {
ll[v] = merge(u, ll[v]);
return v;
}
}
void tr_add(int i) {
split(u_, i);
u_ = merge(merge(l_, node(i)), r_);
}
void tr_remove(int i) {
split(u_, i);
u_ = merge(l_, r_);
}
int tr_lower(int k) {
int u = u_, i_ = -1;
while (u)
if (kk[ii[u] << 1 | 0] < k)
i_ = ii[u], u = rr[u];
else
u = ll[u];
return i_ == -1 ? 0 : kk[i_ << 1 | 1];
}
int tr_higher(int k) {
int u = u_, i_ = -1;
while (u)
if (kk[ii[u] << 1 | 0] > k)
i_ = ii[u], u = ll[u];
else
u = rr[u];
return i_ == -1 ? n2 : kk[i_ << 1 | 0];
}
int xx1[N * 2], xx2[N * 2], yy1[N * 2], yy2[N * 2];
int intersect1(int a, int b, int c, int d) {
return max(a, c) <= min(b, d);
}
int intersect2(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4) {
return intersect1(x1, x2, x3, x4) && intersect1(y1, y2, y3, y4);
}
int main() {
static int ii[N * 2], dd[N * 4], qu[N * 4];
int h, h_, h1, i, i_, j, x1, y1, x2, y2, x, y, d, cnt;
scanf("%d%d%d%d%d", &x1, &y1, &x2, &y2, &n), n += 2, n2 = (n + 1) * 2;
xx[0 << 1 | 0] = xx[0 << 1 | 1] = x1, yy[0 << 1 | 0] = yy[0 << 1 | 1] = y1;
xx[1 << 1 | 0] = xx[1 << 1 | 1] = x2, yy[1 << 1 | 0] = yy[1 << 1 | 1] = y2;
for (i = 2; i < n; i++)
scanf("%d%d%d%d", &xx[i << 1 | 0], &xx[i << 1 | 1], &yy[i << 1 | 0], &yy[i << 1 | 1]);
for (i = 0; i < n * 2; i++)
ii[i] = i;
compare = compare_x, sort(ii, 0, n * 2);
for (i = 0, x = 1; i < n * 2; i++)
xx[ii[i]] = i + 1 == n * 2 || xx[ii[i + 1]] != xx[ii[i]] ? x++ : x;
compare = compare_y, sort(ii, 0, n * 2);
for (i = 0, y = 1; i < n * 2; i++)
yy[ii[i]] = i + 1 == n * 2 || yy[ii[i + 1]] != yy[ii[i]] ? y++ : y;
compare = compare_x, sort(ii, 0, n * 2);
kk = yy;
for (h = 0; h < n * 2; h = h_) {
h_ = h + 1, x = xx[ii[h]];
while (h_ < n * 2 && xx[ii[h_]] == x)
h_++;
for (h1 = h; h1 < h_; h1++) {
i_ = ii[h1], i = i_ >> 1;
if ((i_ & 1) == 1)
tr_remove(i);
}
for (h1 = h; h1 < h_; h1++) {
i_ = ii[h1], i = i_ >> 1;
yy1[i_] = tr_lower(yy[i << 1 | 0]), yy2[i_] = tr_higher(yy[i << 1 | 1]);
}
for (h1 = h; h1 < h_; h1++) {
i_ = ii[h1], i = i_ >> 1;
if ((i_ & 1) == 0 && xx[i_] != xx[i_ ^ 1])
tr_add(i);
}
}
compare = compare_y, sort(ii, 0, n * 2);
kk = xx;
for (h = 0; h < n * 2; h = h_) {
h_ = h + 1, y = yy[ii[h]];
while (h_ < n * 2 && yy[ii[h_]] == y)
h_++;
for (h1 = h; h1 < h_; h1++) {
i_ = ii[h1], i = i_ >> 1;
if ((i_ & 1) == 1)
tr_remove(i);
}
for (h1 = h; h1 < h_; h1++) {
i_ = ii[h1], i = i_ >> 1;
xx1[i_] = tr_lower(xx[i << 1 | 0]), xx2[i_] = tr_higher(xx[i << 1 | 1]);
}
for (h1 = h; h1 < h_; h1++) {
i_ = ii[h1], i = i_ >> 1;
if ((i_ & 1) == 0 && yy[i_] != yy[i_ ^ 1])
tr_add(i);
}
}
if (intersect2(xx1[0], yy[0], xx2[0], yy[0], x2, y2, x2, y2) || intersect2(xx[0], yy1[0], xx[0], yy2[0], x2, y2, x2, y2)) {
printf("1\n");
return 0;
}
for (i = 0; i < n * 4; i++)
dd[i] = n * 4 + 1;
cnt = 0;
dd[0 << 1 | 0] = 0, qu[cnt++] = 0 << 1 | 0;
dd[0 << 1 | 1] = 0, qu[cnt++] = 0 << 1 | 1;
for (h = 0; h < cnt; h++) {
i_ = qu[h], i = i_ >> 1, d = dd[i_] + 1;
if (i == 2) {
printf("%d\n", d);
return 0;
}
if ((i_ & 1) == 0) {
for (j = 0; j < n * 2; j++)
if (intersect2(xx1[i], yy[i], xx2[i], yy[i], xx[j], yy1[j], xx[j], yy2[j]) && dd[j << 1 | 1] > d)
dd[j << 1 | 1] = d, qu[cnt++] = j << 1 | 1;
} else {
for (j = 0; j < n * 2; j++)
if (intersect2(xx[i], yy1[i], xx[i], yy2[i], xx1[j], yy[j], xx2[j], yy[j]) && dd[j << 1 | 0] > d)
dd[j << 1 | 0] = d, qu[cnt++] = j << 1 | 0;
}
}
return 0;
}
Compilation message (stderr)
golf.c: In function 'main':
golf.c:141:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
141 | scanf("%d%d%d%d%d", &x1, &y1, &x2, &y2, &n), n += 2, n2 = (n + 1) * 2;
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
golf.c:145:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
145 | scanf("%d%d%d%d", &xx[i << 1 | 0], &xx[i << 1 | 1], &yy[i << 1 | 0], &yy[i << 1 | 1]);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |