#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int s, t, u, v;
cin >> s >> t >> u >> v;
int n;
cin >> n;
vector<int> a(n), b(n), c(n), d(n);
for (int i = 0; i < n; i++) {
cin >> a[i] >> c[i] >> b[i] >> d[i];
}
vector<int> xs;
xs.push_back(s);
xs.push_back(t);
xs.push_back(u);
xs.push_back(v);
for (int i = 0; i < n; i++) {
xs.push_back(a[i]);
xs.push_back(b[i]);
xs.push_back(c[i]);
xs.push_back(d[i]);
}
sort(xs.begin(), xs.end());
xs.erase(unique(xs.begin(), xs.end()), xs.end());
sort(xs.begin(), xs.end());
xs.erase(unique(xs.begin(), xs.end()), xs.end());
s = (int) (lower_bound(xs.begin(), xs.end(), s) - xs.begin());
t = (int) (lower_bound(xs.begin(), xs.end(), t) - xs.begin());
u = (int) (lower_bound(xs.begin(), xs.end(), u) - xs.begin());
v = (int) (lower_bound(xs.begin(), xs.end(), v) - xs.begin());
for (int i = 0; i < n; i++) {
a[i] = (int) (lower_bound(xs.begin(), xs.end(), a[i]) - xs.begin());
b[i] = (int) (lower_bound(xs.begin(), xs.end(), b[i]) - xs.begin());
c[i] = (int) (lower_bound(xs.begin(), xs.end(), c[i]) - xs.begin());
d[i] = (int) (lower_bound(xs.begin(), xs.end(), d[i]) - xs.begin());
}
int k = (int) xs.size();
vector<array<int, 3>> ver;
vector<array<int, 3>> hor;
for (int i = 0; i < n; i++) {
{
// left
int from = 0;
for (int j = 0; j < n; j++) {
if (d[j] < b[i] && a[j] < a[i] && a[i] < c[j]) {
from = max(from, d[j]);
}
}
int to = k - 1;
for (int j = 0; j < n; j++) {
if (b[j] > d[i] && a[j] < a[i] && a[i] < c[j]) {
to = min(to, b[j]);
}
}
ver.push_back({a[i], from, to});
}
{
// right
int from = 0;
for (int j = 0; j < n; j++) {
if (d[j] < b[i] && a[j] < c[i] && c[i] < c[j]) {
from = max(from, d[j]);
}
}
int to = k - 1;
for (int j = 0; j < n; j++) {
if (b[j] > d[i] && a[j] < c[i] && c[i] < c[j]) {
to = min(to, b[j]);
}
}
ver.push_back({c[i], from, to});
}
{
// bottom
int from = 0;
for (int j = 0; j < n; j++) {
if (c[j] < a[i] && b[j] < b[i] && b[i] < d[j]) {
from = max(from, c[j]);
}
}
int to = k - 1;
for (int j = 0; j < n; j++) {
if (a[j] > c[i] && b[j] < b[i] && b[i] < d[j]) {
to = min(to, a[j]);
}
}
hor.push_back({b[i], from, to});
}
{
// top
int from = 0;
for (int j = 0; j < n; j++) {
if (c[j] < a[i] && b[j] < d[i] && d[i] < d[j]) {
from = max(from, c[j]);
}
}
int to = k - 1;
for (int j = 0; j < n; j++) {
if (a[j] > c[i] && b[j] < d[i] && d[i] < d[j]) {
to = min(to, a[j]);
}
}
hor.push_back({d[i], from, to});
}
}
{
// start ver
int from = 0;
for (int i = 0; i < n; i++) {
if (d[i] < t && a[i] < s && s < c[i]) {
from = max(from, d[i]);
}
}
int to = k - 1;
for (int i = 0; i < n; i++) {
if (b[i] > t && a[i] < s && s < c[i]) {
to = min(to, b[i]);
}
}
ver.push_back({s, from, to});
}
{
// start hor
int from = 0;
for (int i = 0; i < n; i++) {
if (c[i] < s && b[i] < t && t < d[i]) {
from = max(from, c[i]);
}
}
int to = k - 1;
for (int i = 0; i < n; i++) {
if (a[i] > s && b[i] < t && t < d[i]) {
to = min(to, a[i]);
}
}
hor.push_back({t, from, to});
}
{
// end ver
int from = 0;
for (int i = 0; i < n; i++) {
if (d[i] < v && a[i] < u && u < c[i]) {
from = max(from, d[i]);
}
}
int to = k - 1;
for (int i = 0; i < n; i++) {
if (b[i] > v && a[i] < u && u < c[i]) {
to = min(to, b[i]);
}
}
ver.push_back({u, from, to});
}
{
// end hor
int from = 0;
for (int i = 0; i < n; i++) {
if (c[i] < u && b[i] < v && v < d[i]) {
from = max(from, c[i]);
}
}
int to = k - 1;
for (int i = 0; i < n; i++) {
if (a[i] > u && b[i] < v && v < d[i]) {
to = min(to, a[i]);
}
}
hor.push_back({v, from, to});
}
sort(hor.begin(), hor.end());
sort(ver.begin(), ver.end());
hor.erase(unique(hor.begin(), hor.end()), hor.end());
ver.erase(unique(ver.begin(), ver.end()), ver.end());
int sh = (int) hor.size();
int sv = (int) ver.size();
int m = sh + sv;
vector<int> dist(m, -1);
for (int i = 0; i < sh; i++) {
if (hor[i][0] == t && hor[i][1] <= s && s <= hor[i][2]) {
dist[i] = 1;
}
}
for (int i = 0; i < sv; i++) {
if (ver[i][0] == s && ver[i][1] <= t && t <= ver[i][2]) {
dist[sh + i] = 1;
}
}
vector<int> que;
for (int i = 0; i < m; i++) {
if (dist[i] == 1) {
que.push_back(i);
}
}
for (int q = 0; q < (int) que.size(); q++) {
int x = que[q];
if (x < sh) {
for (int i = 0; i < sv; i++) {
if (dist[sh + i] == -1 && hor[x][1] <= ver[i][0] && ver[i][0] <= hor[x][2]
&& ver[i][1] <= hor[x][0] && hor[x][0] <= ver[i][2]) {
dist[sh + i] = dist[x] + 1;
que.push_back(sh + i);
}
}
} else {
x -= sh;
for (int i = 0; i < sh; i++) {
if (dist[i] == -1 && ver[x][1] <= hor[i][0] && hor[i][0] <= ver[x][2]
&& hor[i][1] <= ver[x][0] && ver[x][0] <= hor[i][2]) {
dist[i] = dist[x + sh] + 1;
que.push_back(i);
}
}
}
}
int res = (int) 1e9;
for (int i = 0; i < sh; i++) {
if (hor[i][0] == v && hor[i][1] <= u && u <= hor[i][2]) {
res = min(res, dist[i]);
}
}
for (int i = 0; i < sv; i++) {
if (ver[i][0] == u && ver[i][1] <= v && v <= ver[i][2]) {
res = min(res, dist[i + sh]);
}
}
cout << res << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
45 ms |
588 KB |
Output is correct |
6 |
Correct |
46 ms |
536 KB |
Output is correct |
7 |
Correct |
44 ms |
472 KB |
Output is correct |
8 |
Correct |
46 ms |
592 KB |
Output is correct |
9 |
Correct |
46 ms |
344 KB |
Output is correct |
10 |
Correct |
46 ms |
348 KB |
Output is correct |
11 |
Correct |
46 ms |
348 KB |
Output is correct |
12 |
Correct |
46 ms |
344 KB |
Output is correct |
13 |
Correct |
46 ms |
588 KB |
Output is correct |
14 |
Correct |
47 ms |
576 KB |
Output is correct |
15 |
Correct |
2 ms |
344 KB |
Output is correct |
16 |
Correct |
8 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
45 ms |
588 KB |
Output is correct |
6 |
Correct |
46 ms |
536 KB |
Output is correct |
7 |
Correct |
44 ms |
472 KB |
Output is correct |
8 |
Correct |
46 ms |
592 KB |
Output is correct |
9 |
Correct |
46 ms |
344 KB |
Output is correct |
10 |
Correct |
46 ms |
348 KB |
Output is correct |
11 |
Correct |
46 ms |
348 KB |
Output is correct |
12 |
Correct |
46 ms |
344 KB |
Output is correct |
13 |
Correct |
46 ms |
588 KB |
Output is correct |
14 |
Correct |
47 ms |
576 KB |
Output is correct |
15 |
Correct |
2 ms |
344 KB |
Output is correct |
16 |
Correct |
8 ms |
348 KB |
Output is correct |
17 |
Correct |
52 ms |
604 KB |
Output is correct |
18 |
Correct |
54 ms |
604 KB |
Output is correct |
19 |
Correct |
51 ms |
608 KB |
Output is correct |
20 |
Correct |
50 ms |
600 KB |
Output is correct |
21 |
Correct |
51 ms |
600 KB |
Output is correct |
22 |
Correct |
51 ms |
552 KB |
Output is correct |
23 |
Correct |
51 ms |
604 KB |
Output is correct |
24 |
Correct |
51 ms |
604 KB |
Output is correct |
25 |
Correct |
51 ms |
560 KB |
Output is correct |
26 |
Correct |
51 ms |
612 KB |
Output is correct |
27 |
Correct |
3 ms |
344 KB |
Output is correct |
28 |
Correct |
9 ms |
348 KB |
Output is correct |
29 |
Correct |
9 ms |
560 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
45 ms |
588 KB |
Output is correct |
6 |
Correct |
46 ms |
536 KB |
Output is correct |
7 |
Correct |
44 ms |
472 KB |
Output is correct |
8 |
Correct |
46 ms |
592 KB |
Output is correct |
9 |
Correct |
46 ms |
344 KB |
Output is correct |
10 |
Correct |
46 ms |
348 KB |
Output is correct |
11 |
Correct |
46 ms |
348 KB |
Output is correct |
12 |
Correct |
46 ms |
344 KB |
Output is correct |
13 |
Correct |
46 ms |
588 KB |
Output is correct |
14 |
Correct |
47 ms |
576 KB |
Output is correct |
15 |
Correct |
2 ms |
344 KB |
Output is correct |
16 |
Correct |
8 ms |
348 KB |
Output is correct |
17 |
Correct |
52 ms |
604 KB |
Output is correct |
18 |
Correct |
54 ms |
604 KB |
Output is correct |
19 |
Correct |
51 ms |
608 KB |
Output is correct |
20 |
Correct |
50 ms |
600 KB |
Output is correct |
21 |
Correct |
51 ms |
600 KB |
Output is correct |
22 |
Correct |
51 ms |
552 KB |
Output is correct |
23 |
Correct |
51 ms |
604 KB |
Output is correct |
24 |
Correct |
51 ms |
604 KB |
Output is correct |
25 |
Correct |
51 ms |
560 KB |
Output is correct |
26 |
Correct |
51 ms |
612 KB |
Output is correct |
27 |
Correct |
3 ms |
344 KB |
Output is correct |
28 |
Correct |
9 ms |
348 KB |
Output is correct |
29 |
Correct |
9 ms |
560 KB |
Output is correct |
30 |
Execution timed out |
10045 ms |
7800 KB |
Time limit exceeded |
31 |
Halted |
0 ms |
0 KB |
- |