Language: C++

#include<bits/stdc++.h>
#define f first
#define s second
#define mk make_pair
#define P pair<int,int>
using namespace std;
int w,h,m,sx,sy,gx,gy;
vector<pair<P,P> >v;
vector<int>ax,ay;
map<int,int>mx,my;
map<P,int>qx,qy;
int a[1001][1001],ppp;
int dx[]={0,0,1,-1};
int dy[]={1,-1,0,0};
int ddx[]={2,0,0,-2};
int ddy[]={0,2,-2,0};
vector<int>g[405];
bool used[1005][1005];
void Grip(){
  mx[0]=0;my[0]=0;
  int pre=0;
  for(int i=1;i<ax.size();i++){
    int pp=ax[i];
      ax[i]=ax[i-1]+2;
    qx[mk(pre,pp)]=ax[i]-1;
    mx[pp]=ax[i];
    pre=pp;
  }
  pre=0;
  for(int i=1;i<ay.size();i++){
    int pp=ay[i];
      ay[i]=ay[i-1]+2;
    qy[mk(pre,pp)]=ay[i]-1;
    my[pp]=ay[i];
    pre=pp;
  }
  h=ay[ay.size()-1];
  w=ax[ax.size()-1];
}
void mapmaker(){
  for(int i=0;i<v.size();i++){
    int sx=mx[v[i].f.f],sy=my[v[i].f.s];
    int gx=mx[v[i].s.f],gy=my[v[i].s.s];
    for(int j=sx;j<=gx;j++)
      for(int k=sy;k<=gy;k++)
        a[k][j]=-1;
  }
  for(int i=0;i<=w;i++)a[h][i]=a[0][i]=-1;
  for(int i=0;i<=h;i++)a[i][0]=a[i][w]=-1;
}
void fullfill(int y,int x,int d){
  a[y][x]=d;
  for(int i=0;i<4;i++){
    int rx=x+dx[i],ry=y+dy[i];
    if(rx>=0&&ry>=0&&rx<=w&&ry<=h)
      if(!a[ry][rx])fullfill(ry,rx,d);
  }
}
void graphmaker(int y,int x){
  used[y][x]=1;
  for(int i=0;i<4;i++){
    int rx=x+ddx[i],ry=y+ddy[i];
    if(rx>=0&&ry>=0&&rx<=w&&ry<=h)
      if(a[ry][rx]!=a[y][x]&&a[ry][rx]>0)
        g[a[y][x]].push_back(a[ry][rx]);
  }
  for(int i=0;i<4;i++){
    int rx=x+dx[i],ry=y+dy[i];
      if(rx>=0&&ry>=0&&rx<=h&&ry<=w)
        if(!a[ry][rx]&&used[ry][rx]==0)graphmaker(ry,rx);
  }
}
int main(){
  cin>>w>>h>>m;
    memset(a,0,sizeof(a));
    ax.clear();ay.clear();v.clear();
    mx.clear();my.clear();
    qx.clear();qy.clear();
    ax.push_back(w*2);ax.push_back(0);
    ay.push_back(h*2);ay.push_back(0);
    for(int i=0;i<405;i++)g[i].clear();
    for(int i=0;i<m;i++){
      cin>>sx>>sy>>gx>>gy;
      v.push_back(mk(mk(sx*2,sy*2),mk(gx*2,gy*2)));
      ax.push_back(sx*2);ax.push_back(gx*2);
      ay.push_back(sy*2);ay.push_back(gy*2);
    }
    sort(ax.begin(),ax.end());
    sort(ay.begin(),ay.end());
    ax.erase(unique(ax.begin(),ax.end()),ax.end());
    ay.erase(unique(ay.begin(),ay.end()),ay.end());
    Grip();
    mapmaker();
   // fullfill(490,490,-1);
    int cc=1;
    for(int i=0;i<=h;i++)
      for(int j=0;j<=w;j++){
        if(!a[i][j])fullfill(i,j,cc++);
      }
    memset(used,0,sizeof(used));
    for(int i=0;i<h;i++)
      for(int j=0;j<w;j++)
        if(0<a[i][j]&&!used[i][j])
          graphmaker(i,j);
    for(int i=0;i<405;i++)
      if(g[i].size())
        g[i].erase(unique(g[i].begin(),g[i].end()),g[i].end());
    cin>>m;
    while(m--){
      cin>>sx>>sy>>gx>>gy;sx*=2;sy*=2;gx*=2;gy*=2;
      for(map<P,int>::iterator it=qx.begin();it!=qx.end();it++)
        if((*it).f.f<sx&&(*it).f.s>=sx){sx=(*it).s;break;}
      for(map<P,int>::iterator it=qx.begin();it!=qx.end();it++)
        if((*it).f.f<gx&&(*it).f.s>=gx){gx=(*it).s;break;}
      for(map<P,int>::iterator it=qy.begin();it!=qy.end();it++)
        if((*it).f.f<sy&&(*it).f.s>=sy){sy=(*it).s;break;}
      for(map<P,int>::iterator it=qy.begin();it!=qy.end();it++)
        if((*it).f.f<gy&&(*it).f.s>=gy){gy=(*it).s;break;}
if(a[sy][sx]<0){
if(a[sy+1][sx]>0)sy++;
else if(a[sy][sx+1]>0)sx++;
else sy++,sx++;
}
if(a[gy][gx]<0){
if(a[gy+1][gx]>0)gy++;
else if(a[gy][gx+1]<0)gxx++;
else gy++,gx++;
}
      int so=a[sy][sx],go=a[gy][gx];
      int use[100]={};
      use[so]=1;
      queue<pair<int,int> >q;
      q.push(mk(so,0));
      while(!q.empty()){
        P u=q.front();q.pop();
        if(u.f==go){
          cout<<u.s<<endl;
          goto L;
        }
        for(int i=0;i<g[u.f].size();i++)
          if(!use[g[u.f][i]]){
            use[g[u.f][i]]=1;
            q.push(mk(g[u.f][i],u.s+1));
          }
      }L:;
    }
}

Compiler output:
a.cc: In function 'int main()':
a.cc:126:23: error: 'gxx' was not declared in this scope; did you mean 'gx'?
  126 | else if(a[gy][gx+1]<0)gxx++;
      |                       ^~~
      |                       gx