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


#include <bits/stdc++.h>

#define ln '\\n'
#define all(dat) dat.begin(), dat.end()
#define loop(i, to) for (int i = 0; i < to; ++i)
#define cont(i, to) for (int i = 1; i <= to; ++i)
#define circ(i, fm, to) for (int i = fm; i <= to; ++i)
#define foreach(i, dat)                                                        \\
  for (__typeof(dat.begin()) i = dat.begin(); i != dat.end(); ++i)

typedef long long num;

using namespace std;

const int nsz = int(1e3), msz = nsz * nsz;
const int dx[] = {-1, 0, 0, +1}, dy[] = {0, -1, +1, 0};
bool vis[nsz + 5][nsz + 5];
int n, m, d[nsz + 5][nsz + 5];
num ans;
pair<int, int> fm[msz + 5];

bool inline upd(int &a, int b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

void inline bfs(int sx, int sy) {
  static int qx[msz + 5], qy[msz + 5];
  int ql = 0, qr = 0;
  qx[qr++] = sx, qy[qr++] = sy;
  for (; ql != qr;) {
    int x = qx[ql++], y = qy[ql++];
    loop(dr, 4) {
      int cx = x + dx[dr], cy = y + dy[dr];
      if (cx < 1 || cy < 1 || cx > n || cy > n ||
          !upd(d[cx][cy], d[x][y] + !vis[x][y]))
        continue;
      qx[qr++] = cx, qy[qr++] = cy;
    }
  }
}

int main() {
  scanf("%d", &n);
  m = n * n;
  int s = 0;
  cont(i, n) cont(j, n) {
    fm[++s] = make_pair(i, j);
    d[i][j] = n;
    upd(d[i][j], i - 1);
    upd(d[i][j], j - 1);
    upd(d[i][j], n - i);
    upd(d[i][j], n - j);
  }
  cont(i, m) {
    int id, x, y;
    scanf("%d", &id);
    x = fm[id].first, y = fm[id].second;
    ans += d[x][y];
    vis[x][y] = 1;
    bfs(x, y);
  }
  printf("%lld\\n", ans);
}

Error: 0

Fixed submission:


#include <bits/stdc++.h>

#define ln '\\n'
#define all(dat) dat.begin(), dat.end()
#define loop(i, to) for (int i = 0; i < to; ++i)
#define cont(i, to) for (int i = 1; i <= to; ++i)
#define circ(i, fm, to) for (int i = fm; i <= to; ++i)
#define foreach(i, dat)                                                        \\
  for (__typeof(dat.begin()) i = dat.begin(); i != dat.end(); ++i)

typedef long long num;

using namespace std;

const int nsz = int(5e2), msz = nsz * nsz;
const int dx[] = {-1, 0, 0, +1}, dy[] = {0, -1, +1, 0};
bool vis[nsz + 5][nsz + 5];
int n, m, d[nsz + 5][nsz + 5];
num ans;
pair<int, int> fm[msz + 5];

bool inline upd(int &a, int b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

void inline bfs(int sx, int sy) {
  static int qx[msz + 5], qy[msz + 5];
  int ql = 0, qr = 0;
  qx[qr++] = sx, qy[qr++] = sy;
  for (; ql != qr;) {
    int x = qx[ql++], y = qy[ql++];
    loop(dr, 4) {
      int cx = x + dx[dr], cy = y + dy[dr];
      if (cx < 1 || cy < 1 || cx > n || cy > n ||
          !upd(d[cx][cy], d[x][y] + !vis[x][y]))
        continue;
      qx[qr++] = cx, qy[qr++] = cy;
    }
  }
}

int main() {
  scanf("%d", &n);
  m = n * n;
  int s = 0;
  cont(i, n) cont(j, n) {
    fm[++s] = make_pair(i, j);
    d[i][j] = n;
    upd(d[i][j], i - 1);
    upd(d[i][j], j - 1);
    upd(d[i][j], n - i);
    upd(d[i][j], n - j);
  }
  cont(i, m) {
    int id, x, y;
    scanf("%d", &id);
    x = fm[id].first, y = fm[id].second;
    ans += d[x][y];
    vis[x][y] = 1;
    bfs(x, y);
  }
  printf("%lld\\n", ans);
}