#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);
}
}
vector<set<pair<int, int>>> st_hor(4 * k);
vector<set<pair<int, int>>> st_ver(4 * k);
vector<int> qv;
function<void(int, int, int, int, int, int, pair<int, int>)> Insert = [&](int t, int x, int l, int r, int ll, int rr, pair<int, int> p) {
if (ll <= l && r <= rr) {
if (t == 0) {
st_hor[x].insert(p);
} else {
st_ver[x].insert(p);
}
return;
}
int mid = l + r >> 1;
if (rr <= mid) {
Insert(t, x << 1, l, mid, ll, rr, p);
} else if (ll > mid) {
Insert(t, x << 1 | 1, mid + 1, r, ll, rr, p);
} else {
Insert(t, x << 1, l, mid, ll, rr, p);
Insert(t, x << 1 | 1, mid + 1, r, ll, rr, p);
}
};
function<void(int, int, int, int, int, int, pair<int, int>)> Remove = [&](int t, int x, int l, int r, int ll, int rr, pair<int, int> p) {
if (ll <= l && r <= rr) {
if (t == 0) {
st_hor[x].erase(p);
} else {
st_ver[x].erase(p);
}
return;
}
int mid = l + r >> 1;
if (rr <= mid) {
Remove(t, x << 1, l, mid, ll, rr, p);
} else if (ll > mid) {
Remove(t, x << 1 | 1, mid + 1, r, ll, rr, p);
} else {
Remove(t, x << 1, l, mid, ll, rr, p);
Remove(t, x << 1 | 1, mid + 1, r, ll, rr, p);
}
};
function<void(int, int, int, int, int, int, int)> Query = [&](int t, int x, int l, int r, int i, int ll, int rr) {
if (t == 0) {
while (true) {
auto it = st_hor[x].lower_bound(make_pair(ll, -1));
if (it != st_hor[x].end() && it->first <= rr) {
int idx = it->second;
qv.push_back(idx);
Remove(0, 1, 0, k - 1, hor[idx][1], hor[idx][2], make_pair(hor[idx][0], idx));
} else {
break;
}
}
} else {
while (true) {
auto it = st_ver[x].lower_bound(make_pair(ll, -1));
if (it != st_ver[x].end() && it->first <= rr) {
int idx = it->second;
qv.push_back(idx);
Remove(1, 1, 0, k - 1, ver[idx][1], ver[idx][2], make_pair(ver[idx][0], idx));
} else {
break;
}
}
}
if (l == r) {
return;
}
int mid = l + r >> 1;
if (i <= mid) {
Query(t, x << 1, l, mid, i, ll, rr);
} else {
Query(t, x << 1 | 1, mid + 1, r, i, ll, rr);
}
};
for (int i = 0; i < sh; i++) {
if (dist[i] == -1) {
Insert(0, 1, 0, k - 1, hor[i][1], hor[i][2], make_pair(hor[i][0], i));
}
}
for (int i = 0; i < sv; i++) {
if (dist[sh + i] == -1) {
Insert(1, 1, 0, k - 1, ver[i][1], ver[i][2], make_pair(ver[i][0], i));
}
}
for (int q = 0; q < (int) que.size(); q++) {
int x = que[q];
if (x < sh) {
qv.clear();
Query(1, 1, 0, k - 1, hor[x][0], hor[x][1], hor[x][2]);
for (int i : qv) {
dist[sh + i] = dist[x] + 1;
que.push_back(sh + i);
}
} else {
x -= sh;
qv.clear();
Query(0, 1, 0, k - 1, ver[x][0], ver[x][1], ver[x][2]);
for (int i : qv) {
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;
}
Compilation message
golf.cpp: In lambda function:
golf.cpp:210:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
210 | int mid = l + r >> 1;
| ~~^~~
golf.cpp: In lambda function:
golf.cpp:229:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
229 | int mid = l + r >> 1;
| ~~^~~
golf.cpp: In lambda function:
golf.cpp:266:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
266 | int mid = l + r >> 1;
| ~~^~~
# |
결과 |
실행 시간 |
메모리 |
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 |
604 KB |
Output is correct |
5 |
Correct |
38 ms |
1516 KB |
Output is correct |
6 |
Correct |
40 ms |
1616 KB |
Output is correct |
7 |
Correct |
38 ms |
1616 KB |
Output is correct |
8 |
Correct |
39 ms |
1588 KB |
Output is correct |
9 |
Correct |
39 ms |
1620 KB |
Output is correct |
10 |
Correct |
40 ms |
1620 KB |
Output is correct |
11 |
Correct |
40 ms |
1616 KB |
Output is correct |
12 |
Correct |
40 ms |
1620 KB |
Output is correct |
13 |
Correct |
40 ms |
1596 KB |
Output is correct |
14 |
Correct |
40 ms |
1624 KB |
Output is correct |
15 |
Correct |
3 ms |
860 KB |
Output is correct |
16 |
Correct |
7 ms |
1260 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 |
604 KB |
Output is correct |
5 |
Correct |
38 ms |
1516 KB |
Output is correct |
6 |
Correct |
40 ms |
1616 KB |
Output is correct |
7 |
Correct |
38 ms |
1616 KB |
Output is correct |
8 |
Correct |
39 ms |
1588 KB |
Output is correct |
9 |
Correct |
39 ms |
1620 KB |
Output is correct |
10 |
Correct |
40 ms |
1620 KB |
Output is correct |
11 |
Correct |
40 ms |
1616 KB |
Output is correct |
12 |
Correct |
40 ms |
1620 KB |
Output is correct |
13 |
Correct |
40 ms |
1596 KB |
Output is correct |
14 |
Correct |
40 ms |
1624 KB |
Output is correct |
15 |
Correct |
3 ms |
860 KB |
Output is correct |
16 |
Correct |
7 ms |
1260 KB |
Output is correct |
17 |
Correct |
42 ms |
3408 KB |
Output is correct |
18 |
Correct |
41 ms |
3152 KB |
Output is correct |
19 |
Correct |
42 ms |
3412 KB |
Output is correct |
20 |
Correct |
42 ms |
3364 KB |
Output is correct |
21 |
Correct |
43 ms |
3384 KB |
Output is correct |
22 |
Correct |
43 ms |
3412 KB |
Output is correct |
23 |
Correct |
44 ms |
3408 KB |
Output is correct |
24 |
Correct |
43 ms |
3412 KB |
Output is correct |
25 |
Correct |
43 ms |
3372 KB |
Output is correct |
26 |
Correct |
43 ms |
3400 KB |
Output is correct |
27 |
Correct |
4 ms |
860 KB |
Output is correct |
28 |
Correct |
9 ms |
1624 KB |
Output is correct |
29 |
Correct |
9 ms |
1628 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 |
604 KB |
Output is correct |
5 |
Correct |
38 ms |
1516 KB |
Output is correct |
6 |
Correct |
40 ms |
1616 KB |
Output is correct |
7 |
Correct |
38 ms |
1616 KB |
Output is correct |
8 |
Correct |
39 ms |
1588 KB |
Output is correct |
9 |
Correct |
39 ms |
1620 KB |
Output is correct |
10 |
Correct |
40 ms |
1620 KB |
Output is correct |
11 |
Correct |
40 ms |
1616 KB |
Output is correct |
12 |
Correct |
40 ms |
1620 KB |
Output is correct |
13 |
Correct |
40 ms |
1596 KB |
Output is correct |
14 |
Correct |
40 ms |
1624 KB |
Output is correct |
15 |
Correct |
3 ms |
860 KB |
Output is correct |
16 |
Correct |
7 ms |
1260 KB |
Output is correct |
17 |
Correct |
42 ms |
3408 KB |
Output is correct |
18 |
Correct |
41 ms |
3152 KB |
Output is correct |
19 |
Correct |
42 ms |
3412 KB |
Output is correct |
20 |
Correct |
42 ms |
3364 KB |
Output is correct |
21 |
Correct |
43 ms |
3384 KB |
Output is correct |
22 |
Correct |
43 ms |
3412 KB |
Output is correct |
23 |
Correct |
44 ms |
3408 KB |
Output is correct |
24 |
Correct |
43 ms |
3412 KB |
Output is correct |
25 |
Correct |
43 ms |
3372 KB |
Output is correct |
26 |
Correct |
43 ms |
3400 KB |
Output is correct |
27 |
Correct |
4 ms |
860 KB |
Output is correct |
28 |
Correct |
9 ms |
1624 KB |
Output is correct |
29 |
Correct |
9 ms |
1628 KB |
Output is correct |
30 |
Execution timed out |
10072 ms |
4456 KB |
Time limit exceeded |
31 |
Halted |
0 ms |
0 KB |
- |