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

// In the name of GOD
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

const ll maxn = 1e6 + 100;
const ll mod = 1e9 + 7;
const ll inf = 1e18;

#define fast_io                                                                \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define file_io                                                                \\
  freopen("input.txt", "r+", stdin);                                           \\
  freopen("output.txt", "w+", stdout);
#define pb push_back
#define Mp make_pair
#define pll pair<ll, ll>
#define F first
#define S second

ll n, m, k;
queue<pll> q;
string s[maxn];
vector<ll> d[maxn];
set<ll> s1[maxn], s2[maxn], d1[maxn], d2[maxn];

void add(ll x, ll y, ll dis) {
  dis++;
  if (d[x][y] <= dis)
    return;
  d[x][y] = dis;
  s1[x].erase(y);
  s2[x].erase(-y);
  d1[y].erase(x);
  d2[y].erase(-x);
  q.push(Mp(x, y));
  return;
}

int main() {
  fast_io;

  cin >> n >> m >> k;
  ll x, y, x1, y1, e;
  cin >> x >> y >> x1 >> y1;
  x--, y--, x1--, y1--;
  for (ll i = 0; i < n; i++) {
    cin >> s[i];
    d[i].resize(m);
    fill(d[i].begin(), d[i].end(), inf);
    for (ll j = 0; j < m; j++) {
      s1[i].insert(j);
      s2[i].insert(-j);
    }
  }
  for (ll j = 0; j < m; j++) {
    for (ll i = 0; i < n; i++) {
      d1[j].insert(i);
      d2[j].insert(-i);
    }
  }
  add(x, y, -1);
  set<ll>::iterator it;
  while (!q.empty()) {
    x = q.front().F;
    y = q.front().S;
    q.pop();
    it = s1[x].lower_bound(y);
    while (it != s1[x].end()) {
      e = *it;
      if (e - y > k || s[x][e] == '@')
        break;
      add(x, e, d[x][y]);
      it = s1[x].lower_bound(e);
    }
    it = s2[x].lower_bound(-y);
    while (it != s2[x].end()) {
      e = -*it;
      if (y - e > k || s[x][e] == '@')
        break;
      add(x, e, d[x][y]);
      it = s2[x].lower_bound(-e);
    }
    it = d1[y].lower_bound(x);
    while (it != d1[y].end()) {
      e = *it;
      if (e - x > k || s[e][y] == '@')
        break;
      add(e, y, d[x][y]);
      it = d1[y].lower_bound(e);
    }
    it = d2[y].lower_bound(-x);
    while (it != d2[y].end()) {
      e = -*it;
      if (x - e > k || s[e][y] == '@')
        break;
      add(e, y, d[x][y]);
      it = d2[y].lower_bound(-e);
    }
  }
  if (d[x1][y1] == inf)
    d[x1][y1] = -1;
  cout << d[x1][y1];

  return 0;
}


Error: -11

Fixed submission:

// In the name of GOD
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

const ll maxn = 1e6 + 100;
const ll mod = 1e9 + 7;
const ll inf = 1e18;

#define fast_io                                                                \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define file_io                                                                \\
  freopen("input.txt", "r+", stdin);                                           \\
  freopen("output.txt", "w+", stdout);
#define pb push_back
#define Mp make_pair
#define pll pair<ll, ll>
#define F first
#define S second

ll n, m, k;
queue<pll> q;
string s[maxn];
vector<ll> d[maxn];
set<ll> s1[maxn], s2[maxn], d1[maxn], d2[maxn];

void add(ll x, ll y, ll dis) {
  dis++;
  if (d[x][y] <= dis)
    return;
  d[x][y] = dis;
  s1[x].erase(y);
  s2[x].erase(-y);
  d1[y].erase(x);
  d2[y].erase(-x);
  q.push(Mp(x, y));
  return;
}

int main() {
  fast_io;

  cin >> n >> m >> k;
  ll x, y, x1, y1, e;
  cin >> x >> y >> x1 >> y1;
  x--, y--, x1--, y1--;
  for (ll i = 0; i < n; i++) {
    cin >> s[i];
    d[i].resize(m);
    fill(d[i].begin(), d[i].end(), inf);
    for (ll j = 0; j < m; j++) {
      s1[i].insert(j);
      s2[i].insert(-j);
    }
  }
  for (ll j = 0; j < m; j++) {
    for (ll i = 0; i < n; i++) {
      d1[j].insert(i);
      d2[j].insert(-i);
    }
  }
  add(x, y, -1);
  set<ll>::iterator it;
  while (!q.empty()) {
    x = q.front().F;
    y = q.front().S;
    q.pop();
    it = s1[x].lower_bound(y);
    while (it != s1[x].end()) {
      e = *it;
      if (e - y > k || s[x][e] == '@')
        break;
      add(x, e, d[x][y]);
      it = s1[x].lower_bound(e);
    }
    it = s2[x].lower_bound(-y);
    while (it != s2[x].end()) {
      e = *it;
      if (e + y > k || s[x][-e] == '@')
        break;
      add(x, e, d[x][y]);
      it = s2[x].lower_bound(e);
    }
    it = d1[y].lower_bound(x);
    while (it != d1[y].end()) {
      e = *it;
      if (e - x > k || s[e][y] == '@')
        break;
      add(e, y, d[x][y]);
      it = d1[y].lower_bound(e);
    }
    it = d2[y].lower_bound(-x);
    while (it != d2[y].end()) {
      e = *it;
      if (e + x > k)
        break;
      add(e, y, d[x][y] || s[-e][y] == '@');
      it = d2[y].lower_bound(e);
    }
  }
  if (d[x1][y1] == inf)
    d[x1][y1] = -1;
  cout << d[x1][y1];

  return 0;
}