#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];
set<int>px[N],py[N];
int calc(int x1,int y1,int x2,int y2){
if(x1>x2)swap(x1,x2);
if(y1>y2)swap(y1,y2);
return ad[x2][y2]-ad[x1-1][y2]-ad[x2][y1-1]+ad[x1-1][y1-1];
}
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+2;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++){
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;
if(calc(i,j,i,j)==0){
px[i].insert(j);
py[j].insert(i);
}
// cout<<calc(i,j,i,j)<<' ';
}
// cout<<'\n';
}
dp[s.first][s.second]=0;
px[s.first].erase(s.second);
py[s.second].erase(s.first);
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();
if(px[x.first].size()>0){
auto it=px[x.first].lower_bound(x.second);
while(it!=px[x.first].end()){
if(calc(x.first,x.second,x.first,*it)!=0)break;
if(dp[x.first][*it]==-1){
dp[x.first][*it]=dp[x.first][x.second]+1;
q.push({x.first,*it});
py[*it].erase(x.first);
it=px[x.first].erase(it);
}
}
}
if(px[x.first].size()>0){
auto it=px[x.first].lower_bound(x.second);
it--;
while(it!=px[x.first].end()){
if(calc(x.first,*it,x.first,x.second)!=0)break;
if(dp[x.first][*it]==-1){
dp[x.first][*it]=dp[x.first][x.second]+1;
q.push({x.first,*it});
py[*it].erase(x.first);
it=px[x.first].erase(it);
if(it==px[x.first].begin()){
break;
}
it--;
}
}
}
if(py[x.second].size()>0){
auto it=py[x.second].lower_bound(x.first);
while(it!=py[x.second].end()){
if(calc(x.first,x.second,*it,x.second)!=0)break;
if(dp[*it][x.second]==-1){
dp[*it][x.second]=dp[x.first][x.second]+1;
q.push({*it,x.second});
px[*it].erase(x.second);
it=py[x.second].erase(it);
}
}
}
if(py[x.second].size()>0){
auto it=py[x.second].lower_bound(x.first);
it--;
while(it!=py[x.second].end()){
if(calc(x.first,x.second,*it,x.second)!=0)break;
if(dp[*it][x.second]==-1){
dp[*it][x.second]=dp[x.first][x.second]+1;
q.push({*it,x.second});
px[*it].erase(x.second);
it=py[x.second].erase(it);
if(it==py[x.second].begin()){
break;
}
it--;
}
}
}
}
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... |