prithivMLmods/Coder-Stat huggingface dataset:
Problem p02579 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

#define rep(X, N) for (ll X = 0LL; X < (N); X++)
#define ALL(V) (V).begin(), (V).end()
#define endl "\n"

using namespace std;
typedef long long ll;

const double PI = 3.1415926535897932384626;
const ll MODN = 1000000007;
const ll MODN2 = 998244353;
const double EPS = 1e-10;

int main() {

  int h, w;
  cin >> h >> w;
  int ch, cw, dh, dw;
  cin >> ch >> cw;
  cin >> dh >> dw;

  ch--;
  cw--;
  dh--;
  dw--;

  vector<string> s(h);

  rep(i, h) { cin >> s[i]; }

  deque<pair<int, pair<int, int>>> deq;
  vector<vector<int>> dist(h, vector<int>(w, h * w));

  deq.push_front(make_pair(0, make_pair(ch, cw)));

  while (!deq.empty()) {
    int tmpd = deq.front().first;
    int tmpy = deq.front().second.first;
    int tmpx = deq.front().second.second;

    deq.pop_front();

    if (tmpd < dist[tmpy][tmpx]) {
      cerr << tmpy << " " << tmpx << "  " << tmpd << endl;
      dist[tmpy][tmpx] = tmpd;

      for (int i = -2; i <= 2; i++) {
        for (int j = -2; j <= 2; j++) {
          int nexty = tmpy + i;
          int nextx = tmpx + j;

          if (0 <= nexty && nexty < h && 0 <= nextx && nextx < w &&
              s[nexty][nextx] == '.') {
            if (abs(i) + abs(j) == 0) {

            } else if (abs(i) + abs(j) == 1) {
              deq.push_front(make_pair(tmpd, make_pair(nexty, nextx)));
            } else {
              deq.push_back(make_pair(tmpd + 1, make_pair(nexty, nextx)));
            }
          }
        }
      }
    }
  }

  cout << (dist[dh][dw] == h * w ? -1 : dist[dh][dw]) << endl;
}

Fixed submission:
#include <bits/stdc++.h>

#define rep(X, N) for (ll X = 0LL; X < (N); X++)
#define ALL(V) (V).begin(), (V).end()
#define endl "\n"

using namespace std;
typedef long long ll;

const double PI = 3.1415926535897932384626;
const ll MODN = 1000000007;
const ll MODN2 = 998244353;
const double EPS = 1e-10;

int main() {

  int h, w;
  cin >> h >> w;
  int ch, cw, dh, dw;
  cin >> ch >> cw;
  cin >> dh >> dw;

  ch--;
  cw--;
  dh--;
  dw--;

  vector<string> s(h);

  rep(i, h) { cin >> s[i]; }

  deque<pair<int, pair<int, int>>> deq;
  vector<vector<int>> dist(h, vector<int>(w, h * w));

  deq.push_front(make_pair(0, make_pair(ch, cw)));

  while (!deq.empty()) {
    int tmpd = deq.front().first;
    int tmpy = deq.front().second.first;
    int tmpx = deq.front().second.second;

    deq.pop_front();

    if (tmpd < dist[tmpy][tmpx]) {
      dist[tmpy][tmpx] = tmpd;

      for (int i = -2; i <= 2; i++) {
        for (int j = -2; j <= 2; j++) {
          int nexty = tmpy + i;
          int nextx = tmpx + j;

          if (0 <= nexty && nexty < h && 0 <= nextx && nextx < w &&
              s[nexty][nextx] == '.') {
            if (abs(i) + abs(j) == 0) {

            } else if (abs(i) + abs(j) == 1) {
              deq.push_front(make_pair(tmpd, make_pair(nexty, nextx)));
            } else {
              deq.push_back(make_pair(tmpd + 1, make_pair(nexty, nextx)));
            }
          }
        }
      }
    }
  }

  cout << (dist[dh][dw] == h * w ? -1 : dist[dh][dw]) << endl;
}
Problem p02579 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <queue>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;

const int di[] = {-1, 0, 1, 0};
const int dj[] = {0, 1, 0, -1};

int main() {
  int h, w;
  cin >> h >> w;
  int si, sj;
  cin >> si >> sj;
  int ti, tj;
  cin >> ti >> tj;
  si--;
  sj--;
  ti--;
  tj--;

  vector<string> s(h);
  rep(i, h) { cin >> s[i]; }
  const int INF = 1e9;

  vector<vector<int>> dist(h, vector<int>(w, INF));
  deque<P> q;
  dist[si][sj] = 0;
  q.emplace_back(si, sj);

  while (!q.empty()) {
    int i = q.front().first;
    int j = q.front().second;
    int d = dist[i][j];

    q.pop_front();
    if (dist[i][j] != d) {
      continue;
    }
    rep(v, 4) {
      int ni = i + di[v];
      int nj = j + dj[v];

      if (ni < 0 || ni >= h || nj < 0 || nj >= w) {
        continue;
      }
      if (s[ni][nj] == '#')
        continue;
      if (dist[ni][nj] <= d) {
        continue;
      }

      dist[nj][nj] = d;
      q.emplace_front(ni, nj);
    }

    for (int ei = -2; ei <= 2; ei++) {
      for (int ej = -2; ej <= 2; ej++) {
        int ni = i + ei;
        int nj = j + ej;
        if (ni < 0 || ni >= h || nj < 0 || nj >= w) {
          continue;
        }
        if (s[ni][nj] == '#')
          continue;
        if (dist[ni][nj] <= d + 1)
          continue;
        dist[ni][nj] = d + 1;
        q.emplace_back(ni, nj);
      }
    }
  }
  int ans = dist[ti][tj];
  if (ans == INF)
    ans = -1;
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <iostream>
#include <queue>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;

const int di[] = {-1, 0, 1, 0};
const int dj[] = {0, 1, 0, -1};

int main() {
  int h, w;
  cin >> h >> w;
  int si, sj;
  cin >> si >> sj;
  int ti, tj;
  cin >> ti >> tj;
  si--;
  sj--;
  ti--;
  tj--;

  vector<string> s(h);
  rep(i, h) { cin >> s[i]; }
  const int INF = 1e9;

  vector<vector<int>> dist(h, vector<int>(w, INF));
  deque<P> q;
  dist[si][sj] = 0;
  q.emplace_back(si, sj);

  while (!q.empty()) {
    int i = q.front().first;
    int j = q.front().second;
    int d = dist[i][j];

    q.pop_front();
    if (dist[i][j] != d) {
      continue;
    }
    rep(v, 4) {
      int ni = i + di[v];
      int nj = j + dj[v];

      if (ni < 0 || ni >= h || nj < 0 || nj >= w) {
        continue;
      }
      if (s[ni][nj] == '#')
        continue;
      if (dist[ni][nj] <= d) {
        continue;
      }

      dist[ni][nj] = d;
      q.emplace_front(ni, nj);
    }

    for (int ei = -2; ei <= 2; ei++) {
      for (int ej = -2; ej <= 2; ej++) {
        int ni = i + ei;
        int nj = j + ej;
        if (ni < 0 || ni >= h || nj < 0 || nj >= w) {
          continue;
        }
        if (s[ni][nj] == '#')
          continue;
        if (dist[ni][nj] <= d + 1)
          continue;
        dist[ni][nj] = d + 1;
        q.emplace_back(ni, nj);
      }
    }
  }
  int ans = dist[ti][tj];
  if (ans == INF)
    ans = -1;
  cout << ans << endl;
  return 0;
}
Problem p02579 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
#define endl "\n"

i64 dh[4] = {-1, 1, 0, 0}, dw[4] = {0, 0, -1, 1};

int main() {
  i64 H, W, Ch, Cw, Dh, Dw;
  cin >> H >> W >> Ch >> Cw >> Dh >> Dw;
  Ch--;
  Cw--;
  Dh--;
  Dw--;
  vector<string> S(H);
  for (i64 i = 0; i < H; i++)
    cin >> S[i];
  vector<vector<i64>> dis(H, vector<i64>(W, 1e9));
  deque<pair<i64, i64>> que;
  dis[Ch][Cw] = 0;
  que.push_back({Ch, Cw});
  while (que.size()) {
    pair<i64, i64> p = que.front();
    que.pop_front();
    for (i64 i = 0; i < 4; i++) {
      i64 ddh = p.first + dh[i], ddw = p.second + dw[i];
      if (ddh < 0 || H <= ddh || ddw < 0 || W <= ddw || S[ddh][ddw] == '#' ||
          dis[ddh][ddw] <= dis[p.first][p.second])
        continue;
      dis[ddh][ddh] = dis[p.first][p.second];
      que.push_front({ddh, ddw});
    }
    for (i64 i = -2; i < 3; i++)
      for (i64 j = -2; j < 3; j++) {
        i64 ddh = p.first + i, ddw = p.second + j;
        if (ddh < 0 || H <= ddh || ddw < 0 || W <= ddw || S[ddh][ddw] == '#' ||
            dis[ddh][ddw] <= dis[p.first][p.second] + 1)
          continue;
        dis[ddh][ddw] = dis[p.first][p.second] + 1;
        que.push_back({ddh, ddw});
      }
  }
  cout << (dis[Dh][Dw] == 1e9 ? -1 : dis[Dh][Dw]) << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
#define endl "\n"

i64 dh[4] = {-1, 1, 0, 0}, dw[4] = {0, 0, -1, 1};

int main() {
  i64 H, W, Ch, Cw, Dh, Dw;
  cin >> H >> W >> Ch >> Cw >> Dh >> Dw;
  Ch--;
  Cw--;
  Dh--;
  Dw--;
  vector<string> S(H);
  for (i64 i = 0; i < H; i++)
    cin >> S[i];
  vector<vector<i64>> dis(H, vector<i64>(W, 1e9));
  deque<pair<i64, i64>> que;
  dis[Ch][Cw] = 0;
  que.push_back({Ch, Cw});
  while (que.size()) {
    pair<i64, i64> p = que.front();
    que.pop_front();
    for (i64 i = 0; i < 4; i++) {
      i64 ddh = p.first + dh[i], ddw = p.second + dw[i];
      if (ddh < 0 || H <= ddh || ddw < 0 || W <= ddw || S[ddh][ddw] == '#' ||
          dis[ddh][ddw] <= dis[p.first][p.second])
        continue;
      dis[ddh][ddw] = dis[p.first][p.second];
      que.push_front({ddh, ddw});
    }
    for (i64 i = -2; i < 3; i++)
      for (i64 j = -2; j < 3; j++) {
        i64 ddh = p.first + i, ddw = p.second + j;
        if (ddh < 0 || H <= ddh || ddw < 0 || W <= ddw || S[ddh][ddw] == '#' ||
            dis[ddh][ddw] <= dis[p.first][p.second] + 1)
          continue;
        dis[ddh][ddw] = dis[p.first][p.second] + 1;
        que.push_back({ddh, ddw});
      }
  }
  cout << (dis[Dh][Dw] == 1e9 ? -1 : dis[Dh][Dw]) << endl;
  return 0;
}
Problem p02579 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
struct node {
  int x, y;
} t;
char a[1005][1005];
int inq[1005][1005];
int n, m;
deque<node> q;
bool check(int x, int y, int w) {
  return !(inq[x][y] <= w || x < 1 || x > n || y < 1 || y > m);
}
signed main() {
  int i, j, sx, sy, ex, ey;
  string s;
  cin >> n >> m >> sx >> sy >> ex >> ey;
  for (i = 1; i <= n; i++) {
    cin >> s;
    for (j = 1; j <= m; j++) {
      a[i][j] = s[j - 1];
      if (a[i][j] == '#')
        inq[i][j] = -1;
      else
        inq[i][j] = 2147483647;
    }
  }
  q.push_front((node){sx, sy});
  inq[sx][sy] = 0;
  while (q.size()) {
    t = q.front();
    q.pop_front();
    if (t.x == ex && t.y == ey) {
      cout << inq[ex][ey];
      while (q.size())
        q.pop_front();
      return 0;
    }
    for (i = 0; i < 4; i++)
      if (check(t.x + dx[i], t.y + dy[i], inq[t.x][t.y])) {
        inq[t.x + dx[i]][t.y + dy[i]] = inq[t.x][t.y];
        q.push_front((node){t.x + dx[i], t.y + dy[i]});
      }
    for (i = -2; i < 3; i++)
      for (j = -2; j < 3; j++)
        if (check(t.x + i, t.y + j, inq[t.x][t.y] + 1)) {
          inq[t.x + i][t.y + j] = inq[t.x][t.y] + 1;
          q.push_back((node){t.x + i, t.y + j});
        }
  }
  cout << -1;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
struct node {
  int x, y;
} t;
char a[1005][1005];
int inq[1005][1005];
int n, m;
deque<node> q;
bool check(int x, int y, int w) {
  return !(x < 1 || x > n || y < 1 || y > m || inq[x][y] <= w);
}
signed main() {
  int i, j, sx, sy, ex, ey;
  string s;
  cin >> n >> m >> sx >> sy >> ex >> ey;
  for (i = 1; i <= n; i++) {
    cin >> s;
    for (j = 1; j <= m; j++) {
      a[i][j] = s[j - 1];
      if (a[i][j] == '#')
        inq[i][j] = -1;
      else
        inq[i][j] = 2147483647;
    }
  }
  q.push_front((node){sx, sy});
  inq[sx][sy] = 0;
  while (q.size()) {
    t = q.front();
    q.pop_front();
    if (t.x == ex && t.y == ey) {
      cout << inq[ex][ey];
      while (q.size())
        q.pop_front();
      return 0;
    }
    for (i = 0; i < 4; i++)
      if (check(t.x + dx[i], t.y + dy[i], inq[t.x][t.y])) {
        inq[t.x + dx[i]][t.y + dy[i]] = inq[t.x][t.y];
        q.push_front((node){t.x + dx[i], t.y + dy[i]});
      }
    for (i = -2; i < 3; i++)
      for (j = -2; j < 3; j++)
        if (check(t.x + i, t.y + j, inq[t.x][t.y] + 1)) {
          inq[t.x + i][t.y + j] = inq[t.x][t.y] + 1;
          q.push_back((node){t.x + i, t.y + j});
        }
  }
  cout << -1;
  return 0;
}
Problem p02579 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
// #include <atcoder/all>
#define rep(i, n) for (int i = 0; i < (n); i++)
using ll = long long;
using namespace std;
// using namespace atcoder;

int main() {
  int h, w, sy, sx, gy, gx;
  cin >> h >> w >> sy >> sx >> gy >> gx;
  sy--, sx--, gy--, gx--;
  string s[h];
  for (int i = 0; i < h; i++)
    cin >> s[i];
  int dis[h][w];
  rep(i, h) rep(j, w) dis[i][j] = 1e9;
  dis[sy][sx] = 0;
  deque<pair<int, int>> q;
  q.push_back({sy, sx});
  int dy[] = {0, 0, -1, 1}, dx[] = {-1, 1, 0, 0};
  while (q.size()) {
    auto a = q.back();
    q.pop_back();
    int y = a.first, x = a.second;
    rep(i, 4) {
      int ny = y + dy[i], nx = x + dx[i];
      if (0 <= ny && ny < h && 0 <= nx && nx < w && s[ny][nx] == '.' &&
          dis[y][x] < dis[ny][nx]) {
        dis[ny][nx] = dis[y][x];
        q.push_front({ny, nx});
      }
    }
    for (int i = -2; i <= 2; i++)
      for (int j = -2; j <= 2; j++) {
        int ny = y + i, nx = x + j;
        if (0 <= ny && ny < h && 0 <= nx && nx < w && s[ny][nx] == '.' &&
            dis[y][x] + 1 < dis[ny][nx]) {
          dis[ny][nx] = dis[y][x] + 1;
          q.push_back({ny, nx});
        }
      }
  }
  if (dis[gy][gx] == 1e9)
    cout << -1 << endl;
  else
    cout << dis[gy][gx] << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
// #include <atcoder/all>
#define rep(i, n) for (int i = 0; i < (n); i++)
using ll = long long;
using namespace std;
// using namespace atcoder;

int main() {
  int h, w, sy, sx, gy, gx;
  cin >> h >> w >> sy >> sx >> gy >> gx;
  sy--, sx--, gy--, gx--;
  string s[h];
  for (int i = 0; i < h; i++)
    cin >> s[i];
  int dis[h][w];
  rep(i, h) rep(j, w) dis[i][j] = 1e9;
  dis[sy][sx] = 0;
  deque<pair<int, int>> q;
  q.push_back({sy, sx});
  int dy[] = {0, 0, -1, 1}, dx[] = {-1, 1, 0, 0};
  while (q.size()) {
    auto a = q.front();
    q.pop_front();
    int y = a.first, x = a.second;
    rep(i, 4) {
      int ny = y + dy[i], nx = x + dx[i];
      if (0 <= ny && ny < h && 0 <= nx && nx < w && s[ny][nx] == '.' &&
          dis[y][x] < dis[ny][nx]) {
        dis[ny][nx] = dis[y][x];
        q.push_front({ny, nx});
      }
    }
    for (int i = -2; i <= 2; i++)
      for (int j = -2; j <= 2; j++) {
        int ny = y + i, nx = x + j;
        if (0 <= ny && ny < h && 0 <= nx && nx < w && s[ny][nx] == '.' &&
            dis[y][x] + 1 < dis[ny][nx]) {
          dis[ny][nx] = dis[y][x] + 1;
          q.push_back({ny, nx});
        }
      }
  }
  if (dis[gy][gx] == 1e9)
    cout << -1 << endl;
  else
    cout << dis[gy][gx] << endl;
  return 0;
}
Problem p02579 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(v) v.begin(), v.end()
#define len(x) (ll)(x).length()
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int INF = 1e9;
const int di[] = {-1, 0, 1, 0};
const int dj[] = {0, -1, 0, 1};

int main() {
  int h, w;
  cin >> h >> w;
  int si, sj;
  cin >> si >> sj;
  int ti, tj;
  cin >> ti >> tj;
  si--;
  sj--;
  ti--;
  tj--;
  vector<string> s(h);
  rep(i, h) cin >> s[i];
  vector<vector<int>> dist(h, vector<int>(w, INF));
  deque<P> q;
  dist[si][sj] = 0;
  q.emplace_back(si, sj);
  while (!q.empty()) {
    int i = q.front().first;
    int j = q.front().second;
    int d = dist[i][j];
    q.pop_front();
    rep(v, 4) {
      int ni = i + di[v], nj = j + dj[v];
      if (ni < 0 || ni >= h || nj < 0 || nj >= w)
        continue;
      if (s[ni][nj] == '#')
        continue;
      if (dist[ni][nj] <= d)
        continue;
      dist[ni][nj] = d;
      q.emplace_front(ni, nj);
    }
    for (int ei = -2; ei <= 2; ei++) {
      for (int ej = -2; ej <= 2; ej++) {
        int ni = i + ei, nj = j + ej;
        if (ni < 0 || ni >= h || nj < 0 || nj >= w)
          continue;
        if (s[ni][nj] == '#')
          continue;
        if (dist[ni][nj] <= d + 1)
          continue;
        dist[ni][nj] = d + 1;
        q.emplace_front(ni, nj);
      }
    }
  }
  int ans = dist[ti][tj];
  if (ans == INF)
    ans = -1;
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(v) v.begin(), v.end()
#define len(x) (ll)(x).length()
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int INF = 1e9;
const int di[] = {-1, 0, 1, 0};
const int dj[] = {0, -1, 0, 1};

int main() {
  int h, w;
  cin >> h >> w;
  int si, sj;
  cin >> si >> sj;
  int ti, tj;
  cin >> ti >> tj;
  si--;
  sj--;
  ti--;
  tj--;
  vector<string> s(h);
  rep(i, h) cin >> s[i];
  vector<vector<int>> dist(h, vector<int>(w, INF));
  deque<P> q;
  dist[si][sj] = 0;
  q.emplace_back(si, sj);
  while (!q.empty()) {
    int i = q.front().first;
    int j = q.front().second;
    int d = dist[i][j];
    q.pop_front();
    rep(v, 4) {
      int ni = i + di[v], nj = j + dj[v];
      if (ni < 0 || ni >= h || nj < 0 || nj >= w)
        continue;
      if (s[ni][nj] == '#')
        continue;
      if (dist[ni][nj] <= d)
        continue;
      dist[ni][nj] = d;
      q.emplace_front(ni, nj);
    }
    for (int ei = -2; ei <= 2; ei++) {
      for (int ej = -2; ej <= 2; ej++) {
        int ni = i + ei, nj = j + ej;
        if (ni < 0 || ni >= h || nj < 0 || nj >= w)
          continue;
        if (s[ni][nj] == '#')
          continue;
        if (dist[ni][nj] <= d + 1)
          continue;
        dist[ni][nj] = d + 1;
        q.emplace_back(ni, nj);
      }
    }
  }
  int ans = dist[ti][tj];
  if (ans == INF)
    ans = -1;
  cout << ans << endl;
}
Problem p02579 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using lint = long long;

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

  int N, M;
  cin >> N >> M;
  int sx, sy, ex, ey;
  cin >> sx >> sy >> ex >> ey;
  sx--, sy--, ex--, ey--;
  vector<string> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  deque<pair<pair<int, int>, int>> dq;
  dq.push_back({{sx, sy}, 0});
  vector<vector<int>> dist(N, vector<int>(M, -1));
  dist[sx][sy] = 0;
  while (!dq.empty()) {
    int x = dq.front().first.first;
    int y = dq.front().first.second;
    int d = dq.front().second;
    dq.pop_front();
    if (dist[x][y] != d) {
      continue;
    }
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        int nx = x + i, ny = y + j;
        if (abs(i) + abs(j) == 1 && 0 <= nx && nx < N && 0 <= ny && ny < M &&
            A[nx][ny] != '#') {
          if (dist[nx][ny] == -1 || dist[nx][ny] > dist[x][y]) {
            dist[nx][ny] = dist[x][y];
            dq.push_front({{nx, ny}, dist[nx][ny]});
          }
        }
        if (0 <= nx && nx < N && 0 <= ny && ny < M && A[nx][ny] != '#') {
          if (dist[nx][ny] == -1 || dist[nx][ny] > dist[x][y] + 1) {
            dist[nx][ny] = dist[x][y] + 1;
            dq.push_front({{nx, ny}, dist[nx][ny]});
          }
        }
      }
    }
  }
  cout << dist[ex][ey] << "\n";
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using lint = long long;

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

  int N, M;
  cin >> N >> M;
  int sx, sy, ex, ey;
  cin >> sx >> sy >> ex >> ey;
  sx--, sy--, ex--, ey--;
  vector<string> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  deque<pair<pair<int, int>, int>> dq;
  dq.push_back({{sx, sy}, 0});
  vector<vector<int>> dist(N, vector<int>(M, -1));
  dist[sx][sy] = 0;
  while (!dq.empty()) {
    int x = dq.front().first.first;
    int y = dq.front().first.second;
    int d = dq.front().second;
    dq.pop_front();
    if (dist[x][y] != d) {
      continue;
    }
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        int nx = x + i, ny = y + j;
        if (abs(i) + abs(j) == 1 && 0 <= nx && nx < N && 0 <= ny && ny < M &&
            A[nx][ny] != '#') {
          if (dist[nx][ny] == -1 || dist[nx][ny] > dist[x][y]) {
            dist[nx][ny] = dist[x][y];
            dq.push_front({{nx, ny}, dist[nx][ny]});
          }
        }
        if (0 <= nx && nx < N && 0 <= ny && ny < M && A[nx][ny] != '#') {
          if (dist[nx][ny] == -1 || dist[nx][ny] > dist[x][y] + 1) {
            dist[nx][ny] = dist[x][y] + 1;
            dq.push_back({{nx, ny}, dist[nx][ny]});
          }
        }
      }
    }
  }
  cout << dist[ex][ey] << "\n";
  return 0;
}
Problem p02579 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

int main() {
  ios_base ::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  int n, m;
  cin >> n >> m;

  int x, y;
  cin >> x >> y;

  int t_x, t_y;
  cin >> t_x >> t_y;
  x--, y--;
  t_x--, t_y--;

  vector<vector<char>> grid(n, vector<char>(m));
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j)
      cin >> grid[i][j];

  vector<vector<long long>> sp(n, vector<long long>(m, -1));
  priority_queue<pair<long long, pair<int, int>>> pq;
  pq.push({0, {x, y}});

  vector<int> X0 = {1, -1, 0, 0};
  vector<int> Y0 = {0, 0, 1, -1};

  vector<int> X1 = {-2, -2, -2, -2, -2, -1, -1, -1, -1, 0,
                    0,  1,  1,  1,  1,  2,  2,  2,  2,  2};
  vector<int> Y1 = {-2, -1, 0,  1, 2, -2, -1, 1, 2, -2,
                    2,  -2, -1, 1, 2, -2, -1, 0, 1, 2};

  while (pq.empty() ^ 1) {
    int cx = pq.top().second.first;
    int cy = pq.top().second.second;
    int cost = pq.top().first * -1;

    pq.pop();
    if (cx < 0 || cx >= n || cy < 0 || cy >= m || grid[cx][cy] == '#' ||
        sp[cx][cy] != -1)
      continue;
    sp[cx][cy] = cost;

    for (int i = 0; i < 4; ++i)
      pq.push({-cost, {cx + X0[i], cy + Y0[i]}});

    for (int i = 0; i < 20; ++i)
      pq.push({-(cost + 1), {cx + X1[i], cy + Y1[i]}});
  };

  cout << sp[t_x][t_y];
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int main() {
  ios_base ::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  int n, m;
  cin >> n >> m;

  int x, y;
  cin >> x >> y;

  int t_x, t_y;
  cin >> t_x >> t_y;
  x--, y--;
  t_x--, t_y--;

  vector<vector<char>> grid(n, vector<char>(m));
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j)
      cin >> grid[i][j];

  vector<vector<long long>> sp(n, vector<long long>(m, -1));
  priority_queue<pair<long long, pair<int, int>>> pq;
  pq.push({0, {x, y}});

  vector<int> X0 = {1, -1, 0, 0};
  vector<int> Y0 = {0, 0, 1, -1};

  vector<int> X1 = {-2, -2, -2, -2, -2, -1, -1, -1, -1, 0,
                    0,  1,  1,  1,  1,  2,  2,  2,  2,  2};
  vector<int> Y1 = {-2, -1, 0,  1, 2, -2, -1, 1, 2, -2,
                    2,  -2, -1, 1, 2, -2, -1, 0, 1, 2};

  while (pq.empty() ^ 1) {
    int cx = pq.top().second.first;
    int cy = pq.top().second.second;
    int cost = pq.top().first * -1;

    pq.pop();
    if (cx < 0 || cx >= n || cy < 0 || cy >= m || grid[cx][cy] == '#' ||
        sp[cx][cy] != -1)
      continue;
    sp[cx][cy] = cost;
    if (cx == t_x && cy == t_y) {
      cout << cost;
      return 0;
    }
    for (int i = 0; i < 4; ++i)
      pq.push({-cost, {cx + X0[i], cy + Y0[i]}});

    for (int i = 0; i < 20; ++i)
      pq.push({-(cost + 1), {cx + X1[i], cy + Y1[i]}});
  };

  cout << sp[t_x][t_y];
}
Problem p02579 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e3 + 5;

int h, w;
int sx, sy, ex, ey;
int d[N][N];
string s[N];

int check(int x, int y) {
  if (x >= 0 && x < h && y >= 0 && y < w && s[x][y] == '.')
    return 1;
  return 0;
}
int bfs() {
  deque<pair<int, int>> q;
  q.push_back({sx, sy});
  d[sx][sy] = 0;
  while (!q.empty()) {
    int x = q.front().first;
    int y = q.front().second;
    q.pop_front();
    for (int i = -2; i <= 2; i++)
      for (int j = -2; j <= 2; j++) {
        if (i == 0 && j == 0)
          continue;
        if (abs(i) + abs(j) == 1) {
          int tx = x + i;
          int ty = y + j;
          if (check(tx, ty) && d[tx][ty] > d[x][y]) {
            d[tx][ty] = d[x][y];
            q.push_front({tx, ty});
          }
        } else {
          int tx = x + i;
          int ty = y + j;
          if (check(tx, ty) && d[tx][ty] > d[x][y] + 1) {
            d[tx][ty] = d[x][y] + 1;
            q.push_back({tx, ty});
          }
        }
      }
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin >> h >> w;
  cin >> sx >> sy >> ex >> ey;
  for (int i = 0; i < h; i++)
    cin >> s[i];
  sx--;
  sy--;
  ex--;
  ey--;
  memset(d, 0x3f, sizeof(d));
  bfs();
  if (d[ex][ey] == 0x3f3f3f3f)
    cout << -1 << endl;
  else
    cout << d[ex][ey] << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e3 + 5;

int h, w;
int sx, sy, ex, ey;
int d[N][N];
string s[N];

int check(int x, int y) {
  if (x >= 0 && x < h && y >= 0 && y < w && s[x][y] == '.')
    return 1;
  return 0;
}
void bfs() {
  deque<pair<int, int>> q;
  q.push_back({sx, sy});
  d[sx][sy] = 0;
  while (!q.empty()) {
    int x = q.front().first;
    int y = q.front().second;
    q.pop_front();
    for (int i = -2; i <= 2; i++)
      for (int j = -2; j <= 2; j++) {
        if (i == 0 && j == 0)
          continue;
        if (abs(i) + abs(j) == 1) {
          int tx = x + i;
          int ty = y + j;
          if (check(tx, ty) && d[tx][ty] > d[x][y]) {
            d[tx][ty] = d[x][y];
            q.push_front({tx, ty});
          }
        } else {
          int tx = x + i;
          int ty = y + j;
          if (check(tx, ty) && d[tx][ty] > d[x][y] + 1) {
            d[tx][ty] = d[x][y] + 1;
            q.push_back({tx, ty});
          }
        }
      }
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin >> h >> w;
  cin >> sx >> sy >> ex >> ey;
  for (int i = 0; i < h; i++)
    cin >> s[i];
  sx--;
  sy--;
  ex--;
  ey--;
  memset(d, 0x3f, sizeof(d));
  bfs();
  if (d[ex][ey] == 0x3f3f3f3f)
    cout << -1 << endl;
  else
    cout << d[ex][ey] << endl;
}
Problem p02579 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
using ull = uint64_t;
const ll INF = 9e18;

int main() {
  ll H, W;
  cin >> H >> W;
  ll sh, sw, gh, gw;
  cin >> sh >> sw;
  cin >> gh >> gw;
  sh--;
  sw--;
  gh--;
  gw--;

  // 道の一覧
  vector<vector<bool>> roads(H, vector<bool>(W, false));
  for (ll h = 0; h < H; h++) {
    for (ll w = 0; w < W; w++) {
      char c;
      cin >> c;
      while (c != '.' && c != '#') {
        cin >> c;
      }
      if (c == '.') {
        roads[h][w] = true;
      }
    }
  }

  vector<vector<bool>> done(H, vector<bool>(W, false));
  vector<stack<pair<ll, ll>>> diststack(1 + (H * W) / 4);
  ll d = 0;
  diststack[0].push(make_pair(sh, sw));
  while (d < (ll)diststack.size()) {
    if (diststack[d].empty()) {
      d++;
      continue;
    }
    auto src = diststack[d].top();
    diststack[d].pop();
    ll h = src.first;
    ll w = src.second;
    if (h == gh && w == gw) {
      cout << d << endl;
      return 0;
    }
    if (done[h][w]) {
      continue;
    }
    done[h][w] = true;
    for (ll y = h - 2; y <= h + 2; y++) {
      for (ll x = w - 2; x <= w + 2; x++) {
        if (y >= 0 && y < H && x >= 0 && x < W && roads[y][x] && !done[y][x]) {
          if (abs(x - w) + abs(y - h) == 1) {
            diststack[d].push(make_pair(y, x));
          } else {
            diststack[d + 1].push(make_pair(y, x));
          }
        }
      }
    }
  }
  cout << -1 << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
using ull = uint64_t;
const ll INF = 9e18;

int main() {
  ll H, W;
  cin >> H >> W;
  ll sh, sw, gh, gw;
  cin >> sh >> sw;
  cin >> gh >> gw;
  sh--;
  sw--;
  gh--;
  gw--;

  // 道の一覧
  vector<vector<bool>> roads(H, vector<bool>(W, false));
  for (ll h = 0; h < H; h++) {
    for (ll w = 0; w < W; w++) {
      char c;
      cin >> c;
      while (c != '.' && c != '#') {
        cin >> c;
      }
      if (c == '.') {
        roads[h][w] = true;
      }
    }
  }

  vector<vector<bool>> done(H, vector<bool>(W, false));
  vector<stack<pair<ll, ll>>> diststack(1 + (H * W) / 2);
  ll d = 0;
  diststack[0].push(make_pair(sh, sw));
  while (d < (ll)diststack.size()) {
    if (diststack[d].empty()) {
      d++;
      continue;
    }
    auto src = diststack[d].top();
    diststack[d].pop();
    ll h = src.first;
    ll w = src.second;
    if (h == gh && w == gw) {
      cout << d << endl;
      return 0;
    }
    if (done[h][w]) {
      continue;
    }
    done[h][w] = true;
    for (ll y = h - 2; y <= h + 2; y++) {
      for (ll x = w - 2; x <= w + 2; x++) {
        if (y >= 0 && y < H && x >= 0 && x < W && roads[y][x] && !done[y][x]) {
          if (abs(x - w) + abs(y - h) == 1) {
            diststack[d].push(make_pair(y, x));
          } else {
            diststack[d + 1].push(make_pair(y, x));
          }
        }
      }
    }
  }
  cout << -1 << endl;
}
Problem p02579 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <queue>
using namespace std;

int h, w, g[15][15], ans[15][15], vis[15][15], ch, cw, dh, dw;
string s;

int main() {
  cin >> h >> w;
  cin >> ch >> cw >> dh >> dw;
  for (int i = 1; i <= h; i++) {
    cin >> s;
    for (int j = 1; j <= w; j++) {
      if (s[j - 1] == '.')
        g[i][j] = 1;
    }
  }
  priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>,
                 greater<pair<int, pair<int, int>>>>
      pq;
  pq.push({0, {ch, cw}});
  while (!pq.empty()) {
    int d = pq.top().first, x = pq.top().second.first,
        y = pq.top().second.second;
    pq.pop();
    if (vis[x][y])
      continue;
    ans[x][y] = d;
    vis[x][y] = 1;
    for (int i = max(1, x - 2); i <= min(h, x + 2); i++) {
      for (int j = max(1, y - 2); j <= min(w, y + 2); j++) {
        if (!g[i][j] || vis[i][j])
          continue;
        if (abs(i - x) + abs(j - y) == 1) {
          ans[i][j] = ans[x][y];
          pq.push({ans[i][j], {i, j}});
        } else {
          ans[i][j] = ans[x][y] + 1;
          pq.push({ans[i][j], {i, j}});
        }
      }
    }
  }
  if (!vis[dh][dw])
    cout << -1 << "\n";
  else
    cout << ans[dh][dw] << "\n";
}


Fixed submission:
#include <iostream>
#include <queue>
using namespace std;

int h, w, g[1005][1005], ans[1005][1005], vis[1005][1005], ch, cw, dh, dw;
string s;

int main() {
  cin >> h >> w;
  cin >> ch >> cw >> dh >> dw;
  for (int i = 1; i <= h; i++) {
    cin >> s;
    for (int j = 1; j <= w; j++) {
      if (s[j - 1] == '.')
        g[i][j] = 1;
    }
  }
  priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>,
                 greater<pair<int, pair<int, int>>>>
      pq;
  pq.push({0, {ch, cw}});
  while (!pq.empty()) {
    int d = pq.top().first, x = pq.top().second.first,
        y = pq.top().second.second;
    pq.pop();
    if (vis[x][y])
      continue;
    ans[x][y] = d;
    vis[x][y] = 1;
    for (int i = max(1, x - 2); i <= min(h, x + 2); i++) {
      for (int j = max(1, y - 2); j <= min(w, y + 2); j++) {
        if (!g[i][j] || vis[i][j])
          continue;
        if (abs(i - x) + abs(j - y) == 1) {
          ans[i][j] = ans[x][y];
          pq.push({ans[i][j], {i, j}});
        } else {
          ans[i][j] = ans[x][y] + 1;
          pq.push({ans[i][j], {i, j}});
        }
      }
    }
  }
  if (!vis[dh][dw])
    cout << -1 << "\n";
  else
    cout << ans[dh][dw] << "\n";
}
Problem p02579 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 7;
int mat[N][N];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);

  int h, w, sI, sJ, eI, eJ;
  cin >> h >> w >> sI >> sJ >> eI >> eJ;
  sI--;
  sJ--;
  eI--;
  eJ--;
  for (int i = 0; i < h; i++) {
    string s;
    cin >> s;
    for (int j = 0; j < w; j++) {
      if (s[j] == '#')
        mat[i][j] = 1;
      else
        mat[i][j] = 0;
    }
  }
  deque<pair<int, int>> q;
  vector<vector<int>> d(h, vector<int>(w, 1e9 + 7));
  q.push_front({sI, sJ});
  d[sI][sJ] = 0;
  int dir[] = {1, 0, -1, 0, 1};
  while (!q.empty()) {
    int cI, cJ;
    tie(cI, cJ) = q.front();
    q.pop_front();
    for (int i = 0; i < 4; i++) {
      int nI = cI + dir[i];
      int nJ = cJ + dir[i + 1];
      if (nI >= h || nI < 0 || nJ >= w || nJ < 0)
        continue;
      if (mat[nI][nJ])
        continue;
      if (d[nI][nJ] > d[cI][cJ]) {
        d[nI][nJ] = d[cI][cJ];
        q.push_front({nI, nJ});
      }
    }
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        int nI = cI + i;
        int nJ = cJ + j;
        if (nI >= h || nI < 0 || nJ >= w || nJ < 0)
          continue;
        if (mat[nI][nJ])
          continue;
        if (d[nI][nJ] > d[cI][cJ] + 1) {
          d[nI][nJ] = d[cI][cJ] + 1;
          q.push_front({nI, nJ});
        }
      }
    }
  }
  if (d[eI][eJ] == 1e9 + 7)
    cout << -1 << endl;
  else
    cout << d[eI][eJ] << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 7;
int mat[N][N];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);

  int h, w, sI, sJ, eI, eJ;
  cin >> h >> w >> sI >> sJ >> eI >> eJ;
  sI--;
  sJ--;
  eI--;
  eJ--;
  for (int i = 0; i < h; i++) {
    string s;
    cin >> s;
    for (int j = 0; j < w; j++) {
      if (s[j] == '#')
        mat[i][j] = 1;
      else
        mat[i][j] = 0;
    }
  }
  deque<pair<int, int>> q;
  vector<vector<int>> d(h, vector<int>(w, 1e9 + 7));
  q.push_front({sI, sJ});
  d[sI][sJ] = 0;
  int dir[] = {1, 0, -1, 0, 1};
  while (!q.empty()) {
    int cI, cJ;
    tie(cI, cJ) = q.front();
    q.pop_front();
    for (int i = 0; i < 4; i++) {
      int nI = cI + dir[i];
      int nJ = cJ + dir[i + 1];
      if (nI >= h || nI < 0 || nJ >= w || nJ < 0)
        continue;
      if (mat[nI][nJ])
        continue;
      if (d[nI][nJ] > d[cI][cJ]) {
        d[nI][nJ] = d[cI][cJ];
        q.push_front({nI, nJ});
      }
    }
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        int nI = cI + i;
        int nJ = cJ + j;
        if (nI >= h || nI < 0 || nJ >= w || nJ < 0)
          continue;
        if (mat[nI][nJ])
          continue;
        if (d[nI][nJ] > d[cI][cJ] + 1) {
          d[nI][nJ] = d[cI][cJ] + 1;
          q.push_back({nI, nJ});
        }
      }
    }
  }
  if (d[eI][eJ] == 1e9 + 7)
    cout << -1 << endl;
  else
    cout << d[eI][eJ] << endl;

  return 0;
}
Problem p02580 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

#define rep(i, a) for (int i = 0; i < (a); i++)

typedef pair<int, int> P;
typedef long long ll;

const int INF = 1001001001;
const int MOD = 1000000007;
const double PI = acos(-1);

int main() {
  int H, W, M;
  cin >> H >> W >> M;
  set<pair<int, int>> S;

  vector<vector<int>> Aw(W, vector<int>(2, 0));
  vector<vector<int>> Ah(H, vector<int>(2, 0));
  rep(i, W) { Aw[i][1] = i; }
  rep(i, H) { Ah[i][1] = i; }

  rep(i, M) {
    int h, w;
    cin >> h >> w;
    h--, w--;
    S.insert(make_pair(h, w));
    Ah[h][0]++;
    Aw[w][0]++;
  }
  sort(Aw.begin(), Aw.end());
  sort(Ah.begin(), Ah.end());
  ll ans = 0;

  ll wnum = Aw.back()[0];
  ll wint = Aw.back()[1];
  vector<int> wmax;
  int idx = W - 1;
  while (Aw[idx][0] == wnum) {
    wmax.push_back(Aw[idx][1]);
    idx--;
    if (idx < 0)
      break;
  }

  ll hnum = Ah.back()[0];
  ll hint = Ah.back()[1];
  vector<int> hmax;
  idx = H - 1;
  while (Ah[idx][0] == hnum) {
    hmax.push_back(Ah[idx][1]);
    idx--;
    if (idx < 0)
      break;
  }

  int cnt = 0;
  rep(h, hmax.size()) {
    rep(w, wmax.size()) {
      ll tmp = hnum + wnum;
      // cout << hmax[h]+1 << " " << wmax[w]+1 << endl;
      if (S.count(make_pair(hmax[h], wmax[w]))) {
        tmp--;
      }
      ans = max(ans, tmp);
    }
    if (cnt > 1000000)
      break;
    if (cnt > 1000000)
      break;
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define rep(i, a) for (int i = 0; i < (a); i++)

typedef pair<int, int> P;
typedef long long ll;

const int INF = 1001001001;
const int MOD = 1000000007;
const double PI = acos(-1);

int main() {
  int H, W, M;
  cin >> H >> W >> M;
  set<pair<int, int>> S;

  vector<vector<int>> Aw(W, vector<int>(2, 0));
  vector<vector<int>> Ah(H, vector<int>(2, 0));
  rep(i, W) { Aw[i][1] = i; }
  rep(i, H) { Ah[i][1] = i; }

  rep(i, M) {
    int h, w;
    cin >> h >> w;
    h--, w--;
    S.insert(make_pair(h, w));
    Ah[h][0]++;
    Aw[w][0]++;
  }
  sort(Aw.begin(), Aw.end());
  sort(Ah.begin(), Ah.end());
  ll ans = 0;

  ll wnum = Aw.back()[0];
  ll wint = Aw.back()[1];
  vector<int> wmax;
  int idx = W - 1;
  while (Aw[idx][0] == wnum) {
    wmax.push_back(Aw[idx][1]);
    idx--;
    if (idx < 0)
      break;
  }

  ll hnum = Ah.back()[0];
  ll hint = Ah.back()[1];
  vector<int> hmax;
  idx = H - 1;
  while (Ah[idx][0] == hnum) {
    hmax.push_back(Ah[idx][1]);
    idx--;
    if (idx < 0)
      break;
  }

  int cnt = 0;
  rep(h, hmax.size()) {
    rep(w, wmax.size()) {
      ll tmp = hnum + wnum;
      // cout << hmax[h]+1 << " " << wmax[w]+1 << endl;
      if (S.count(make_pair(hmax[h], wmax[w]))) {
        tmp--;
      }
      ans = max(ans, tmp);
      cnt++;
    }
    if (cnt > 1000000)
      break;
    if (cnt > 1000000)
      break;
  }
  cout << ans << endl;
}
Problem p02580 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main() {
  ll h, w, m;
  cin >> h >> w >> m;
  ll a[h][w];
  for (ll i = 0; i < h; i++)
    for (ll j = 0; j < w; j++) {
      a[i][j] = 0;
    }
  set<pair<ll, ll>> s;
  vector<ll> mpr(300001, 0);
  vector<ll> mpc(300001, 0);
  while (m--) {
    ll x, y;
    cin >> x >> y;
    x--;
    y--;
    s.insert({x, y});
    mpr[x]++;
    mpc[y]++;
  }
  ll mar = INT_MIN;
  ll r;
  for (ll i = 0; i < h; i++) {
    if (mpr[i] > mar) {
      mar = mpr[i];
      r = i;
    }
  }
  ll mac = INT_MIN;
  ll c;
  for (ll i = 0; i < w; i++) {
    if (mpc[i] > mac) {
      mac = mpc[i];
      c = i;
    }
  }
  ll cnt = INT_MIN;
  for (ll i = 0; i < h; i++) {
    if (s.find({i, c}) != s.end()) {
      cnt = max(cnt, mpr[i] + mac - 1);
    } else {
      cnt = max(cnt, mpr[i] + mac);
    }
  }
  for (ll i = 0; i < w; i++) {
    if (s.find({r, i}) != s.end()) {
      cnt = max(cnt, mpc[i] + mar - 1);
    } else {
      cnt = max(cnt, mpc[i] + mar);
    }
  }
  cout << cnt << endl;
  // cout<<ma<<endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main() {
  ll h, w, m;
  cin >> h >> w >> m;

  set<pair<ll, ll>> s;
  vector<ll> mpr(300001, 0);
  vector<ll> mpc(300001, 0);
  while (m--) {
    ll x, y;
    cin >> x >> y;
    x--;
    y--;
    s.insert({x, y});
    mpr[x]++;
    mpc[y]++;
  }
  ll mar = INT_MIN;
  ll r;
  for (ll i = 0; i < h; i++) {
    if (mpr[i] > mar) {
      mar = mpr[i];
      r = i;
    }
  }
  ll mac = INT_MIN;
  ll c;
  for (ll i = 0; i < w; i++) {
    if (mpc[i] > mac) {
      mac = mpc[i];
      c = i;
    }
  }
  ll cnt = INT_MIN;
  for (ll i = 0; i < h; i++) {
    if (s.find({i, c}) != s.end()) {
      cnt = max(cnt, mpr[i] + mac - 1);
    } else {
      cnt = max(cnt, mpr[i] + mac);
    }
  }
  for (ll i = 0; i < w; i++) {
    if (s.find({r, i}) != s.end()) {
      cnt = max(cnt, mpc[i] + mar - 1);
    } else {
      cnt = max(cnt, mpc[i] + mar);
    }
  }
  cout << cnt << endl;
  // cout<<ma<<endl;
}
Problem p02580 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define forr(i, a, n) for (long long int i = a; i < n; i++)
#define loop(i, a, n) for (long long int i = a; i >= n; i--)

int main() {
  ios_base ::sync_with_stdio(false), cin.tie(NULL);

  int a, b, c, ans = -1;
  cin >> a >> b >> c;
  int x[c], y[c], m2x = -1, m1x = -1;
  map<int, int> m2, m1;
  map<pair<int, int>, int> m;
  forr(i, 0, c) {
    cin >> x[i] >> y[i];
    m[{x[i], y[i]}]++;
    m1[x[i]]++;
    m2[y[i]]++;
    m2x = max(m2x, m2[y[i]]);
    m1x = max(m1x, m1[x[i]]);
  }
  vector<int> v, w;
  for (auto const x : m1) {
    if (x.second == m1x)
      v.push_back(x.first);
  }
  for (auto const x : m2) {
    if (x.second == m2x)
      w.push_back(x.first);
  }
  forr(i, 0, v.size()) {
    forr(j, 0, w.size()) {
      int xx = m1[v[i]] + m2[w[j]] - m[{v[i], w[j]}];
      ans = max(ans, xx);
    }
  }
  cout << ans << '\n';
  // cout<<m1[5]<<' '<<m2[3]<<' '<<m[{5, 3}]<<' '<<m[{3, 5}];

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define forr(i, a, n) for (long long int i = a; i < n; i++)
#define loop(i, a, n) for (long long int i = a; i >= n; i--)

int main() {
  ios_base ::sync_with_stdio(false), cin.tie(NULL);

  int a, b, c, ans = -1;
  cin >> a >> b >> c;
  int x[c], y[c], m2x = -1, m1x = -1;
  map<int, int> m2, m1;
  map<pair<int, int>, int> m;
  forr(i, 0, c) {
    cin >> x[i] >> y[i];
    m[{x[i], y[i]}]++;
    m1[x[i]]++;
    m2[y[i]]++;
    m2x = max(m2x, m2[y[i]]);
    m1x = max(m1x, m1[x[i]]);
  }
  vector<int> v, w;
  for (auto const x : m1) {
    if (x.second == m1x)
      v.push_back(x.first);
  }
  for (auto const x : m2) {
    if (x.second == m2x)
      w.push_back(x.first);
  }
  forr(i, 0, v.size()) {
    forr(j, 0, w.size()) {
      int xx = m1[v[i]] + m2[w[j]] - m[{v[i], w[j]}];
      ans = max(ans, xx);
      if (m[{v[i], w[j]}] == 0)
        break;
    }
  }
  cout << ans << '\n';
  // cout<<m1[5]<<' '<<m2[3]<<' '<<m[{5, 3}]<<' '<<m[{3, 5}];

  return 0;
}
Problem p02580 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define mp make_pair
ll bg = 100000000007;
#define ff(i, n) for (i = 0; i < n; i++)
ll row[300001];
int main() {
  ll kamal;
  kamal = 1;
  while (kamal--) {
    ll i, j, h, w, m;
    cin >> h >> w >> m;
    vector<set<ll>> adj(w);
    ff(i, m) {
      ll a, b;
      cin >> a >> b;
      row[a - 1]++;
      adj[b - 1].insert(a - 1);
    }
    vector<pair<ll, ll>> vec;
    ff(i, h) vec.pb(mp(row[i], i));
    sort(vec.begin(), vec.end());
    ll ans = 0;
    ff(i, m) {
      ll e = adj[i].size(), add = 0;
      for (j = vec.size() - 1; j >= 0; j--) {
        if (adj[i].find(vec[j].second) != adj[i].end()) {
          add = max(add, vec[j].first - 1);
        } else {
          add = max(add, vec[j].first);
          break;
        }
      }
      ans = max(ans, e + add);
    }
    cout << ans << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define mp make_pair
ll bg = 100000000007;
#define ff(i, n) for (i = 0; i < n; i++)
ll row[300001];
int main() {
  ll kamal;
  kamal = 1;
  while (kamal--) {
    ll i, j, h, w, m;
    cin >> h >> w >> m;
    vector<set<ll>> adj(w);
    ff(i, m) {
      ll a, b;
      cin >> a >> b;
      row[a - 1]++;
      adj[b - 1].insert(a - 1);
    }
    vector<pair<ll, ll>> vec;
    ff(i, h) vec.pb(mp(row[i], i));
    sort(vec.begin(), vec.end());
    ll ans = 0;
    ff(i, w) {
      ll e = adj[i].size(), add = 0;
      for (j = vec.size() - 1; j >= 0; j--) {
        if (adj[i].find(vec[j].second) != adj[i].end()) {
          add = max(add, vec[j].first - 1);
        } else {
          add = max(add, vec[j].first);
          break;
        }
      }
      ans = max(ans, e + add);
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p02580 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

#define rng(i, a, b) for (int i = int(a); i < int(b); i++)
#define rep(i, b) rng(i, 0, b)
#define gnr(i, a, b) for (int i = int(b) - 1; i >= int(a); i--)
#define per(i, b) gnr(i, 0, b)

using namespace std;

using ll = long long;
using P = pair<int, int>;

int main() {
  int h, w, m;
  cin >> h >> w >> m;
  vector<P> hw(m);
  vector<int> hh(h, 0);
  vector<int> ww(w, 0);
  set<P> s;
  rep(i, m) {
    int hhh, www;
    cin >> hhh >> www;
    hhh--;
    www--;
    hh.at(hhh)++;
    ww.at(www)++;
    hw.at(i) = P(hhh, www);
    s.emplace(hhh, www);
  }
  int hMax = -1;
  vector<int> hi;
  int wMax = -1;
  vector<int> wi;
  rep(i, h) {
    if (hh.at(i) > hMax) {
      hMax = hh.at(i);
      hi.clear();
      hi.push_back(i);
    } else if (hh.at(i) == hMax) {
      hi.push_back(i);
    }
  }
  rep(i, w) {
    if (ww.at(i) > wMax) {
      wMax = ww.at(i);
      wi.clear();
      wi.push_back(i);
    } else if (ww.at(i) == wMax) {
      wi.push_back(i);
    }
  }
  int res = hMax + wMax;
  bool cross = true; // 交点に爆破対象
  for (int i : hi) {
    for (int j : wi) {
      if (s.count(P(i, j)) > 0) {
        continue;
      }
      // 爆破対象がない交点があった
      cross = false;
    }
  }
  if (cross) {
    res--;
  }
  cout << res << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

#define rng(i, a, b) for (int i = int(a); i < int(b); i++)
#define rep(i, b) rng(i, 0, b)
#define gnr(i, a, b) for (int i = int(b) - 1; i >= int(a); i--)
#define per(i, b) gnr(i, 0, b)

using namespace std;

using ll = long long;
using P = pair<int, int>;

int main() {
  int h, w, m;
  cin >> h >> w >> m;
  vector<P> hw(m);
  vector<int> hh(h, 0);
  vector<int> ww(w, 0);
  set<P> s;
  rep(i, m) {
    int hhh, www;
    cin >> hhh >> www;
    hhh--;
    www--;
    hh.at(hhh)++;
    ww.at(www)++;
    hw.at(i) = P(hhh, www);
    s.emplace(hhh, www);
  }
  int hMax = -1;
  vector<int> hi;
  int wMax = -1;
  vector<int> wi;
  rep(i, h) {
    if (hh.at(i) > hMax) {
      hMax = hh.at(i);
      hi.clear();
      hi.push_back(i);
    } else if (hh.at(i) == hMax) {
      hi.push_back(i);
    }
  }
  rep(i, w) {
    if (ww.at(i) > wMax) {
      wMax = ww.at(i);
      wi.clear();
      wi.push_back(i);
    } else if (ww.at(i) == wMax) {
      wi.push_back(i);
    }
  }
  int res = hMax + wMax;
  bool cross = true; // 交点に爆破対象
  for (int i : hi) {
    for (int j : wi) {
      if (s.count(P(i, j)) > 0) {
        continue;
      }
      // 爆破対象がない交点があった
      cross = false;
      break;
    }
    if (!cross) {
      break;
    }
  }
  if (cross) {
    res--;
  }
  cout << res << endl;
  return 0;
}
Problem p02580 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define pb push_back
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define FOR(i, a, b) for (int i = a; i <= (b); i++)
#define ROF(i, a, b) for (int i = a; i >= (b); i--)

using pii = pair<int, int>;
using vpii = vector<pii>;
using vi = vector<int>;
using vvi = vector<vi>;
using ll = long long;
using pll = pair<ll, ll>;
using vpll = vector<pll>;
using vll = vector<ll>;
using vvll = vector<vll>;

int main() {
  int h, w, m;
  cin >> h >> w >> m;
  vector<map<int, bool>> cy(h + 1);
  vi sx(h + 1), sy(w + 1);
  FOR(i, 1, m) {
    int x, y;
    cin >> x >> y;
    cy[x][y] = 1, sx[x]++, sy[y]++;
  }
  priority_queue<pii> pq;
  FOR(i, 1, w)
  pq.push({sy[i], i});
  int ans = 0;
  FOR(i, 1, h) {
    if (sx[i]) {
      vpii rm;
      int tmx = 0;
      while (cy[i][pq.top().se]) {
        tmx = max(tmx, pq.top().fi - 1);
        rm.pb(pq.top());
        pq.pop();
      }
      if (!pq.empty())
        tmx = max(tmx, pq.top().fi);
      ans = max(ans, sx[i] + tmx);
      for (pii j : rm)
        pq.push(j);
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define pb push_back
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define FOR(i, a, b) for (int i = a; i <= (b); i++)
#define ROF(i, a, b) for (int i = a; i >= (b); i--)

using pii = pair<int, int>;
using vpii = vector<pii>;
using vi = vector<int>;
using vvi = vector<vi>;
using ll = long long;
using pll = pair<ll, ll>;
using vpll = vector<pll>;
using vll = vector<ll>;
using vvll = vector<vll>;

int main() {
  int h, w, m;
  cin >> h >> w >> m;
  vector<map<int, bool>> cy(h + 1);
  vi sx(h + 1), sy(w + 1);
  FOR(i, 1, m) {
    int x, y;
    cin >> x >> y;
    cy[x][y] = 1, sx[x]++, sy[y]++;
  }
  priority_queue<pii> pq;
  FOR(i, 1, w)
  pq.push({sy[i], i});
  int ans = 0;
  FOR(i, 1, h) {
    if (sx[i]) {
      vpii rm;
      int tmx = 0;
      while (!pq.empty() && cy[i][pq.top().se]) {
        tmx = max(tmx, pq.top().fi - 1);
        rm.pb(pq.top());
        pq.pop();
      }
      if (!pq.empty())
        tmx = max(tmx, pq.top().fi);
      ans = max(ans, sx[i] + tmx);
      for (pii j : rm)
        pq.push(j);
    }
  }
  cout << ans << endl;
}