Problem p00287 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
#define pb push_back
#define fi first
#define sc second
typedef long long int ll;
ll W, H;
int M;
ll px[114];
ll py[114];
ll qx[114];
ll qy[114];
vector<ll> x, y;
int Q;
int grid[501][501];
int s[250001];
int dx[4] = {-1, 0, 0, 1};
int dy[4] = {0, -1, 1, 0};
int sx1, sy1, gx1, gy1;
int WW, HH;
vector<int> G[250001];
map<P, int> MAP;
/*
int dfs(int x1,int y1){
        f[x1][y1]=true;
        for(int k=0;k<4;k++){
                if(x1+dx[k]>=0&&x1+dx[k]<WW&&y1+dy[k]>=0&&y1+dy[k]<HH){
                        if(!f[x1+dx[k]][y1+dy[k]])s[x1+dx[k]][y1+dy[k]]=min(s[x1][y1],dfs(x1+dx[k],y1+dy[k]));
                }
        }
        if(grid[x1][y1])s[x1][y1]++;
        printf("%d %d %d\\n",x1,y1,s[x1][y1]);
        return s[x1][y1];
}*/
int main() {
  scanf("%lld%lld%d", &W, &H, &M);
  W *= 10;
  H *= 10;
  x.pb(0);
  x.pb(W);
  y.pb(0);
  y.pb(H);
  for (int i = 0; i < M; i++) {
    scanf("%lld%lld%lld%lld", &px[i], &py[i], &qx[i], &qy[i]);
    px[i] *= 10;
    py[i] *= 10;
    qx[i] *= 10;
    qy[i] *= 10;
    if (px[i] != 0) {
      x.pb(px[i] - 1);
    }
    x.pb(px[i]);
    if (px[i] != W) {
      x.pb(px[i] + 1);
    }

    if (qx[i] != 0) {
      x.pb(qx[i] - 1);
    }
    x.pb(qx[i]);
    if (qx[i] != W) {
      x.pb(qx[i] + 1);
    }

    if (py[i] != 0) {
      y.pb(py[i] - 1);
    }
    y.pb(py[i]);
    if (py[i] != H) {
      y.pb(py[i] + 1);
    }

    if (qy[i] != 0) {
      y.pb(qy[i] - 1);
    }
    y.pb(qy[i]);
    if (qy[i] != H) {
      y.pb(qy[i] + 1);
    }
  }
  sort(x.begin(), x.end());
  sort(y.begin(), y.end());
  x.erase(unique(x.begin(), x.end()), x.end());
  y.erase(unique(y.begin(), y.end()), y.end());

  WW = (int)x.size();
  HH = (int)y.size();
  for (int i = 0; i < M; i++) {
    int ax = lower_bound(x.begin(), x.end(), px[i]) - x.begin();
    int bx = lower_bound(x.begin(), x.end(), qx[i]) - x.begin();
    int ay = lower_bound(y.begin(), y.end(), py[i]) - y.begin();
    int by = lower_bound(y.begin(), y.end(), qy[i]) - y.begin();
    if (ax >= bx)
      swap(ax, bx);
    if (ay >= by)
      swap(ay, by);
    for (int j = ax; j <= bx; j++) {
      for (int k = ay; k <= by; k++) {
        grid[j][k] = -1;
      }
    }
  }

  int index = 1;
  for (int i = 0; i < WW; i++) {
    for (int j = 0; j < HH; j++) {
      if (grid[i][j] == 0) {
        queue<P> que;
        que.push(P(i, j));
        while (!que.empty()) {
          P p = que.front();
          que.pop();
          if (grid[p.fi][p.sc] == index)
            continue;
          grid[p.fi][p.sc] = index;
          for (int k = 0; k < 4; k++) {
            if (p.fi + dx[k] >= 0 && p.fi + dx[k] < WW && p.sc + dy[k] >= 0 &&
                p.sc + dy[k] < HH) {
              if (grid[p.fi + dx[k]][p.sc + dy[k]] == 0) {
                que.push(P(p.fi + dx[k], p.sc + dy[k]));
              }
            }
          }
        }
        index++;
      }
    }
  }
  /*
  for(int i=0;i<HH;i++){
          for(int j=0;j<WW;j++){
                  printf("%d ",grid[j][i]);
          }
          printf("\\n");
  }*/

  for (int i = 0; i < WW; i++) {
    for (int j = 0; j < HH; j++) {
      if (grid[i][j] == -1) {
        for (int k = 0; k < 4; k++) {
          if (i + dx[k] >= 0 && i - dx[k] >= 0 && i + dx[k] < WW &&
              i - dx[k] < WW) {
            if (j + dy[k] >= 0 && j - dy[k] >= 0 && j + dy[k] < HH &&
                j - dy[k] < HH) {
              // printf("%d %d\\n",i,j);
              int g1 = grid[i + dx[k]][j + dy[k]];
              int g2 = grid[i - dx[k]][j - dy[k]];
              if (g1 != -1 && g2 != -1 && MAP[P(g1, g2)] == 0 && g1 != g2) {
                G[g1 - 1].pb(g2 - 1);
                G[g2 - 1].pb(g1 - 1);
                MAP[P(g1, g2)] = 1;
                MAP[P(g2, g1)] = 1;
                // printf("%d %d\\n",g1,g2);
              }
            }
          }
        }
      }
    }
  }

  scanf("%d", &Q);
  for (int i = 0; i < Q; i++) {
    for (int j = 0; j < WW; j++) {
      for (int k = 0; k < HH; k++) {
        s[j * HH + k] = 100101001;
        // ds[j][k]=2*(WW+HH)+1;
        // f[j][k]=false;
      }
    }
    ll sx, sy, gx, gy;
    scanf("%lld%lld%lld%lld", &sx, &sy, &gx, &gy);
    sx *= 10;
    sy *= 10;
    gx *= 10;
    gy *= 10;

    sx1 = lower_bound(x.begin(), x.end(), sx) - x.begin();
    gx1 = lower_bound(x.begin(), x.end(), gx) - x.begin();
    sy1 = lower_bound(y.begin(), y.end(), sy) - y.begin();
    gy1 = lower_bound(y.begin(), y.end(), gy) - y.begin();

    // printf("%d:%d-%d:%d\\n",sx1,sy1,gx1,gy1);
    int ss = grid[sx1][sy1] - 1;
    int gg = grid[gx1][gy1] - 1;
    priority_queue<P, vector<P>, greater<P>> que;
    que.push(make_pair(0, ss));
    // printf("%d %d\\n",ss,gg);
    // printf("%d %d\\n",sx1,sy1);
    // int n=0;

    while (!que.empty()) {
      P p = que.top();
      que.pop();
      if (p.fi >= s[p.sc])
        continue;
      // printf("%d %d\\n",p.sc.fi,p.sc.sc);
      s[p.sc] = p.fi;
      for (int j = 0; j < (int)G[p.sc].size(); j++) {
        if (p.fi + 1 < s[G[p.sc][j]]) {
          que.push(P(p.fi + 1, G[p.sc][j]));
        }
      }
    }
    // printf("%d\\n",n);
    printf("%d\\n", s[gg]);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
#define pb push_back
#define fi first
#define sc second
typedef long long int ll;
ll W, H;
int M;
ll px[114];
ll py[114];
ll qx[114];
ll qy[114];
vector<ll> x, y;
int Q;
int grid[401][401];
int s[160001];
int dx[4] = {-1, 0, 0, 1};
int dy[4] = {0, -1, 1, 0};
int sx1, sy1, gx1, gy1;
int WW, HH;
vector<int> G[160001];
map<P, int> MAP;
/*
int dfs(int x1,int y1){
        f[x1][y1]=true;
        for(int k=0;k<4;k++){
                if(x1+dx[k]>=0&&x1+dx[k]<WW&&y1+dy[k]>=0&&y1+dy[k]<HH){
                        if(!f[x1+dx[k]][y1+dy[k]])s[x1+dx[k]][y1+dy[k]]=min(s[x1][y1],dfs(x1+dx[k],y1+dy[k]));
                }
        }
        if(grid[x1][y1])s[x1][y1]++;
        printf("%d %d %d\\n",x1,y1,s[x1][y1]);
        return s[x1][y1];
}*/
int main() {
  scanf("%lld%lld%d", &W, &H, &M);
  W *= 10;
  H *= 10;
  x.pb(0);
  x.pb(W);
  y.pb(0);
  y.pb(H);
  for (int i = 0; i < M; i++) {
    scanf("%lld%lld%lld%lld", &px[i], &py[i], &qx[i], &qy[i]);
    px[i] *= 10;
    py[i] *= 10;
    qx[i] *= 10;
    qy[i] *= 10;
    if (px[i] != 0) {
      x.pb(px[i] - 1);
    }
    x.pb(px[i]);
    if (px[i] != W) {
      x.pb(px[i] + 1);
    }

    if (qx[i] != 0) {
      x.pb(qx[i] - 1);
    }
    x.pb(qx[i]);
    if (qx[i] != W) {
      x.pb(qx[i] + 1);
    }

    if (py[i] != 0) {
      y.pb(py[i] - 1);
    }
    y.pb(py[i]);
    if (py[i] != H) {
      y.pb(py[i] + 1);
    }

    if (qy[i] != 0) {
      y.pb(qy[i] - 1);
    }
    y.pb(qy[i]);
    if (qy[i] != H) {
      y.pb(qy[i] + 1);
    }
  }
  sort(x.begin(), x.end());
  sort(y.begin(), y.end());
  x.erase(unique(x.begin(), x.end()), x.end());
  y.erase(unique(y.begin(), y.end()), y.end());

  WW = (int)x.size();
  HH = (int)y.size();
  for (int i = 0; i < M; i++) {
    int ax = lower_bound(x.begin(), x.end(), px[i]) - x.begin();
    int bx = lower_bound(x.begin(), x.end(), qx[i]) - x.begin();
    int ay = lower_bound(y.begin(), y.end(), py[i]) - y.begin();
    int by = lower_bound(y.begin(), y.end(), qy[i]) - y.begin();
    if (ax >= bx)
      swap(ax, bx);
    if (ay >= by)
      swap(ay, by);
    for (int j = ax; j <= bx; j++) {
      for (int k = ay; k <= by; k++) {
        grid[j][k] = -1;
      }
    }
  }

  int index = 1;
  for (int i = 0; i < WW; i++) {
    for (int j = 0; j < HH; j++) {
      if (grid[i][j] == 0) {
        queue<P> que;
        que.push(P(i, j));
        while (!que.empty()) {
          P p = que.front();
          que.pop();
          if (grid[p.fi][p.sc] == index)
            continue;
          grid[p.fi][p.sc] = index;
          for (int k = 0; k < 4; k++) {
            if (p.fi + dx[k] >= 0 && p.fi + dx[k] < WW && p.sc + dy[k] >= 0 &&
                p.sc + dy[k] < HH) {
              if (grid[p.fi + dx[k]][p.sc + dy[k]] == 0) {
                que.push(P(p.fi + dx[k], p.sc + dy[k]));
              }
            }
          }
        }
        index++;
      }
    }
  }
  /*
  for(int i=0;i<HH;i++){
          for(int j=0;j<WW;j++){
                  printf("%d ",grid[j][i]);
          }
          printf("\\n");
  }*/

  for (int i = 0; i < WW; i++) {
    for (int j = 0; j < HH; j++) {
      if (grid[i][j] == -1) {
        for (int k = 0; k < 4; k++) {
          if (i + dx[k] >= 0 && i - dx[k] >= 0 && i + dx[k] < WW &&
              i - dx[k] < WW) {
            if (j + dy[k] >= 0 && j - dy[k] >= 0 && j + dy[k] < HH &&
                j - dy[k] < HH) {
              // printf("%d %d\\n",i,j);
              int g1 = grid[i + dx[k]][j + dy[k]];
              int g2 = grid[i - dx[k]][j - dy[k]];
              if (g1 != -1 && g2 != -1 && MAP[P(g1, g2)] == 0 && g1 != g2) {
                G[g1 - 1].pb(g2 - 1);
                G[g2 - 1].pb(g1 - 1);
                MAP[P(g1, g2)] = 1;
                MAP[P(g2, g1)] = 1;
                // printf("%d %d\\n",g1,g2);
              }
            }
          }
        }
      }
    }
  }
  scanf("%d", &Q);
  for (int i = 0; i < Q; i++) {
    for (int j = 0; j < WW; j++) {
      for (int k = 0; k < HH; k++) {
        s[j * HH + k] = 100101001;
        // ds[j][k]=2*(WW+HH)+1;
        // f[j][k]=false;
      }
    }
    ll sx, sy, gx, gy;
    scanf("%lld%lld%lld%lld", &sx, &sy, &gx, &gy);
    sx *= 10;
    sy *= 10;
    gx *= 10;
    gy *= 10;

    sx1 = lower_bound(x.begin(), x.end(), sx) - x.begin();
    gx1 = lower_bound(x.begin(), x.end(), gx) - x.begin();
    sy1 = lower_bound(y.begin(), y.end(), sy) - y.begin();
    gy1 = lower_bound(y.begin(), y.end(), gy) - y.begin();

    // printf("%d:%d-%d:%d\\n",sx1,sy1,gx1,gy1);
    int ss = grid[sx1][sy1] - 1;
    int gg = grid[gx1][gy1] - 1;
    priority_queue<P, vector<P>, greater<P>> que;
    que.push(make_pair(0, ss));
    // printf("%d %d\\n",ss,gg);
    // printf("%d %d\\n",sx1,sy1);
    // int n=0;

    while (!que.empty()) {
      P p = que.top();
      que.pop();
      if (p.fi >= s[p.sc])
        continue;
      // printf("%d %d\\n",p.sc.fi,p.sc.sc);
      s[p.sc] = p.fi;
      for (int j = 0; j < (int)G[p.sc].size(); j++) {
        if (p.fi + 1 < s[G[p.sc][j]]) {
          que.push(P(p.fi + 1, G[p.sc][j]));
        }
      }
    }
    // printf("%d\\n",n);
    printf("%d\\n", s[gg]);
  }
  return 0;
}