#include<bits/stdc++.h>
using namespace std;
#ifndef BADGNU
#pragma GCC target("sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
#endif
#pragma GCC optimize("Ofast,unroll-loops,fast-math,O3")
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ll long long
// #define int ll
#define ld long double
#define y1 cheza
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<class T> using ordered_set = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
template<class T> using ordered_multiset = tree<T,null_type,less_equal<T>,rb_tree_tag,tree_order_statistics_node_update>;
const int N=4100;
const int M=5001;
const int B=447;
const int mod=998244353;
const ll INF=1e18;
const int dx[]={1,-1,0,0};
const int dy[]={0,0,1,-1};
const double eps=1e-6;
pair<int,int>s,t;
int n;
int dp[N][N];
int ad[N][N];
void test(){
cin>>s.first>>s.second>>t.first>>t.second;
cin>>n;
vector<tuple<int,int,int,int>>v;
for(int i=1,lx,rx,ly,ry;i<=n;i++){
cin>>lx>>rx>>ly>>ry;
v.push_back({lx,rx,ly,ry});
}
{
map<ld,int>memx,memy;
int tin=0;
memx[s.first]=1;
memx[t.first]=1;
memy[s.second]=1;
memy[t.second]=1;
for(auto [lx,rx,ly,ry]:v){
memx[lx]=1;
memx[rx]=1;
memy[ry]=1;
memy[ly]=1;
memx[lx+0.5]=1;
memx[rx-0.5]=1;
memy[ry-0.5]=1;
memy[ly+0.5]=1;
}
tin=0;
for(auto &i:memx){
i.second=++tin;
}
tin=0;
for(auto &i:memy){
i.second=++tin;
}
s.first=memx[s.first];
s.second=memy[s.second];
t.first=memx[t.first];
t.second=memy[t.second];
for(auto &[lx,rx,ly,ry]:v){
lx=memx[lx+0.5];
rx=memx[rx-0.5];
ly=memy[ly+0.5];
ry=memy[ry-0.5];
}
}
// cout<<s.first<<' '<<s.second<<' '<<t.first<<' '<<t.second<<'\n';
for(auto [lx,rx,ly,ry]:v){
// cout<<lx<<' '<<rx<<' '<<ly<<' '<<ry<<'\n';
ad[lx][ly]++;
ad[lx][ry+1]--;
ad[rx+1][ly]--;
ad[rx+1][ry+1]++;
}
for(int i=1;i<=n*4+2;i++){
for(int j=1;j<=n*4;j++){
ad[i][j]+=(ad[i-1][j]+ad[i][j-1]-ad[i-1][j-1]);
}
}
for(int i=1;i<=n*4+2;i++){
for(int j=1;j<=n*4+2;j++){
dp[i][j]=-1;
}
}
dp[s.first][s.second]=0;
queue<pair<int,int>>q;
q.push(s);
// cout<<s.first<<' '<<s.second<<' '<<t.first<<' '<<t.second<<'\n';
while(q.size()){
auto x=q.front();
q.pop();
for(int j=x.second;j<=n*4+2;j++){
if(ad[x.first][j])break;
if(dp[x.first][j]==-1){
dp[x.first][j]=dp[x.first][x.second]+1;
q.push({x.first,j});
}
}
for(int j=x.second;j>=1;j--){
if(ad[x.first][j])break;
if(dp[x.first][j]==-1){
dp[x.first][j]=dp[x.first][x.second]+1;
q.push({x.first,j});
}
}
for(int j=x.first;j<=n*4+2;j++){
if(ad[j][x.second])break;
if(dp[j][x.second]==-1){
dp[j][x.second]=dp[x.first][x.second]+1;
q.push({j,x.second});
}
}
for(int j=x.first;j>=1;j--){
if(ad[j][x.second])break;
if(dp[j][x.second]==-1){
dp[j][x.second]=dp[x.first][x.second]+1;
q.push({j,x.second});
}
}
}
cout<<dp[t.first][t.second]<<'\n';
}
/*
*/
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
// cout.tie(nullptr);
int t2=1;
// cin>>t2;
for(int i=1;i<=t2;i++){
test();
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |