Problem p00287 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
typedef pair<int, int> P;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};
const int INF = 1001001001;
int W, H, M, Q;
vector<int> xs, ys;
int x1[110], y1[110], x2[110], y2[110];
int qx1[100], qy1[100], qx2[100], qy2[100];
int fld[3000][3000];
vector<vector<int>> G;
int D;
void compress(vector<int> &vec, int Sz) {
  vector<int> latte;
  for (int i = 0; i < vec.size(); i++) {
    for (int d = -1; d <= 1; d++) {
      int v = vec[i] + d;
      if (v < 0 || v > Sz)
        continue;
      latte.push_back(v);
    }
  }
  sort(latte.begin(), latte.end());
  latte.erase(unique(latte.begin(), latte.end()), latte.end());
  vec = latte;
}

void paint(int y, int x, int c) {
  fld[y][x] = c;
  for (int i = 0; i < 4; i++) {
    int ny = y + dy[i], nx = x + dx[i];
    if (ny < 0 || ny >= H || nx < 0 || nx >= W || fld[ny][nx] != 0)
      continue;
    paint(ny, nx, c);
  }
}

void dijkstra(int s, int t) {
  vector<int> dist(D, INF);
  dist[s] = 0;
  queue<int> que;
  que.push(s);

  while (que.size()) {
    int p = que.front();
    que.pop();
    for (int i = 0; i < G[p].size(); i++) {
      int to = G[p][i];
      if (dist[to] <= dist[p] + 1)
        continue;
      dist[to] = dist[p] + 1;
      que.push(to);
    }
  }
  cout << dist[t] << endl;
}

int main() {
  scanf("%d%d%d", &W, &H, &M);
  for (int i = 0; i < M; i++) {
    scanf("%d%d%d%d", &x1[i], &y1[i], &x2[i], &y2[i]);
    if (x1[i] > x2[i])
      swap(x1[i], x2[i]);
    if (y1[i] > y2[i])
      swap(y1[i], y2[i]);
    x1[i] *= 2;
    x2[i] *= 2;
    y1[i] *= 2;
    y2[i] *= 2;
  }
  W *= 2;
  H *= 2;
  x1[M] = 0;
  x2[M] = W;
  y1[M] = 0;
  y2[M] = 0;
  M++;
  x1[M] = 0;
  x2[M] = W;
  y1[M] = H;
  y2[M] = H;
  M++;
  x1[M] = 0;
  x2[M] = 0;
  y1[M] = 0;
  y2[M] = H;
  M++;
  x1[M] = W;
  x2[M] = W;
  y1[M] = 0;
  y2[M] = H;
  M++;

  for (int i = 0; i < M; i++) {
    xs.push_back(x1[i]);
    xs.push_back(x2[i]);
    ys.push_back(y1[i]);
    ys.push_back(y2[i]);
  }

  scanf("%d", &Q);
  for (int i = 0; i < Q; i++) {
    scanf("%d%d%d%d", &qx1[i], &qy1[i], &qx2[i], &qy2[i]);
    qx1[i] *= 2;
    xs.push_back(qx1[i]);
    qx2[i] *= 2;
    xs.push_back(qx2[i]);
    qy1[i] *= 2;
    ys.push_back(qy1[i]);
    qy2[i] *= 2;
    ys.push_back(qy2[i]);
  }

  compress(xs, W);
  compress(ys, H);

  for (int i = 0; i < M; i++) {
    x1[i] = lower_bound(xs.begin(), xs.end(), x1[i]) - xs.begin();
    x2[i] = lower_bound(xs.begin(), xs.end(), x2[i]) - xs.begin();
    y1[i] = lower_bound(ys.begin(), ys.end(), y1[i]) - ys.begin();
    y2[i] = lower_bound(ys.begin(), ys.end(), y2[i]) - ys.begin();
  }
  for (int i = 0; i < Q; i++) {
    qx1[i] = lower_bound(xs.begin(), xs.end(), qx1[i]) - xs.begin();
    qx2[i] = lower_bound(xs.begin(), xs.end(), qx2[i]) - xs.begin();
    qy1[i] = lower_bound(ys.begin(), ys.end(), qy1[i]) - ys.begin();
    qy2[i] = lower_bound(ys.begin(), ys.end(), qy2[i]) - ys.begin();
  }

  W = xs.size();
  H = ys.size();
  for (int i = 0; i < M; i++)
    for (int y = y1[i]; y <= y2[i]; y++)
      for (int x = x1[i]; x <= x2[i]; x++)
        fld[y][x] = -1;

  for (int i = 0; i < H; i++)
    for (int j = 0; j < W; j++)
      if (fld[i][j] == 0)
        paint(i, j, ++D);

  for (int i = 0; i < H; i++)
    for (int j = 0; j < W; j++)
      if (fld[i][j] != -1)
        fld[i][j]--;

  G.resize(D);

  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      for (int d = 0; d < 2; d++) {
        int ny1 = i + dy[d], ny2 = i + dy[d + 2];
        int nx1 = j + dx[d], nx2 = j + dx[d + 2];
        if (ny1 < 0 || ny1 >= H || nx1 < 0 || nx1 >= W)
          continue;
        if (ny2 < 0 || ny2 >= H || nx2 < 0 || nx2 >= W)
          continue;
        if (fld[ny1][nx1] != -1 && fld[ny2][nx2] != -1 &&
            fld[ny1][nx1] != fld[ny2][nx2]) {
          int c1 = fld[ny1][nx1];
          int c2 = fld[ny2][nx2];
          G[c1].push_back(c2);
          G[c2].push_back(c1);
        }
      }
    }
  }

  for (int i = 0; i < D; i++) {
    sort(G[i].begin(), G[i].end());
    G[i].erase(unique(G[i].begin(), G[i].end()), G[i].end());
  }

  for (int i = 0; i < Q; i++) {
    int c1 = fld[qy1[i]][qx1[i]];
    int c2 = fld[qy2[i]][qx2[i]];
    dijkstra(c1, c2);
  }

  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
#define int long long
using namespace std;
typedef pair<int, int> P;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};
const int INF = 1001001001;
int W, H, M, Q;
vector<int> xs, ys;
int x1[110], y1[110], x2[110], y2[110];
int qx1[100], qy1[100], qx2[100], qy2[100];
int fld[3000][3000];
vector<vector<int>> G;
int D;
void compress(vector<int> &vec, int Sz) {
  vector<int> latte;
  for (int i = 0; i < vec.size(); i++) {
    for (int d = -1; d <= 1; d++) {
      int v = vec[i] + d;
      if (v < 0 || v > Sz)
        continue;
      latte.push_back(v);
    }
  }
  sort(latte.begin(), latte.end());
  latte.erase(unique(latte.begin(), latte.end()), latte.end());
  vec = latte;
}

void paint(int y, int x, int c) {
  fld[y][x] = c;
  for (int i = 0; i < 4; i++) {
    int ny = y + dy[i], nx = x + dx[i];
    if (ny < 0 || ny >= H || nx < 0 || nx >= W || fld[ny][nx] != 0)
      continue;
    paint(ny, nx, c);
  }
}

void dijkstra(int s, int t) {
  vector<int> dist(D, INF);
  dist[s] = 0;
  queue<int> que;
  que.push(s);

  while (que.size()) {
    int p = que.front();
    que.pop();
    for (int i = 0; i < G[p].size(); i++) {
      int to = G[p][i];
      if (dist[to] <= dist[p] + 1)
        continue;
      dist[to] = dist[p] + 1;
      que.push(to);
    }
  }
  if (dist[t] == INF)
    for (;;)
      ;
  cout << dist[t] << endl;
}

signed main() {
  scanf("%lld%lld%lld", &W, &H, &M);
  for (int i = 0; i < M; i++) {
    scanf("%lld%lld%lld%lld", &x1[i], &y1[i], &x2[i], &y2[i]);
    x1[i] *= 2;
    x2[i] *= 2;
    y1[i] *= 2;
    y2[i] *= 2;
  }
  H *= 2;
  W *= 2;
  x1[M] = 0;
  x2[M] = W;
  y1[M] = 0;
  y2[M] = 0;
  M++;
  x1[M] = 0;
  x2[M] = W;
  y1[M] = H;
  y2[M] = H;
  M++;
  x1[M] = 0;
  x2[M] = 0;
  y1[M] = 0;
  y2[M] = H;
  M++;
  x1[M] = W;
  x2[M] = W;
  y1[M] = 0;
  y2[M] = H;
  M++;

  for (int i = 0; i < M; i++) {
    xs.push_back(x1[i]);
    xs.push_back(x2[i]);
    ys.push_back(y1[i]);
    ys.push_back(y2[i]);
  }

  scanf("%d", &Q);
  for (int i = 0; i < Q; i++) {
    scanf("%lld%lld%lld%lld", &qx1[i], &qy1[i], &qx2[i], &qy2[i]);
    qx1[i] *= 2;
    xs.push_back(qx1[i]);
    qx2[i] *= 2;
    xs.push_back(qx2[i]);
    qy1[i] *= 2;
    ys.push_back(qy1[i]);
    qy2[i] *= 2;
    xs.push_back(qy2[i]);
  }

  compress(xs, W);
  compress(ys, H);

  for (int i = 0; i < M; i++) {
    x1[i] = lower_bound(xs.begin(), xs.end(), x1[i]) - xs.begin();
    x2[i] = lower_bound(xs.begin(), xs.end(), x2[i]) - xs.begin();
    y1[i] = lower_bound(ys.begin(), ys.end(), y1[i]) - ys.begin();
    y2[i] = lower_bound(ys.begin(), ys.end(), y2[i]) - ys.begin();
  }
  for (int i = 0; i < Q; i++) {
    qx1[i] = lower_bound(xs.begin(), xs.end(), qx1[i]) - xs.begin();
    qx2[i] = lower_bound(xs.begin(), xs.end(), qx2[i]) - xs.begin();
    qy1[i] = lower_bound(ys.begin(), ys.end(), qy1[i]) - ys.begin();
    qy2[i] = lower_bound(ys.begin(), ys.end(), qy2[i]) - ys.begin();
  }

  W = xs.size();
  H = ys.size();
  memset(fld, 0, sizeof(fld));

  for (int i = 0; i < M; i++)
    for (int y = y1[i]; y <= y2[i]; y++)
      for (int x = x1[i]; x <= x2[i]; x++)
        fld[y][x] = -1;

  for (int i = 0; i < H; i++)
    for (int j = 0; j < W; j++)
      if (fld[i][j] == 0)
        paint(i, j, ++D);

  for (int i = 0; i < H; i++)
    for (int j = 0; j < W; j++)
      if (fld[i][j] != -1)
        fld[i][j]--;

  G.resize(D);

  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      for (int d = 0; d < 2; d++) {
        int ny1 = i + dy[d], ny2 = i + dy[d + 2];
        int nx1 = j + dx[d], nx2 = j + dx[d + 2];
        if (ny1 < 0 || ny1 >= H || nx1 < 0 || nx1 >= W)
          continue;
        if (ny2 < 0 || ny2 >= H || nx2 < 0 || nx2 >= W)
          continue;
        if (fld[ny1][nx1] != -1 && fld[ny2][nx2] != -1 &&
            fld[ny1][nx1] != fld[ny2][nx2]) {
          int c1 = fld[ny1][nx1];
          int c2 = fld[ny2][nx2];
          G[c1].push_back(c2);
          G[c2].push_back(c1);
        }
      }
    }
  }

  for (int i = 0; i < D; i++) {
    sort(G[i].begin(), G[i].end());
    G[i].erase(unique(G[i].begin(), G[i].end()), G[i].end());
  }

  for (int i = 0; i < Q; i++) {
    int c1 = fld[qy1[i]][qx1[i]];
    int c2 = fld[qy2[i]][qx2[i]];
    dijkstra(c1, c2);
  }

  return 0;
}