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

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

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPr(i, n) for (int i = (n)-1; i >= 0; --i)
#define FORq(i, m, n) for (int i = (m); i <= (n); ++i)
#define FORqr(i, m, n) for (int i = (n); i >= (m); --i)
#define MP make_pair
#define SIN(x, S) (S.count(x) != 0)
#define M0(x) memset(x, 0, sizeof(x))
#define FILL(x, y) memset(x, y, sizeof(x))
#define MM(x) memset(x, -1, sizeof(x))
#define ALL(x) (x).begin(), (x).end()
#define DB(x) cerr << #x << " = " << x << endl
#define DB2(x, y)                                                              \\
  cerr << "(" << #x << ", " << #y << ") = (" << x << ", " << y << ")\\n";
#define DEBUG                                                                  \\
  int x12345;                                                                  \\
  cin >> x12345;
typedef pair<int, int> PII;
typedef pair<long long, long long> PLL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<long long> VL;
typedef long long ll;
typedef long long integer;
///////////////////////////////////////////////
const long long MOD = 1e9 + 7;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

///////////////////////////////////////////////
/// 🍈( '-' 🍈 |AC|
int main() {
  int N, M;
  cin >> N >> M;
  if (M == 1) {
    cout << "1 2" << endl;
    return 0;
  }

  if (N % 2 == 0)
    N--;
  vector<PII> ans;

  int N2 = N / 2;
  int N1 = N - N2;

  for (int i = 1; i <= N1 / 2; i++) {
    int j = N1 + 1 - i;
    ans.push_back(MP(i, j));
  }

  for (int i = 1; i <= N2 / 2; i++) {
    int j = N2 + 1 - i;
    ans.push_back(MP(N1 + i, N1 + j));
  }

  REP(i, M) { cout << ans[i].first << " " << ans[i].second << "\\n"; }
}

Error: 0

Fixed submission:

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

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPr(i, n) for (int i = (n)-1; i >= 0; --i)
#define FORq(i, m, n) for (int i = (m); i <= (n); ++i)
#define FORqr(i, m, n) for (int i = (n); i >= (m); --i)
#define MP make_pair
#define SIN(x, S) (S.count(x) != 0)
#define M0(x) memset(x, 0, sizeof(x))
#define FILL(x, y) memset(x, y, sizeof(x))
#define MM(x) memset(x, -1, sizeof(x))
#define ALL(x) (x).begin(), (x).end()
#define DB(x) cerr << #x << " = " << x << endl
#define DB2(x, y)                                                              \\
  cerr << "(" << #x << ", " << #y << ") = (" << x << ", " << y << ")\\n";
#define DEBUG                                                                  \\
  int x12345;                                                                  \\
  cin >> x12345;
typedef pair<int, int> PII;
typedef pair<long long, long long> PLL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<long long> VL;
typedef long long ll;
typedef long long integer;
///////////////////////////////////////////////
const long long MOD = 1e9 + 7;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

///////////////////////////////////////////////
/// 🍈( '-' 🍈 |AC|
int nextn(int x, int n) {
  if (x == 1)
    return n;
  else
    return x - 1;
}

vector<PII> solve(int n) {
  vector<PII> ret;
  if (n == 0)
    return ret;
  vector<bool> a(100010);

  FORq(d, 1, n / 2) {
    int x = 1;
    while (1) {
      if (a[x] or a[x + d]) {
        x++;
      } else {
        a[x] = true;
        a[x + d] = true;
        break;
      }
    }
    ret.push_back(MP(x, x + d));
    // DB2(x,x+d);
  }

  return ret;
}

int main() {
  int N, M;
  cin >> N >> M;
  int N1 = 0;
  int N2 = 0;
  if (N % 2 == 0) {
    N1 = N / 2 - 1;
    N2 = N / 2 + 1;
  } else {
    N1 = N;
    N2 = 0;
  }
  vector<PII> ans1, ans2;
  ans1 = solve(N1);
  ans2 = solve(N2);
  int count = 0;

  REP(i, ans1.size()) {
    if (count >= M)
      break;
    cout << ans1[i].first << " " << ans1[i].second << "\\n";
    count++;
  }

  REP(i, ans2.size()) {
    if (count >= M)
      break;
    cout << ans2[i].first + N1 << " " << ans2[i].second + N1 << "\\n";
    count++;
  }
}