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

#include <bits/stdc++.h>
#include <chrono>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <random>

using namespace std;
using namespace __gnu_pbds;

#define endl '\\n'

typedef long long ll;
typedef pair<int, int> pii;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

template <typename T> static T randint(T lo, T hi) {
  return uniform_int_distribution<T>(lo, hi)(rng);
}

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

  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);

  int n, m, k;
  cin >> n >> m >> k;

  int xo, yo, xi, yi;
  cin >> xo >> yo >> xi >> yi;

  vector<vector<char>> a(n + 1, vector<char>(m + 1));
  vector<set<int>> row(n + 1), col(m + 1);
  vector<set<int>> r_block(n + 1), c_block(m + 1);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      cin >> a[i][j];
      if (a[i][j] == '@') {
        r_block[i].insert(j);
        c_block[j].insert(i);
        continue;
      }

      row[i].insert(j);
      col[j].insert(i);
    }

  priority_queue<pair<int, pii>, vector<pair<int, pii>>,
                 greater<pair<int, pii>>>
      pq;
  pq.push({0, {xo, yo}});

  row[xo].erase(yo);
  col[yo].erase(xo);

  while (!pq.empty()) {
    auto u = pq.top();
    pq.pop();
    int x = u.second.first, y = u.second.second, cost = u.first;

    if (x == xi && y == yi) {
      cout << cost << endl;
      return 0;
    }

    auto lo = r_block[x].lower_bound(y);
    auto hi = r_block[x].upper_bound(y);

    int b, e;

    if (lo == r_block[x].begin())
      b = 0;
    else {
      lo--;
      b = *lo;
    }

    if (hi == r_block[x].end())
      e = m + 1;
    else
      e = *hi;

    lo = row[x].lower_bound(max(b + 1, y - k));
    hi = row[x].upper_bound(min(e - 1, y + k));

    while (lo != hi) {
      pq.push({cost + 1, {x, *lo}});
      lo++;
    }

    row[x].erase(row[x].lower_bound(max(b + 1, y - k)), hi);

    lo = c_block[y].lower_bound(x);
    hi = c_block[y].upper_bound(x);

    if (lo == c_block[y].begin())
      b = 0;
    else {
      lo--;
      b = *lo;
    }

    if (hi == c_block[y].end())
      e = n + 1;
    else
      e = *hi;

    lo = col[y].lower_bound(max(b + 1, x - k));
    hi = col[y].upper_bound(min(e - 1, x + k));

    while (lo != hi) {
      pq.push({cost + 1, {*lo, y}});
      lo++;
    }

    col[y].erase(col[y].lower_bound(max(b + 1, x - k)), hi);
  }

  cout << -1 << endl;

  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#include <chrono>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <random>

using namespace std;
using namespace __gnu_pbds;

#define endl '\\n'

typedef long long ll;
typedef pair<int, int> pii;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

template <typename T> static T randint(T lo, T hi) {
  return uniform_int_distribution<T>(lo, hi)(rng);
}

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

  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);

  int n, m, k;
  cin >> n >> m >> k;

  int xo, yo, xi, yi;

  vector<vector<char>> a(n + 1, vector<char>(m + 1));
  vector<set<int>> row(n + 1), col(m + 1);
  vector<set<int>> r_block(n + 1), c_block(m + 1);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      cin >> a[i][j];
      if (a[i][j] == '@') {
        r_block[i].insert(j);
        c_block[j].insert(i);
        continue;
      }

      row[i].insert(j);
      col[j].insert(i);
    }

  cin >> xo >> yo >> xi >> yi;

  priority_queue<pair<int, pii>, vector<pair<int, pii>>,
                 greater<pair<int, pii>>>
      pq;
  pq.push({0, {xo, yo}});

  row[xo].erase(yo);
  col[yo].erase(xo);

  while (!pq.empty()) {
    auto u = pq.top();
    pq.pop();
    int x = u.second.first, y = u.second.second, cost = u.first;

    if (x == xi && y == yi) {
      cout << cost << endl;
      return 0;
    }

    auto lo = r_block[x].lower_bound(y);
    auto hi = r_block[x].upper_bound(y);

    int b, e;

    if (lo == r_block[x].begin())
      b = 0;
    else {
      lo--;
      b = *lo;
    }

    if (hi == r_block[x].end())
      e = m + 1;
    else
      e = *hi;

    lo = row[x].lower_bound(max(b + 1, y - k));
    hi = row[x].upper_bound(min(e - 1, y + k));

    while (lo != hi) {
      pq.push({cost + 1, {x, *lo}});
      lo++;
    }

    row[x].erase(row[x].lower_bound(max(b + 1, y - k)), hi);

    lo = c_block[y].lower_bound(x);
    hi = c_block[y].upper_bound(x);

    if (lo == c_block[y].begin())
      b = 0;
    else {
      lo--;
      b = *lo;
    }

    if (hi == c_block[y].end())
      e = n + 1;
    else
      e = *hi;

    lo = col[y].lower_bound(max(b + 1, x - k));
    hi = col[y].upper_bound(min(e - 1, x + k));

    while (lo != hi) {
      pq.push({cost + 1, {*lo, y}});
      lo++;
    }

    col[y].erase(col[y].lower_bound(max(b + 1, x - k)), hi);
  }

  cout << -1 << endl;

  return 0;
}