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

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

set<int> rows[1000005], cols[1000005];
set<int> rowbs[1000005], colbs[1000005];

vector<vector<int>> dirs = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};

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

  int R, C, K, x, y, xx, yy;
  cin >> R >> C >> K;
  cin >> x >> y >> xx >> yy;
  --x;
  --y;
  --xx;
  --yy;
  vector<string> G(R);
  vector<vector<int>> strokes(R, vector<int>(C, 0));
  for (auto &s : G)
    cin >> s;
  for (int r = 0; r < R; ++r) {
    for (int c = 0; c < C; ++c) {
      if (G[r][c] == '.')
        rows[r].insert(c);
      else
        rowbs[r].insert(c);
    }
  }
  for (int c = 0; c < C; ++c) {
    for (int r = 0; r < R; ++r) {
      if (G[r][c] == '.')
        cols[c].insert(r);
      else
        colbs[c].insert(r);
    }
  }
  queue<pair<int, int>> Q;
  Q.emplace(x, y);
  rows[x].erase(y);
  cols[y].erase(x);
  vector<int> todo;
  while (Q.size()) {
    int atx, aty;
    tie(atx, aty) = Q.front();
    if (atx == xx && aty == yy) {
      cout << strokes[atx][aty] << endl;
      return 0;
    }
    Q.pop();

    // cout << "AT " << atx << " " << aty << " " << strokes[atx][aty] << endl;

    todo.clear();
    // down
    auto it = cols[aty].lower_bound(atx);
    auto itb = colbs[aty].lower_bound(atx);
    while (true) {
      if (it == cols[aty].end())
        break;
      if (abs(atx - *it) > K)
        break;
      if (itb != colbs[aty].end() && *it > *itb)
        break;
      strokes[*it][aty] = strokes[atx][aty] + 1;
      // cout << "c1 " << *it << " " << aty << endl;
      Q.emplace(*it, aty);
      todo.push_back(*it);
      it = next(it);
    }
    // up
    if (cols[aty].size()) {
      auto lb = cols[aty].lower_bound(atx);
      if (lb != cols[aty].begin()) {
        it = prev(lb);
        if (itb == colbs[aty].begin() || colbs[aty].empty())
          itb = colbs[aty].end();
        else
          itb = prev(itb);
        while (true) {
          if (abs(atx - *it) > K)
            break;
          if (itb != colbs[aty].end() && *it < *itb)
            break;
          strokes[*it][aty] = strokes[atx][aty] + 1;
          // cout << "c2 " << *it << " " << aty << endl;
          Q.emplace(*it, aty);
          todo.push_back(*it);
          if (it == cols[aty].begin())
            break;
          it = prev(it);
        }
      }
    }
    for (int t : todo) {
      cols[aty].erase(t);
      rows[t].erase(aty);
    }

    todo.clear();
    // right
    it = rows[atx].lower_bound(aty);
    itb = rowbs[atx].lower_bound(aty);
    while (true) {
      if (it == rows[atx].end())
        break;
      if (abs(aty - *it) > K)
        break;
      if (itb != rowbs[atx].end() && *it > *itb)
        break;
      strokes[atx][*it] = strokes[atx][aty] + 1;
      // cout << "c2 " << atx << " " << *it << endl;
      Q.emplace(atx, *it);
      todo.push_back(*it);
      it = next(it);
    }
    // left
    if (rows[atx].size()) {
      auto lb = rows[atx].lower_bound(aty);
      if (lb != rows[atx].begin()) {
        it = prev(rows[atx].lower_bound(aty));
        if (itb == rowbs[atx].begin() || rowbs[atx].empty())
          itb = rowbs[atx].end();
        else
          itb = prev(itb);
        while (true) {
          if (abs(aty - *it) > K)
            break;
          if (itb != rowbs[atx].end() && *it < *itb)
            break;
          strokes[atx][*it] = strokes[atx][aty] + 1;
          // cout << "c4 " << atx << " " << *it << endl;
          Q.emplace(atx, *it);
          todo.push_back(*it);
          if (it == rows[atx].begin())
            break;
          it = prev(it);
        }
      }
    }
    for (int t : todo) {
      rows[atx].erase(t);
      cols[t].erase(atx);
    }
  }
  cout << -1 << endl;
}

Error: -11

Fixed submission:

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

set<int> rows[1000005], cols[1000005];
set<int> rowbs[1000005], colbs[1000005];

vector<vector<int>> dirs = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};

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

  int R, C, K, x, y, xx, yy;
  cin >> R >> C >> K;
  cin >> x >> y >> xx >> yy;
  --x;
  --y;
  --xx;
  --yy;
  vector<string> G(R);
  vector<vector<int>> strokes(R, vector<int>(C, 0));
  for (auto &s : G)
    cin >> s;
  for (int r = 0; r < R; ++r) {
    for (int c = 0; c < C; ++c) {
      if (G[r][c] == '.')
        rows[r].insert(c);
      else
        rowbs[r].insert(c);
    }
  }
  for (int c = 0; c < C; ++c) {
    for (int r = 0; r < R; ++r) {
      if (G[r][c] == '.')
        cols[c].insert(r);
      else
        colbs[c].insert(r);
    }
  }
  queue<pair<int, int>> Q;
  Q.emplace(x, y);
  rows[x].erase(y);
  cols[y].erase(x);
  vector<int> todo;
  while (Q.size()) {
    int atx, aty;
    tie(atx, aty) = Q.front();
    if (atx == xx && aty == yy) {
      cout << strokes[atx][aty] << endl;
      return 0;
    }
    Q.pop();

    // cout << "AT " << atx << " " << aty << " " << strokes[atx][aty] << endl;

    todo.clear();
    // down
    auto it = cols[aty].lower_bound(atx);
    auto itb = colbs[aty].lower_bound(atx);
    while (true) {
      if (it == cols[aty].end())
        break;
      if (abs(atx - *it) > K)
        break;
      if (itb != colbs[aty].end() && *it > *itb)
        break;
      strokes[*it][aty] = strokes[atx][aty] + 1;
      // cout << "c1 " << *it << " " << aty << endl;
      Q.emplace(*it, aty);
      todo.push_back(*it);
      it = next(it);
    }
    // up
    if (cols[aty].size()) {
      auto lb = cols[aty].lower_bound(atx);
      if (lb != cols[aty].begin()) {
        it = prev(lb);
        if (itb == colbs[aty].begin() || colbs[aty].empty())
          itb = colbs[aty].end();
        else
          itb = prev(itb);
        while (true) {
          if (abs(atx - *it) > K)
            break;
          if (itb != colbs[aty].end() && *it < *itb)
            break;
          strokes[*it][aty] = strokes[atx][aty] + 1;
          // cout << "c2 " << *it << " " << aty << endl;
          Q.emplace(*it, aty);
          todo.push_back(*it);
          if (it == cols[aty].begin())
            break;
          it = prev(it);
        }
      }
    }
    for (int t : todo) {
      cols[aty].erase(t);
      rows[t].erase(aty);
    }

    todo.clear();
    // right
    it = rows[atx].lower_bound(aty);
    itb = rowbs[atx].lower_bound(aty);
    while (true) {
      if (it == rows[atx].end())
        break;
      if (abs(aty - *it) > K)
        break;
      if (itb != rowbs[atx].end() && *it > *itb)
        break;
      strokes[atx][*it] = strokes[atx][aty] + 1;
      // cout << "c2 " << atx << " " << *it << endl;
      Q.emplace(atx, *it);
      todo.push_back(*it);
      it = next(it);
    }
    // left
    if (rows[atx].size()) {
      auto lb = rows[atx].lower_bound(aty);
      if (lb != rows[atx].begin()) {
        it = prev(rows[atx].lower_bound(aty));
        if (itb != rowbs[atx].begin() || rowbs[atx].empty())
          itb = prev(itb);
        else
          itb = rowbs[atx].end();
        while (true) {
          if (abs(aty - *it) > K)
            break;
          if (itb != rowbs[atx].end() && *it < *itb)
            break;
          strokes[atx][*it] = strokes[atx][aty] + 1;
          // cout << "c4 " << atx << " " << *it << endl;
          Q.emplace(atx, *it);
          todo.push_back(*it);
          if (it == rows[atx].begin())
            break;
          it = prev(it);
        }
      }
    }
    for (int t : todo) {
      rows[atx].erase(t);
      cols[t].erase(atx);
    }
  }
  cout << -1 << endl;
}