#include <bits/stdc++.h>
//qwerty47924692
using namespace std;
using ll = long long;
const ll N=2515;
const string br="617283";
#define sz(a)(ll)a.size()
#define f first
#define s second
ll a[N][N],xa,ya,xb,yb,d[N][N],used[N][N],n,can[N][N][4];
ll dx[4]={1,0,0,-1},dy[4]={0,1,-1,0};
void bfs(ll xs,ll ys){
deque<pair<ll,ll>>q;
q.push_front({xs,ys});
d[xs][ys]=0;
while(q.size()){
ll x=q.front().f;
ll y=q.front().s;
q.pop_front();
if(used[x][y])continue;
used[x][y]=1;
for(ll i=0;i<4;i++){
ll nx=x+dx[i];
ll ny=y+dy[i];
if(nx<1||nx>2500||ny<1||ny>2500)continue;
if(a[nx][ny])continue;
// if(x==2&&y==5)cout<<nx<<' '<<ny<<'\n';
if(d[nx][ny]>d[x][y]+can[x][y][i]){
d[nx][ny]=d[x][y]+can[x][y][i];
for(ll j=0;j<4;j++)can[nx][ny][j]=1;
can[nx][ny][i]=0;
}
if(d[nx][ny]==d[x][y]+can[x][y][i]){
can[nx][ny][i]=0;
if(can[x][y][i])q.push_back({nx,ny});
else q.push_front({nx,ny});
}
}
}
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cin>>xa>>ya>>xb>>yb;
xa+=10;
xb+=10;
ya+=10;
yb+=10;
xa*=2;
ya*=2;
xb*=2;
yb*=2;
cin>>n;
for(ll i=1;i<=n;i++){
ll xa,ya,xb,yb;
cin>>xa>>xb>>ya>>yb;
xa+=10;
xb+=10;
ya+=10;
yb+=10;
xa*=2;
ya*=2;
xb*=2;
yb*=2;
xa++;
xb--;
ya++;
yb--;
a[xa][ya]++;
a[xb+1][ya]--;
a[xa][yb+1]--;
a[xb+1][yb+1]++;
}
for(ll i=1;i<=2500;i++){
for(ll j=1;j<=2500;j++){
a[i][j]+=a[i-1][j]+a[i][j-1]-a[i-1][j-1];
d[i][j]=1e18;
for(ll k=0;k<4;k++)can[i][j][k]=1;
}
}
bfs(xa,ya);
cout<<d[xb][yb];
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |