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

#pragma GCC optimize(3, "Ofast", "inline")
#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define ms(a, b) memset(a, b, sizeof(a))
#define msn(a, n, b)                                                           \\
  for (int i = 0; i <= n; i++)                                                 \\
  a[i] = b
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
#define fi first
#define se second
using namespace std;
mt19937 rng_32(chrono::steady_clock::now().time_since_epoch().count());
typedef long long ll;
typedef long double ld;
typedef pair<int, int> P;
typedef double db;
const int mod = 1e9 + 7;
const int seed = 233;
const double PI = acos(-1.0);
const double eps = 1e-7;
const int inf = 0x3f3f3f3f;
const int max_n = 100005;
ll ceil(ll x, ll y) {
  if (x == 0)
    return 0;
  if (y < 0)
    x = -x, y = -y;
  bool sign = x > 0;
  x = abs(x);
  return sign ? (x + y - 1) / y : -x / y;
}
ll floor(ll x, ll y) {
  if (x == 0)
    return 0;
  if (y < 0)
    x = -x, y = -y;
  bool sign = x > 0;
  x = abs(x);
  return sign ? x / y : -(x + y - 1) / y;
}
char str[max_n];
void reads(string &s) {
  scanf(" %s", str);
  s = str;
}
namespace {
inline int Add(int x, int y) { return (x += y) >= mod ? x - mod : x; }
inline int Sub(int x, int y) { return (x -= y) < 0 ? x + mod : x; }
inline int Mul(int x, int y) { return 1ll * x * y % mod; }
inline int Pow(int x, int y = mod - 2) {
  int res = 1;
  while (y) {
    if (y & 1)
      res = 1ll * res * x % mod;
    x = 1ll * x * x % mod;
    y >>= 1;
  }
  return res;
}
} // namespace
/**********************head************************/
int n, m, k;
int sx, sy, ex, ey;
int main() {
  scanf("%d%d%d", &n, &m, &k);
  scanf("%d%d%d%d", &sx, &sy, &ex, &ey);
  vector<vector<char>> a(n + 1, vector<char>(m + 1, 0));
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++)
      scanf(" %c", &a[i][j]);
  }
  vector<vector<int>> lef2(n + 2, vector<int>(m + 2, 0));
  vector<vector<int>> rig2(n + 2, vector<int>(m + 2, 0));
  vector<vector<int>> dwn2(m + 2, vector<int>(n + 2, 0));
  vector<vector<int>> upp2(m + 2, vector<int>(n + 2, 0));

  vector<vector<int>> lef(n + 2, vector<int>(m + 2, 0));
  vector<vector<int>> rig(n + 2, vector<int>(m + 2, 0));
  vector<vector<int>> dwn(m + 2, vector<int>(n + 2, 0));
  vector<vector<int>> upp(m + 2, vector<int>(n + 2, 0));
  vector<vector<int>> dis(n + 1, vector<int>(m + 1, 0));
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++)
      dis[i][j] = inf;
  }
  dis[sx][sy] = 0;
  queue<P> que;
  que.push(P(sx, sy));
  function<int(vector<int> &, int)> findpar = [&](vector<int> &f, int x) {
    return x == f[x] ? x : f[x] = findpar(f, f[x]);
  };
  vector<vector<bool>> vis(n + 2, vector<bool>(m + 2, 0));
  auto add = [&](int x, int y) {
    assert(vis[x][y] == false);
    vis[x][y] = true;
    // upp[x][y]=x-1,dwn[x][y]=x+1;
    /*
    lef[x][y]=y-1,rig[x][y]=y+1;
    upp[y][x]=x-1,dwn[y][x]=x+1;
    */

    lef[x][y] = findpar(lef[x], y - 1);
    rig[x][y] = findpar(rig[x], y + 1);
    upp[y][x] = findpar(upp[y], x - 1);
    dwn[y][x] = findpar(dwn[y], x + 1);
  };
  for (int i = 0; i <= n + 1; i++) {
    for (int j = 0; j <= m + 1; j++) {
      lef[i][j] = rig[i][j] = j;
      upp[j][i] = dwn[j][i] = i;
    }
  }
  for (int i = 1; i <= n; i++) {
    lef2[i][0] = 0;
    for (int j = 1; j <= m; j++) {
      if (a[i][j] == '@')
        lef2[i][j] = j;
      else
        lef2[i][j] = lef2[i][j - 1];
    }
    rig2[i][m + 1] = m + 1;
    for (int j = m; j >= 1; j--) {
      if (a[i][j] == '@')
        rig2[i][j] = j;
      else
        rig2[i][j] = rig2[i][j + 1];
    }
  }
  for (int j = 1; j <= m; j++) {
    upp2[j][0] = 0;
    for (int i = 1; i <= n; i++) {
      if (a[i][j] == '@')
        upp2[j][i] = i;
      else
        upp2[j][i] = upp2[j][i - 1];
    }
    dwn2[j][n + 1] = n + 1;
    for (int i = n; i >= 1; i--) {
      if (a[i][j] == '@')
        dwn2[j][i] = i;
      else
        dwn2[j][i] = dwn2[j][i + 1];
    }
  }
  add(sx, sy);
  while (!que.empty()) {
    P p = que.front();
    que.pop();
    int x = p.fi, y = p.se;
    if (x == ex && y == ey) {
      printf("%d\\n", dis[ex][ey]);
      return 0;
    }
    for (int nx = findpar(dwn[y], x + 1);
         nx <= min(min(x + k, n), dwn2[y][x + 1] - 1);
         nx = findpar(dwn[y], nx + 1)) {
      add(nx, y);
      // cout<<nx<<' '<<y<<endl;
      que.push(P(nx, y));
      dis[nx][y] = dis[x][y] + 1;
    }
    for (int nx = findpar(upp[y], x - 1);
         nx >= max(max(1, x - k), upp2[y][x - 1] + 1);
         nx = findpar(upp[y], nx - 1)) {
      // cout<<nx<<' '<<y<<endl;
      add(nx, y);
      que.push(P(nx, y));
      dis[nx][y] = dis[x][y] + 1;
    }
    for (int ny = findpar(rig[x], y + 1);
         ny <= min(min(y + k, m), rig2[x][y + 1] - 1);
         ny = findpar(rig[x], ny + 1)) {
      add(x, ny);
      que.push(P(x, ny));
      dis[x][ny] = dis[x][y] + 1;
    }
    for (int ny = findpar(lef[x], y - 1);
         ny >= max(max(1, y - k), lef2[x][y - 1] + 1);
         ny = findpar(lef[x], ny - 1)) {
      add(x, ny);
      que.push(P(x, ny));
      dis[x][ny] = dis[x][y] + 1;
    }
    // break;
  }
  printf("-1\\n");
  return 0;
}

Error: -6
b9ceed11-a6ba-47f4-9233-21fc28127a6c.out: /home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02644/C++/s642324749.cpp:63: main()::<lambda(int, int)>: Assertion `vis[x][y]==false' failed.


Fixed submission:

#pragma GCC optimize(3, "Ofast", "inline")
#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define ms(a, b) memset(a, b, sizeof(a))
#define msn(a, n, b)                                                           \\
  for (int i = 0; i <= n; i++)                                                 \\
  a[i] = b
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
#define fi first
#define se second
using namespace std;
mt19937 rng_32(chrono::steady_clock::now().time_since_epoch().count());
typedef long long ll;
typedef long double ld;
typedef pair<int, int> P;
typedef double db;
const int mod = 1e9 + 7;
const int seed = 233;
const double PI = acos(-1.0);
const double eps = 1e-7;
const int inf = 0x3f3f3f3f;
const int max_n = 100005;
ll ceil(ll x, ll y) {
  if (x == 0)
    return 0;
  if (y < 0)
    x = -x, y = -y;
  bool sign = x > 0;
  x = abs(x);
  return sign ? (x + y - 1) / y : -x / y;
}
ll floor(ll x, ll y) {
  if (x == 0)
    return 0;
  if (y < 0)
    x = -x, y = -y;
  bool sign = x > 0;
  x = abs(x);
  return sign ? x / y : -(x + y - 1) / y;
}
char str[max_n];
void reads(string &s) {
  scanf(" %s", str);
  s = str;
}
namespace {
inline int Add(int x, int y) { return (x += y) >= mod ? x - mod : x; }
inline int Sub(int x, int y) { return (x -= y) < 0 ? x + mod : x; }
inline int Mul(int x, int y) { return 1ll * x * y % mod; }
inline int Pow(int x, int y = mod - 2) {
  int res = 1;
  while (y) {
    if (y & 1)
      res = 1ll * res * x % mod;
    x = 1ll * x * x % mod;
    y >>= 1;
  }
  return res;
}
} // namespace
/**********************head************************/
int n, m, k;
int sx, sy, ex, ey;
int main() {
  scanf("%d%d%d", &n, &m, &k);
  scanf("%d%d%d%d", &sx, &sy, &ex, &ey);
  vector<vector<char>> a(n + 1, vector<char>(m + 1, 0));
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++)
      scanf(" %c", &a[i][j]);
  }
  vector<vector<int>> lef2(n + 2, vector<int>(m + 2, 0));
  vector<vector<int>> rig2(n + 2, vector<int>(m + 2, 0));
  vector<vector<int>> dwn2(m + 2, vector<int>(n + 2, 0));
  vector<vector<int>> upp2(m + 2, vector<int>(n + 2, 0));

  vector<vector<int>> lef(n + 2, vector<int>(m + 2, 0));
  vector<vector<int>> rig(n + 2, vector<int>(m + 2, 0));
  vector<vector<int>> dwn(m + 2, vector<int>(n + 2, 0));
  vector<vector<int>> upp(m + 2, vector<int>(n + 2, 0));
  vector<vector<int>> dis(n + 1, vector<int>(m + 1, 0));
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++)
      dis[i][j] = inf;
  }
  dis[sx][sy] = 0;
  queue<P> que;
  que.push(P(sx, sy));
  function<int(vector<int> &, int)> findpar = [&](vector<int> &f, int x) {
    return x == f[x] ? x : f[x] = findpar(f, f[x]);
  };
  vector<vector<bool>> vis(n + 2, vector<bool>(m + 2, 0));
  auto add = [&](int x, int y) {
    assert(vis[x][y] == false);
    vis[x][y] = true;
    // upp[x][y]=x-1,dwn[x][y]=x+1;
    /*
    lef[x][y]=y-1,rig[x][y]=y+1;
    upp[y][x]=x-1,dwn[y][x]=x+1;
    */
    lef[x][y] = findpar(lef[x], y - 1);
    rig[x][y] = findpar(rig[x], y + 1);
    upp[y][x] = findpar(upp[y], x - 1);
    dwn[y][x] = findpar(upp[y], x + 1);
  };
  for (int i = 0; i <= n + 1; i++) {
    for (int j = 0; j <= m + 1; j++) {
      lef[i][j] = rig[i][j] = j;
      upp[j][i] = dwn[j][i] = i;
    }
  }
  for (int i = 1; i <= n; i++) {
    lef2[i][0] = 0;
    for (int j = 1; j <= m; j++) {
      if (a[i][j] == '@')
        lef2[i][j] = j;
      else
        lef2[i][j] = lef2[i][j - 1];
    }
    rig2[i][m + 1] = m + 1;
    for (int j = m; j >= 1; j--) {
      if (a[i][j] == '@')
        rig2[i][j] = j;
      else
        rig2[i][j] = rig2[i][j + 1];
    }
  }
  for (int j = 1; j <= m; j++) {
    upp2[j][0] = 0;
    for (int i = 1; i <= n; i++) {
      if (a[i][j] == '@')
        upp2[j][i] = i;
      else
        upp2[j][i] = upp2[j][i - 1];
    }
    dwn2[j][n + 1] = n + 1;
    for (int i = n; i >= 1; i--) {
      if (a[i][j] == '@')
        dwn2[j][i] = i;
      else
        dwn2[j][i] = dwn2[j][i + 1];
    }
  }
  add(sx, sy);
  while (!que.empty()) {
    P p = que.front();
    que.pop();
    int x = p.fi, y = p.se;
    if (x == ex && y == ey) {
      printf("%d\\n", dis[ex][ey]);
      return 0;
    }
    for (int nx = findpar(dwn[y], x + 1);
         nx <= min(min(x + k, n), dwn2[y][x + 1] - 1);
         nx = findpar(dwn[y], nx + 1)) {
      add(nx, y);
      // cout<<nx<<' '<<y<<endl;
      que.push(P(nx, y));
      dis[nx][y] = dis[x][y] + 1;
    }
    for (int nx = findpar(upp[y], x - 1);
         nx >= max(max(1, x - k), upp2[y][x - 1] + 1);
         nx = findpar(upp[y], nx - 1)) {
      // cout<<nx<<' '<<y<<endl;
      add(nx, y);
      que.push(P(nx, y));
      dis[nx][y] = dis[x][y] + 1;
    }
    for (int ny = findpar(rig[x], y + 1);
         ny <= min(min(y + k, m), rig2[x][y + 1] - 1);
         ny = findpar(rig[x], ny + 1)) {
      add(x, ny);
      que.push(P(x, ny));
      dis[x][ny] = dis[x][y] + 1;
    }
    for (int ny = findpar(lef[x], y - 1);
         ny >= max(max(1, y - k), lef2[x][y - 1] + 1);
         ny = findpar(lef[x], ny - 1)) {
      add(x, ny);
      que.push(P(x, ny));
      dis[x][ny] = dis[x][y] + 1;
    }
    // break;
  }
  printf("-1\\n");
  return 0;
}