This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
//#pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
#define ll long long
//#define int long long
const int nmax = 1e6 + 5, N = 1e6;
const ll oo = 1e9 + 5, base = 311;
const int lg = 17, tx = 26;
const ll mod = 998244353;
#define pii pair<int, int>
#define fi first
#define se second
#define debug(a, n) for(int i = 1; i <= n; ++i) cout << a[i] << ' '; cout << "\n";
using namespace std;
struct node{
int x1, x2, y1, y2;
}a[nmax];
int n, rt, S, T, U, V;
vector<int> nen;
int gg(int u){
return lower_bound(nen.begin(), nen.end(), u) - nen.begin() + 1;
}
struct holder{
int x, l, r;
};
vector<holder> A, B;
int tree[nmax << 2], lz[nmax << 2];
void build(int idx){
if(!idx) lz[1] = gg(-oo);
else lz[1] = gg(oo);
}
void fix(int id, int val){
if(!val) return;
tree[id] = val;
lz[id] = val;
}
void down(int id){
fix(id << 1, lz[id]);
fix(id << 1 | 1, lz[id]);
lz[id] = 0;
}
void update(int id, int l, int r, int u, int v, int val){
if(l > v || r < u || u > v) return;
if(l >= u && r <= v) return fix(id, val);
down(id);
int mid = r + l >> 1;
update(id << 1, l, mid, u, v,val);
update(id << 1 | 1, mid + 1, r, u, v, val);
}
#define update(l, r, val) update(1, 1, rt, l, r, val)
int get(int id, int l, int r, int u){
if(l == r) return tree[id];
down(id);
int mid = r + l >> 1;
if(u <= mid)return get(id << 1, l, mid, u);
return get(id << 1 | 1, mid + 1, r, u);
}
#define get(x) get(1, 1, rt, x)
int L[nmax], R[nmax];
void add(int x1, int y1, int x2, int y2){
if(x1 > x2) swap(x1, x2);
if(y1 > y2) swap(y1, y2);
A.push_back({y1, x1, x2});
A.push_back({y2, x1, x2});
B.push_back({x1, y1, y2});
B.push_back({x2, y1, y2});
}
struct hos{
int x, l, r, id;
};
vector<holder> one,two;
vector<int> adj[nmax];
//A doc
//B ngang
int ans[nmax][2];
deque<pii> q;
void solve(){
//tao canh
S = gg(S), T = gg(T);
U = gg(U), V = gg(V);
// cout << nen[U - 1]<< ' ' << nen[V - 1] << ' ';
add(gg(-oo), gg(-oo), gg(oo), gg(oo));
for(int i = 1; i <= n; ++i){
A.push_back({a[i].y1, a[i].x1 + 1, a[i].x2 - 1});
A.push_back({a[i].y2, a[i].x1 + 1, a[i].x2 - 1});
B.push_back({a[i].x1, a[i].y1 + 1, a[i].y2 - 1});
B.push_back({a[i].x2, a[i].y1 + 1, a[i].y2 - 1});
}
one.push_back({gg(-oo), gg(-oo), gg(oo)});
one.push_back({gg(oo), gg(-oo), gg(oo)});
two.push_back({gg(-oo), gg(-oo), gg(oo)});
two.push_back({gg(oo), gg(-oo), gg(oo)});
sort(A.begin(), A.end(), [](holder A, holder B){
return A.x < B.x;
});
vector<pii> tmp;
tmp.push_back({S, T});
tmp.push_back({U, V});
for(int i = 1; i <= n; ++i){
tmp.push_back({a[i].x1, a[i].y1});
tmp.push_back({a[i].x1, a[i].y2});
tmp.push_back({a[i].x2, a[i].y1});
tmp.push_back({a[i].x2, a[i].y2});
}
//#####
sort(tmp.begin(), tmp.end(), [](pii a, pii b){
return a.se < b.se;
});
build(0);
int pos = 0;
for(int i = 0; i < tmp.size(); ++i){
int x = tmp[i].fi, y = tmp[i].se;
while(pos < A.size() && A[pos].x < y){
update(A[pos].l, A[pos].r, A[pos].x);
++pos;
}
L[i] = get(x);
}
build(1);
pos = 0;
reverse(A.begin(), A.end());
for(int i = tmp.size() - 1; i >= 0; --i){
int x = tmp[i].fi, y = tmp[i].se;
while(pos < A.size() && A[pos].x > y){
update(A[pos].l, A[pos].r, A[pos].x);
++pos;
}
R[i] = get(x);
}
for(int i = 0; i < tmp.size(); ++i){
one.push_back({tmp[i].fi, L[i], R[i]});
}
//######
sort(B.begin(), B.end(), [](holder A, holder B){
return A.x < B.x;
});
sort(tmp.begin(), tmp.end(), [](pii a, pii b){
return a.fi < b.fi;
});
build(0); pos = 0;
for(int i = 0; i < tmp.size(); ++i){
int x = tmp[i].fi, y = tmp[i].se;
while(pos < B.size() && B[pos].x < x){
update(B[pos].l, B[pos].r, B[pos].x);
++pos;
}
L[i] = get(y);
}
build(1); pos = 0;
reverse(B.begin(), B.end());
for(int i = tmp.size() - 1; i >= 0; --i){
int x = tmp[i].fi, y = tmp[i].se;
while(pos < B.size() && B[pos].x > x){
update(B[pos].l, B[pos].r, B[pos].x);
++pos;
}
R[i] = get(y);
}
for(int i = 0; i < tmp.size(); ++i){
two.push_back({tmp[i].se, L[i], R[i]});
}
//#####
memset(ans, 0x3f, sizeof ans);
for(int i = 0; i < one.size(); ++i){
if(S == one[i].x && one[i].l <= T && one[i].r >= T ){
q.push_back({i, 0});
ans[i][0] = 1;
}
}
for(int i = 0; i < two.size(); ++i){
if(T == two[i].x && two[i].l <= S && two[i].r >= S){
q.push_back({i, 1});
ans[i][1] = 1;
}
}
while(q.size()){
pii tmp = q.front();q.pop_front();
if(tmp.se == 0){
for(int j = 0; j < two.size(); ++j){
int x = one[tmp.fi].x, l = one[tmp.fi].l, r = one[tmp.fi].r;
int y = two[j].x, le = two[j].l, ri = two[j].r;
if(le <= x && x <= ri && l <= y && y <= r){
if(ans[j][1] > oo){
ans[j][1] = ans[tmp.fi][0] + 1;
q.push_back({j, 1});
}
}
}
}
else{
for(int j = 0; j < one.size(); ++j){
int x = two[tmp.fi].x, l = two[tmp.fi].l, r = two[tmp.fi].r;
int y = one[j].x, le = one[j].l, ri = one[j].r;
if(le <= x && x <= ri && l <= y && y <= r){
if(ans[j][0] > oo){
ans[j][0] = ans[tmp.fi][1] + 1;
q.push_back({j, 0});
}
}
}
}
}
int mi = oo;
for(int i = 0; i < one.size(); ++i){
if(U == one[i].x && one[i].l <= V && one[i].r >= V ){
mi = min(ans[i][0], mi);
}
// cout << nen[one[i].x - 1]<< ' ' << nen[one[i].l - 1] << ' ' << nen[one[i].r - 1] << ' ' << ans[i][0] << endl;
}
// cout <<"###\n";
for(int i = 0; i < two.size(); ++i){
if(V == two[i].x && two[i].l <= U && two[i].r >= U){
mi = min(mi, ans[i][1]);
}
// cout << nen[two[i].x - 1] << ' ' << nen[two[i].l - 1] << ' ' << nen[two[i].r - 1] << ' ' << ans[i][1] << endl;
}
cout << mi;
}
main(){
ios_base::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
// freopen("code.inp", "r", stdin);
// freopen("code.out", "w", stdout);
cin >> S >> T >> U >> V >> n;
for(int i = 1; i <= n; ++i){
cin >> a[i].x1 >> a[i].x2 >> a[i].y1 >> a[i].y2;
nen.push_back(a[i].x1);
nen.push_back(a[i].x2);
nen.push_back(a[i].y1);
nen.push_back(a[i].y2);
nen.push_back(a[i].x1 - 1);
nen.push_back(a[i].x2 + 1);
nen.push_back(a[i].y1 - 1);
nen.push_back(a[i].y2 + 1);
}
for(int x = -1; x <= 1; ++x){
nen.push_back(S + x); nen.push_back(U + x);
nen.push_back(T + x); nen.push_back(V + x);
nen.push_back(-oo + x); nen.push_back(oo + x);
}
// for(int i = 1; i <= n; ++i){
// for(int x = a[i].x1; x <= a[i].x2;++x){
// for(int y = a[i].y1; y <= a[i].y2; ++y){
// vis[x][y] = 1;
// }
// }
// }
// vis[S][T] = vis[U][V]= 2;
// for(int i = 0; i <= 30; ++i){
// for(int j = 0; j <= 30; ++j){
// if(vis[i][j] == 1) cout << "#";
// else if(vis[i][j] == 2) cout << "?";
// else cout << ".";
// }
// cout << endl;
// }
sort(nen.begin(), nen.end());
nen.erase(unique(nen.begin(), nen.end()), nen.end());
rt = nen.size();
for(int i = 1; i <= n; ++i){
a[i].x1 = gg(a[i].x1);
a[i].x2 = gg(a[i].x2);
a[i].y1 = gg(a[i].y1);
a[i].y2 = gg(a[i].y2);
}
//tao canh
solve();
}
/*
0 0 5 5
1
2 3 2 4
3 5 8 6
1
5 6 2 8
*/
Compilation message (stderr)
golf.cpp: In function 'void update(int, int, int, int, int, int)':
golf.cpp:51:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
51 | int mid = r + l >> 1;
| ~~^~~
golf.cpp: In function 'int get(int, int, int, int)':
golf.cpp:60:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
60 | int mid = r + l >> 1;
| ~~^~~
golf.cpp: In function 'void solve()':
golf.cpp:119:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
119 | for(int i = 0; i < tmp.size(); ++i){
| ~~^~~~~~~~~~~~
golf.cpp:121:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<holder>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
121 | while(pos < A.size() && A[pos].x < y){
| ~~~~^~~~~~~~~~
golf.cpp:132:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<holder>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
132 | while(pos < A.size() && A[pos].x > y){
| ~~~~^~~~~~~~~~
golf.cpp:138:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
138 | for(int i = 0; i < tmp.size(); ++i){
| ~~^~~~~~~~~~~~
golf.cpp:150:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
150 | for(int i = 0; i < tmp.size(); ++i){
| ~~^~~~~~~~~~~~
golf.cpp:152:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<holder>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
152 | while(pos < B.size() && B[pos].x < x){
| ~~~~^~~~~~~~~~
golf.cpp:162:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<holder>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
162 | while(pos < B.size() && B[pos].x > x){
| ~~~~^~~~~~~~~~
golf.cpp:168:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
168 | for(int i = 0; i < tmp.size(); ++i){
| ~~^~~~~~~~~~~~
golf.cpp:174:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<holder>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
174 | for(int i = 0; i < one.size(); ++i){
| ~~^~~~~~~~~~~~
golf.cpp:180:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<holder>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
180 | for(int i = 0; i < two.size(); ++i){
| ~~^~~~~~~~~~~~
golf.cpp:189:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<holder>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
189 | for(int j = 0; j < two.size(); ++j){
| ~~^~~~~~~~~~~~
golf.cpp:202:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<holder>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
202 | for(int j = 0; j < one.size(); ++j){
| ~~^~~~~~~~~~~~
golf.cpp:215:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<holder>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
215 | for(int i = 0; i < one.size(); ++i){
| ~~^~~~~~~~~~~~
golf.cpp:223:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<holder>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
223 | for(int i = 0; i < two.size(); ++i){
| ~~^~~~~~~~~~~~
golf.cpp: At global scope:
golf.cpp:232:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
232 | main(){
| ^~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |