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

#include <algorithm>
#include <array>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

#define Rep(i, a, b) for (ll i = a; i < b; ++i)
#define rep(i, b) Rep(i, 0, b)
#define allof(a) (a).begin(), (a).end()

#define Yes(q) ((q) ? "Yes" : "No")
#define YES(q) ((q) ? "YES" : "NO")
#define Possible(q) ((q) ? "Possible" : "Impossible")
#define POSSIBLE(q) ((q) ? "POSSIBLE" : "IMPOSSIBLE")

using ll = long long;

constexpr int inf = 1e9 + 7;
constexpr ll infll = 1ll << 60ll;
constexpr ll mod = 1e9 + 7;
// 0~3までは右左下上 4~7までは斜め
constexpr int dx[] = {1, 0, -1, 0, 1, 1, -1, -1};
constexpr int dy[] = {0, -1, 0, 1, 1, -1, -1, 1};

namespace {
template <typename T> void chmax(T &a, T b) { a = std::max(a, b); }
template <typename T> void chmin(T &a, T b) { a = std::min(a, b); }
template <typename T> void chadd(T &a, T b) { a = a + b; }

void Cout(long long x, const char *end = "\\n") { std::cout << x << end; }
template <typename T> void Cout(const T &x, const char *end = "\\n") {
  std::cout << x << end;
}
template <typename T>
void Cout(const std::vector<T> &x, const char *sep = " ",
          const char *end = "\\n") {
  for (std::size_t i = 0, sz = x.size(); i < sz; i++) {
    std::cout << x[i] << (i == sz - 1 ? end : sep);
  }
}

// 標準入出力
struct inp {
  std::size_t sz;
  inp(std::size_t _sz = 1) : sz(_sz) {}
  template <typename T> operator T() const {
    T a;
    std::cin >> a;
    return a;
  }
  template <typename T> operator std::vector<T>() const {
    std::vector<T> a(sz);
    for (std::size_t i = 0; i < sz; i++)
      std::cin >> a[i];
    return a;
  }
};

// 2次元用の標準入出力
template <typename T> struct inpn {
  std::size_t szi, szj;
  inpn(std::size_t _szi, std::size_t _szj) : szi(_szi), szj(_szj) {}
  operator std::vector<std::vector<T>>() const {
    std::vector<std::vector<T>> a(szi, std::vector<T>(szj));
    for (std::size_t i = 0; i < szi; ++i)
      for (std::size_t j = 0; j < szj; ++j)
        cin >> a[i][j];
    return a;
  }
};

inp inp1; // input one
} // namespace

int main() {

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

  chmin(n, 2 * m + 1);

  // 距離の重複をなくす
  // 輪なので2通りの距離があるが
  // 輪の半分だけで組み合わせを作ると一つの距離のみを考慮すればよい

  int ls = 1;
  int le = n / 2;
  int rs = n / 2 + 1;
  int re = n;

  if (le - ls == re - rs) {
    --le;
  }

  vector<pair<int, int>> ans;

  while (le - ls >= 1) {
    ans.emplace_back(ls++, le--);
  }
  while (re - rs >= 1) {
    ans.emplace_back(rs++, re--);
  }

  ans.resize(m);
  for (auto [x, y] : ans) {
    cout << x << " " << y << endl;
  }

  return 0;
}


Error: WA

Fixed submission:

#include <algorithm>
#include <array>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

#define Rep(i, a, b) for (ll i = a; i < b; ++i)
#define rep(i, b) Rep(i, 0, b)
#define allof(a) (a).begin(), (a).end()

#define Yes(q) ((q) ? "Yes" : "No")
#define YES(q) ((q) ? "YES" : "NO")
#define Possible(q) ((q) ? "Possible" : "Impossible")
#define POSSIBLE(q) ((q) ? "POSSIBLE" : "IMPOSSIBLE")

using ll = long long;

constexpr int inf = 1e9 + 7;
constexpr ll infll = 1ll << 60ll;
constexpr ll mod = 1e9 + 7;
// 0~3までは右左下上 4~7までは斜め
constexpr int dx[] = {1, 0, -1, 0, 1, 1, -1, -1};
constexpr int dy[] = {0, -1, 0, 1, 1, -1, -1, 1};

namespace {
template <typename T> void chmax(T &a, T b) { a = std::max(a, b); }
template <typename T> void chmin(T &a, T b) { a = std::min(a, b); }
template <typename T> void chadd(T &a, T b) { a = a + b; }

void Cout(long long x, const char *end = "\\n") { std::cout << x << end; }
template <typename T> void Cout(const T &x, const char *end = "\\n") {
  std::cout << x << end;
}
template <typename T>
void Cout(const std::vector<T> &x, const char *sep = " ",
          const char *end = "\\n") {
  for (std::size_t i = 0, sz = x.size(); i < sz; i++) {
    std::cout << x[i] << (i == sz - 1 ? end : sep);
  }
}

// 標準入出力
struct inp {
  std::size_t sz;
  inp(std::size_t _sz = 1) : sz(_sz) {}
  template <typename T> operator T() const {
    T a;
    std::cin >> a;
    return a;
  }
  template <typename T> operator std::vector<T>() const {
    std::vector<T> a(sz);
    for (std::size_t i = 0; i < sz; i++)
      std::cin >> a[i];
    return a;
  }
};

// 2次元用の標準入出力
template <typename T> struct inpn {
  std::size_t szi, szj;
  inpn(std::size_t _szi, std::size_t _szj) : szi(_szi), szj(_szj) {}
  operator std::vector<std::vector<T>>() const {
    std::vector<std::vector<T>> a(szi, std::vector<T>(szj));
    for (std::size_t i = 0; i < szi; ++i)
      for (std::size_t j = 0; j < szj; ++j)
        cin >> a[i][j];
    return a;
  }
};

inp inp1; // input one
} // namespace

int main() {

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

  // 全てのラウンドの差が異なるようにする
  int left = n / 2;
  int right = n / 2 + 1;

  vector<bool> used(n, false);
  rep(i, m) {
    int dif = right - left;
    while (used[dif]) {
      right = (right + 1) % (n + 1);
      dif = right - left;
    }
    dif = n - right + left;
    while (used[dif]) {
      right = (right + 1) % (n + 1);
      dif = right - left;
    }
    used[dif] = true;
    used[right - left] = true;
    if (left < right) {
      int tmp = left;
      left = right;
      right = tmp;
    }
    cout << left << " " << right << endl;
    --left;
    right = (right + 1) % (n + 1);
  }

  return 0;
}