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

#include <iostream>
#include <set>
#include <vector>

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<ll> vi;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int H, W, K;
  cin >> H >> W >> K;
  pii a, b;
  cin >> a.first >> a.second >> b.first >> b.second;
  --a.first;
  --a.second;
  --b.first;
  --b.second;
  vector<string> c(H);
  vector<set<int>> rs(H), cs(W);
  for (int i = 0; i < H; ++i) {
    cin >> c[i];
    for (int j = 0; j < W; ++j) {
      rs[i].insert(j);
      cs[j].insert(i);
    }
  }

  vector<pii> Q, Q2;
  int ans = 0;
  Q.push_back(a);
  rs[a.first].erase(a.second);
  cs[a.second].erase(a.first);
  while (!Q.empty()) {
    Q2.clear();
    ++ans;
    for (const pii &p : Q) {
      int x = p.first, y = p.second;
      auto it = rs[x].lower_bound(y);
      while (it != rs[x].begin()) {
        --it;
        if (*it < y - K || c[x][*it] == '@') {
          ++it;
          break;
        }
        if (x == b.first && *it == b.second)
          return cout << ans << "\\n", 0;
        Q2.emplace_back(x, *it);
        cs[*it].erase(x);
        it = rs[x].erase(it);
      }
      while (it != rs[x].end()) {
        if (*it > y + K || c[x][*it] == '@')
          break;
        if (x == b.first && *it == b.second)
          return cout << ans << "\\n", 0;
        Q2.emplace_back(x, *it);
        cs[*it].erase(x);
        it = rs[x].erase(it);
      }
      it = cs[y].lower_bound(x);
      while (it != cs[y].begin()) {
        --it;
        if (*it < x - K || c[*it][y] == '@') {
          ++it;
          break;
        }
        if (*it == b.first && y == b.second)
          return cout << ans << "\\n", 0;
        Q2.emplace_back(*it, y);
        rs[*it].erase(y);
        it = cs[y].erase(it);
      }
      while (it != cs[y].end()) {
        if (*it > x + K || c[*it][y] == '@')
          break;
        if (*it == b.first && y == b.second)
          return cout << ans << "\\n", 0;
        Q2.emplace_back(*it, y);
        rs[*it].erase(y);
        it = cs[y].erase(it);
      }
    }
    swap(Q, Q2);
  }
  cout << "-1\\n";

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <queue>
#include <vector>

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<ll> vi;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int H, W, K;
  cin >> H >> W >> K;
  pii a, b;
  cin >> a.first >> a.second >> b.first >> b.second;
  --a.first;
  --a.second;
  --b.first;
  --b.second;
  vector<string> c(H);
  for (int i = 0; i < H; ++i)
    cin >> c[i];
  vector<vi> l(H, vi(W)), r(H, vi(W)), u(H, vi(W)), d(H, vi(W));
  for (int i = 0; i < H; ++i)
    for (int j = 0; j < W; ++j) {
      l[i][j] = i - 1;
      r[i][j] = i + 1;
      u[i][j] = j - 1;
      d[i][j] = j + 1;
    }
  const auto upd = [&](int i, int j) -> void {
    int i0 = l[i][j];
    int i1 = r[i][j];
    if (i0 >= 0)
      r[i0][j] = i1;
    if (i1 < H)
      l[i1][j] = i0;
    int j0 = u[i][j];
    int j1 = d[i][j];
    if (j0 >= 0)
      d[i][j0] = j1;
    if (j1 < W)
      u[i][j1] = j0;
  };

  vector<pii> Q, Q2;
  int ans = 0;
  Q.push_back(a);
  upd(a.first, a.second);
  while (!Q.empty()) {
    Q2.clear();
    ++ans;
    for (const pii &p : Q) {
      int x = p.first, y = p.second;
      int x0 = l[x][y];
      while (x0 >= 0 && x0 >= x - K) {
        if (c[x0][y] == '@')
          break;
        if (x0 == b.first && y == b.second)
          return cout << ans << "\\n", 0;
        Q2.emplace_back(x0, y);
        upd(x0, y);
        x0 = l[x0][y];
      }
      x0 = r[x][y];
      while (x0 < H && x0 <= x + K) {
        if (c[x0][y] == '@')
          break;
        if (x0 == b.first && y == b.second)
          return cout << ans << "\\n", 0;
        Q2.emplace_back(x0, y);
        upd(x0, y);
        x0 = r[x0][y];
      }
      int y0 = u[x][y];
      while (y0 >= 0 && y0 >= y - K) {
        if (c[x][y0] == '@')
          break;
        if (x == b.first && y0 == b.second)
          return cout << ans << "\\n", 0;
        Q2.emplace_back(x, y0);
        upd(x, y0);
        y0 = u[x][y0];
      }
      y0 = d[x][y];
      while (y0 < W && y0 <= y + K) {
        if (c[x][y0] == '@')
          break;
        if (x == b.first && y0 == b.second)
          return cout << ans << "\\n", 0;
        Q2.emplace_back(x, y0);
        upd(x, y0);
        y0 = d[x][y0];
      }
    }
    swap(Q, Q2);
  }
  cout << "-1\\n";

  return 0;
}