Problem p02644 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pil pair<int, ll>
#define pli pair<ll, int>
#define ppi pair<pii, int>
#define pip pair<int, pii>
#define ppp pair<pii, pii>
#define pdd pair<double, double>
#define f first
#define s second
#define MOD 1000000007
#define mkp make_pair
#define FOR(i, l, r) for (int i = l; i <= r; i++)
#define LOR(i, l, r) for (ll i = l; i <= r; i++)
#define FORD(i, r, l) for (int i = r; i >= l; i--)
#define LORD(i, r, l) for (ll i = r; i >= l; i--)
#define CL(x) memset(x, 0, sizeof(x))
#define ALL(x) x.begin(), x.end()
#define SZ(x) (int)(x.size())
#define UI(x) (int)(x - 'A')
#define LI(x) (int)(x - 'a')
#define DI(x) (int)(x - '0')
#define LL_MAX (1LL << 60)
#define DB 0
#define DBG(x)                                                                 \
  if (DB)                                                                      \
  cout << #x << " : " << x << '\n'
#define PRALL(v)                                                               \
  if (DB)                                                                      \
    for (auto it : v)                                                          \
      cout << v << ' ';                                                        \
  NL;
#define NL cout << '\n';
#define FND(S, x) (S.find(x) != S.end())
#define bit(x, y) (!!(x & (1 << y)))
typedef long long ll;
typedef long double ld;

// Grid, BFS, Set
#define MXN 200005
int R, C, K;
int X1, Y1, X2, Y2;
set<int> Rpos[MXN], Cpos[MXN]; // unvisited pos
set<int> RB[MXN], CB[MXN];     // blocks

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

  cin >> R >> C >> K;
  cin >> X1 >> Y1 >> X2 >> Y2;

  string str;
  vector<vector<int>> dis(R + 1, vector<int>(C + 1, 1e9));

  FOR(i, 1, R) {
    RB[i].insert(-1);
    RB[i].insert(C + 1);
    Rpos[i].insert(-2);
    Rpos[i].insert(C + 2);
  }

  FOR(i, 1, C) {
    CB[i].insert(-1);
    CB[i].insert(R + 1);
    Cpos[i].insert(-2);
    Cpos[i].insert(R + 2);
  }

  FOR(i, 1, R) {
    cin >> str;
    FOR(j, 1, C) {
      if (str[j - 1] == '@') {
        RB[i].insert(j);
        CB[j].insert(i);
      } else {
        Rpos[i].insert(j);
        Cpos[j].insert(i);
      }
    }
  }

  // BFS
  dis[X1][Y1] = 0;
  Rpos[X1].erase(Y1);
  Cpos[Y1].erase(X1);

  queue<pii> Q;
  Q.push(mkp(X1, Y1));

  while (!Q.empty()) {
    pii pnow = Q.front();
    Q.pop();
    int x = pnow.f, y = pnow.s;
    int d = dis[x][y];

    // cout<<x<<' '<<y<<' '<<d<<'\n';

    int lb, rb, ub, db;

    // going LR
    auto ptr = RB[x].lower_bound(y);
    rb = *ptr;
    ptr--;
    lb = *ptr;

    vector<int> dlist;
    // going L
    ptr = Rpos[x].lower_bound(y);
    FOR(i, 1, K) {
      ptr--;
      int y2 = *ptr;
      if (y - y2 > K || y2 < lb)
        break;
      dis[x][y2] = d + 1;
      Q.push(mkp(x, y2));
      dlist.pb(y2);
    }
    // going R
    ptr = Rpos[x].lower_bound(y);
    FOR(i, 1, K) {
      int y2 = *ptr;
      if (y2 - y > K || y2 > rb)
        break;
      dis[x][y2] = d + 1;
      Q.push(mkp(x, y2));
      dlist.pb(y2);
      ptr++;
    }
    // cleaning
    for (int yi : dlist) {
      // cout<<"cleaning "<<x<<' '<<yi<<'\n';
      Cpos[yi].erase(x);
      Rpos[x].erase(yi);
    }
    dlist.clear();

    // going UD
    ptr = CB[y].lower_bound(x);
    db = *ptr;
    ptr--;
    ub = *ptr;

    // going U
    ptr = Cpos[y].lower_bound(x);
    FOR(i, 1, K) {
      ptr--;
      int x2 = *ptr;
      if (x - x2 > K || x2 < ub)
        break;
      dis[x2][y] = d + 1;
      Q.push(mkp(x2, y));
      dlist.pb(x2);
    }
    // going R
    ptr = Cpos[y].lower_bound(x);
    FOR(i, 1, K) {
      int x2 = *ptr;
      if (x2 - x > K || x2 > db)
        break;
      dis[x2][y] = d + 1;
      Q.push(mkp(x2, y));
      ;
      dlist.pb(x2);
      ptr++;
    }
    // cleaning
    for (int xi : dlist) {
      // cout<<"cleaning "<<xi<<' '<<y<<'\n';
      Rpos[xi].erase(y);
      Cpos[y].erase(xi);
    }
  }

  if (dis[X2][Y2] == 1e9)
    cout << -1 << '\n';
  else
    cout << dis[X2][Y2] << '\n';

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pil pair<int, ll>
#define pli pair<ll, int>
#define ppi pair<pii, int>
#define pip pair<int, pii>
#define ppp pair<pii, pii>
#define pdd pair<double, double>
#define f first
#define s second
#define MOD 1000000007
#define mkp make_pair
#define FOR(i, l, r) for (int i = l; i <= r; i++)
#define LOR(i, l, r) for (ll i = l; i <= r; i++)
#define FORD(i, r, l) for (int i = r; i >= l; i--)
#define LORD(i, r, l) for (ll i = r; i >= l; i--)
#define CL(x) memset(x, 0, sizeof(x))
#define ALL(x) x.begin(), x.end()
#define SZ(x) (int)(x.size())
#define UI(x) (int)(x - 'A')
#define LI(x) (int)(x - 'a')
#define DI(x) (int)(x - '0')
#define LL_MAX (1LL << 60)
#define DB 0
#define DBG(x)                                                                 \
  if (DB)                                                                      \
  cout << #x << " : " << x << '\n'
#define PRALL(v)                                                               \
  if (DB)                                                                      \
    for (auto it : v)                                                          \
      cout << v << ' ';                                                        \
  NL;
#define NL cout << '\n';
#define FND(S, x) (S.find(x) != S.end())
#define bit(x, y) (!!(x & (1 << y)))
typedef long long ll;
typedef long double ld;

// Grid, BFS, Set
#define MXN 1000005
int R, C, K;
int X1, Y1, X2, Y2;
set<int> Rpos[MXN], Cpos[MXN]; // unvisited pos
set<int> RB[MXN], CB[MXN];     // blocks

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

  cin >> R >> C >> K;
  cin >> X1 >> Y1 >> X2 >> Y2;

  string str;
  vector<vector<int>> dis(R + 1, vector<int>(C + 1, 1e9));

  FOR(i, 1, R) {
    RB[i].insert(-1);
    RB[i].insert(C + 1);
    Rpos[i].insert(-2);
    Rpos[i].insert(C + 2);
  }

  FOR(i, 1, C) {
    CB[i].insert(-1);
    CB[i].insert(R + 1);
    Cpos[i].insert(-2);
    Cpos[i].insert(R + 2);
  }

  FOR(i, 1, R) {
    cin >> str;
    FOR(j, 1, C) {
      if (str[j - 1] == '@') {
        RB[i].insert(j);
        CB[j].insert(i);
      } else {
        Rpos[i].insert(j);
        Cpos[j].insert(i);
      }
    }
  }

  // BFS
  dis[X1][Y1] = 0;
  Rpos[X1].erase(Y1);
  Cpos[Y1].erase(X1);

  queue<pii> Q;
  Q.push(mkp(X1, Y1));

  while (!Q.empty()) {
    pii pnow = Q.front();
    Q.pop();
    int x = pnow.f, y = pnow.s;
    int d = dis[x][y];

    // cout<<x<<' '<<y<<' '<<d<<'\n';

    int lb, rb, ub, db;

    // going LR
    auto ptr = RB[x].lower_bound(y);
    rb = *ptr;
    ptr--;
    lb = *ptr;

    vector<int> dlist;
    // going L
    ptr = Rpos[x].lower_bound(y);
    FOR(i, 1, K) {
      ptr--;
      int y2 = *ptr;
      if (y - y2 > K || y2 < lb)
        break;
      dis[x][y2] = d + 1;
      Q.push(mkp(x, y2));
      dlist.pb(y2);
    }
    // going R
    ptr = Rpos[x].lower_bound(y);
    FOR(i, 1, K) {
      int y2 = *ptr;
      if (y2 - y > K || y2 > rb)
        break;
      dis[x][y2] = d + 1;
      Q.push(mkp(x, y2));
      dlist.pb(y2);
      ptr++;
    }
    // cleaning
    for (int yi : dlist) {
      // cout<<"cleaning "<<x<<' '<<yi<<'\n';
      Cpos[yi].erase(x);
      Rpos[x].erase(yi);
    }
    dlist.clear();

    // going UD
    ptr = CB[y].lower_bound(x);
    db = *ptr;
    ptr--;
    ub = *ptr;

    // going U
    ptr = Cpos[y].lower_bound(x);
    FOR(i, 1, K) {
      ptr--;
      int x2 = *ptr;
      if (x - x2 > K || x2 < ub)
        break;
      dis[x2][y] = d + 1;
      Q.push(mkp(x2, y));
      dlist.pb(x2);
    }
    // going R
    ptr = Cpos[y].lower_bound(x);
    FOR(i, 1, K) {
      int x2 = *ptr;
      if (x2 - x > K || x2 > db)
        break;
      dis[x2][y] = d + 1;
      Q.push(mkp(x2, y));
      ;
      dlist.pb(x2);
      ptr++;
    }
    // cleaning
    for (int xi : dlist) {
      // cout<<"cleaning "<<xi<<' '<<y<<'\n';
      Rpos[xi].erase(y);
      Cpos[y].erase(xi);
    }
  }

  if (dis[X2][Y2] == 1e9)
    cout << -1 << '\n';
  else
    cout << dis[X2][Y2] << '\n';

  return 0;
}