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

#include <bits/stdc++.h>
using namespace std;
char a[1000005];
int dp[1000005];
int h, w, k;
set<int> row[1000005], col[1000005];
void remove(int x, int y) {
  row[x].erase(row[x].find(y));
  col[y].erase(col[y].find(x));
}
void explore(int X, int Y) {
  queue<pair<int, int>> q;
  q.push({X, Y});
  while (!q.empty()) {
    int x = q.front().first, y = q.front().second;
    // cout<<x<<" "<<y<<" "<<dp[x*w+y]<<endl;
    q.pop();
    auto it = col[y].lower_bound(x);
    while (it != col[y].end() && *it <= x + k) {
      int cur = *it;
      if (a[cur * w + y] == '@')
        break;
      dp[cur * w + y] = dp[x * w + y] + 1;
      remove(cur, y);
      q.push({cur, y});
      it = col[y].lower_bound(x);
    }

    it = col[y].lower_bound(x);
    if (!col[y].empty() && it != col[y].begin()) {
      it--;
      while (*it >= x - k) {
        int cur = *it;
        if (a[cur * w + y] == '@')
          break;
        dp[cur * w + y] = dp[x * w + y] + 1;
        bool b = true;
        if (it == col[y].begin())
          b = false;
        remove(cur, y);
        q.push({cur, y});
        it = col[y].lower_bound(x);
        if (it == col[y].begin())
          break;
        else
          it--;
      }
    }
    it = row[x].lower_bound(y);
    while (it != row[x].end() && *it <= y + k) {
      int cur = *it;
      // cout<<cur<<" "<<y+k<<endl;
      if (a[x * w + cur] == '@')
        break;
      dp[x * w + cur] = dp[x * w + y] + 1;
      remove(x, cur);
      q.push({x, cur});
      it = row[x].lower_bound(y);
    }

    it = row[x].lower_bound(y);
    if (!row[x].empty() && it != row[x].begin()) {
      it--;
      while (*it >= y - k) {
        int cur = *it;
        if (a[x * w + cur] == '@')
          break;
        dp[x * w + cur] = dp[x * w + y] + 1;
        remove(x, cur);
        q.push({x, cur});
        it = row[x].lower_bound(y);
        if (it == row[x].begin())
          break;
        else
          it--;
      }
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int x1, y1, x2, y2;
  cin >> h >> w >> k >> x1 >> y1 >> x2 >> y2;
  x1--;
  y1--;
  x2--;
  y2--;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> a[i * w + j];
      row[i].insert(j);
      col[j].insert(i);
    }
  }
  for (int i = 0; i < 1000005; i++)
    dp[i] = -1;
  dp[x1 * w + y1] = 0;
  remove(x1, y1);
  explore(x1, y1);
  cout << dp[x2 * w + y2] << endl;
}


Error: 127
/tmp/79943608-9fbf-43a3-ada1-8b5530f04f14.out: error while loading shared libraries: libc.so.6: failed to map segment from shared object


Fixed submission:

#include <bits/stdc++.h>
using namespace std;
char a[1000005];
int dp[1000005];
int h, w, k;
set<int> row[1000005], col[1000005];
void remove(int x, int y) {
  row[x].erase(row[x].find(y));
  col[y].erase(col[y].find(x));
}
void explore(int X, int Y) {
  queue<pair<int, int>> q;
  q.push({X, Y});
  while (!q.empty()) {
    int x = q.front().first, y = q.front().second;
    // cout<<x<<" "<<y<<" "<<dp[x*w+y]<<endl;
    q.pop();
    auto it = col[y].lower_bound(x);
    while (it != col[y].end() && *it <= x + k) {
      int cur = *it;
      if (a[cur * w + y] == '@')
        break;
      dp[cur * w + y] = dp[x * w + y] + 1;
      remove(cur, y);
      q.push({cur, y});
      it++;
    }

    it = col[y].lower_bound(x);
    if (!col[y].empty() && it != col[y].begin()) {
      it--;
      while (*it >= x - k) {
        int cur = *it;
        if (a[cur * w + y] == '@')
          break;
        dp[cur * w + y] = dp[x * w + y] + 1;
        bool b = true;
        if (it == col[y].begin())
          b = false;
        remove(cur, y);
        q.push({cur, y});
        if (b)
          it--;
        else
          break;
      }
    }
    it = row[x].lower_bound(y);
    while (it != row[x].end() && *it <= y + k) {
      int cur = *it;
      if (a[x * w + cur] == '@')
        break;
      dp[x * w + cur] = dp[x * w + y] + 1;
      remove(x, cur);
      q.push({x, cur});
      it++;
    }

    it = row[x].lower_bound(y);
    if (!row[x].empty() && it != row[x].begin()) {
      it--;
      while (*it >= y - k) {
        int cur = *it;
        if (a[x * w + cur] == '@')
          break;
        dp[x * w + cur] = dp[x * w + y] + 1;
        bool b = true;
        if (it == row[x].begin())
          b = false;
        remove(x, cur);
        q.push({x, cur});
        if (b)
          it--;
        else
          break;
      }
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int x1, y1, x2, y2;
  cin >> h >> w >> k >> x1 >> y1 >> x2 >> y2;
  x1--;
  y1--;
  x2--;
  y2--;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> a[i * w + j];
      row[i].insert(j);
      col[j].insert(i);
    }
  }
  for (int i = 0; i < 1000005; i++)
    dp[i] = -1;
  dp[x1 * w + y1] = 0;
  remove(x1, y1);
  explore(x1, y1);
  cout << dp[x2 * w + y2] << endl;
}