Problem p02644 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define mp make_pair
#define pb push_back

#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)

struct UF {
  vector<int> fa;
  UF(int n) {
    fa.resize(n);
    rep(i, n) fa[i] = i;
  }
  int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); }
  void merge(int x, int y) { fa[find(x)] = find(y); }
};

int h, w, k;
int sx, sy, ex, ey;
char temp[1 << 20];
vector<string> M;
vector<vector<int>> dis, rhv, chv;
vector<UF> ruf, cuf;
int que[1 << 20], ql, qr;

void ins(int x, int y, int d) {
  dis[x][y] = d;
  ruf[x].merge(y, y + 1);
  cuf[y].merge(x, x + 1);
  que[qr++] = x, que[qr++] = y;
}

int main() {
  scanf("%d %d %d", &h, &w, &k);
  scanf("%d %d %d %d", &sx, &sy, &ex, &ey), --sx, --sy, --ex, --ey;
  rep(i, h) scanf("%s", temp), M.pb(temp);
  rep(i, h) dis.pb(vector<int>(w, -1));
  rep(i, h) rhv.pb(vector<int>(w, 0)), ruf.pb(UF(w + 1));
  rep(i, w) chv.pb(vector<int>(h, 0)), cuf.pb(UF(h + 1));
  rep(i, h) rep(j, w) if (M[i][j] == '@') rhv[i][j]++, chv[j][i]++;
  rep(i, h) rep(j, w) if (j) rhv[i][j] += rhv[i][j - 1];
  rep(i, w) rep(j, h) if (j) chv[i][j] += chv[i][j - 1];
  ins(sx, sy, 0);
  while (ql < qr) {
    int x = que[ql++], y = que[ql++], d = dis[x][y];
    { // row
      int ly = max(0, y - k);
      int uy = min(w - 1, y + k);
      ly = lower_bound(rhv[x].begin() + ly, rhv[x].begin() + y, rhv[x][y]) -
           rhv[x].begin();
      if (M[x][ly] == '@')
        ++ly;
      uy = upper_bound(rhv[x].begin() + y + 1, rhv[x].begin() + uy + 1,
                       rhv[x][y]) -
           rhv[x].begin() - 1;
      for (int ny = ruf[x].find(ly); ny <= uy; ny = ruf[x].find(ny))
        ins(x, ny, d + 1);
    }
    { // col
      int lx = max(0, x - k);
      int ux = min(h - 1, x + k);
      lx = lower_bound(chv[y].begin() + lx, chv[y].begin() + x, chv[y][x]) -
           chv[y].begin();
      if (M[lx][y] == '@')
        ++lx;
      ux = upper_bound(chv[y].begin() + x + 1, chv[y].begin() + ux + 1,
                       chv[y][x]) -
           chv[y].begin() - 1;
      for (int nx = cuf[y].find(lx); nx <= ux; nx = cuf[y].find(nx))
        ins(nx, y, d + 1);
    }
  }
  printf("%d\n", dis[ex][ey]);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define mp make_pair
#define pb push_back

#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)

struct UF {
  vector<int> fa;
  UF(int n) {
    fa.resize(n);
    rep(i, n) fa[i] = i;
  }
  int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); }
  void merge(int x, int y) { fa[find(x)] = find(y); }
};

int h, w, k;
int sx, sy, ex, ey;
char temp[1 << 20];
vector<string> M;
vector<vector<int>> dis, rhv, chv;
vector<UF> ruf, cuf;
int que[1 << 21], ql, qr;

void ins(int x, int y, int d) {
  dis[x][y] = d;
  ruf[x].merge(y, y + 1);
  cuf[y].merge(x, x + 1);
  que[qr++] = x, que[qr++] = y;
}

int main() {
  scanf("%d %d %d", &h, &w, &k);
  scanf("%d %d %d %d", &sx, &sy, &ex, &ey), --sx, --sy, --ex, --ey;
  rep(i, h) scanf("%s", temp), M.pb(temp);
  rep(i, h) dis.pb(vector<int>(w, -1));
  rep(i, h) rhv.pb(vector<int>(w, 0)), ruf.pb(UF(w + 1));
  rep(i, w) chv.pb(vector<int>(h, 0)), cuf.pb(UF(h + 1));
  rep(i, h) rep(j, w) if (M[i][j] == '@') rhv[i][j]++, chv[j][i]++;
  rep(i, h) rep(j, w) if (j) rhv[i][j] += rhv[i][j - 1];
  rep(i, w) rep(j, h) if (j) chv[i][j] += chv[i][j - 1];
  ins(sx, sy, 0);
  while (ql < qr) {
    int x = que[ql++], y = que[ql++], d = dis[x][y];
    { // row
      int ly = max(0, y - k);
      int uy = min(w - 1, y + k);
      ly = lower_bound(rhv[x].begin() + ly, rhv[x].begin() + y, rhv[x][y]) -
           rhv[x].begin();
      if (M[x][ly] == '@')
        ++ly;
      uy = upper_bound(rhv[x].begin() + y + 1, rhv[x].begin() + uy + 1,
                       rhv[x][y]) -
           rhv[x].begin() - 1;
      for (int ny = ruf[x].find(ly); ny <= uy; ny = ruf[x].find(ny))
        ins(x, ny, d + 1);
    }
    { // col
      int lx = max(0, x - k);
      int ux = min(h - 1, x + k);
      lx = lower_bound(chv[y].begin() + lx, chv[y].begin() + x, chv[y][x]) -
           chv[y].begin();
      if (M[lx][y] == '@')
        ++lx;
      ux = upper_bound(chv[y].begin() + x + 1, chv[y].begin() + ux + 1,
                       chv[y][x]) -
           chv[y].begin() - 1;
      for (int nx = cuf[y].find(lx); nx <= ux; nx = cuf[y].find(nx))
        ins(nx, y, d + 1);
    }
  }
  printf("%d\n", dis[ex][ey]);
  return 0;
}