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

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

// typedef
typedef long long LL;
typedef vector<LL> VL;
typedef vector<VL> VVL;
typedef vector<string> VS;
typedef pair<LL, LL> PLL;

// conversion
inline LL toLong(string s) {
  LL v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

// math
//-------------------------------------------
template <class T> inline T sqr(T x) { return x * x; }

// container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) LL((a).size())
#define EACH(i, c) for (auto i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

// repetition
#define FOR(i, a, b) for (LL i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define UNTIL(p) while (!(p))

// constant
const double EPS = 1e-5;
const double PI = acos(-1.0);

// debug
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl;

#define PUTS(x) cout << (x) << endl;

LL used[200005] = {0};
int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  LL n, m;
  cin >> n >> m;
  LL x = 1;
  LL y = n;
  REP(i, m) {
    while (true) {
      LL a = y - x;
      LL b = n - a;
      if (a != b && (used[a] == 0 && used[b] == 0)) {
        used[a]++;
        used[b]++;
        break;
      }
      y--;
      if (x >= y) {
        // 通らないはず
        PUTS("????");
        break;
      }
    }
    cout << x << " " << y << endl;
    x++;
    y--;
  }
}
/*
n = 10
1 9 | 8 2
2 8 | 6 4
3 6 | 3 7
4 5 | 1 9

1 10 | 9 1
2 9  | 7 3
3 7  | 4 6
4 6  | 2 8
*/

/*
1 6 | 5 2
2 5 | 3 4
3 4 | 1 6

1 7 | 6 1
2 6 | 4 3
3 5 | 2 5

r1
a b c d e f g
1 2 3 4 5 6 7
a f
b e
c d

r2
a b c d e f g
2 3 4 5 6 7 1
e g
a d
b c

r3
a b c d e f g
3 4 5 6 7 1 2
d f
c g
a b

r4
a b c d e f g
4 5 6 7 1 2 3
c e
b f
a g

r5
a b c d e f g
5 6 7 1 2 3 4
d b
a e
f g

r6
a b c d e f g
6 7 1 2 3 4 5
a c
d g
e f

r7
a b c d e f g
7 1 2 3 4 5 6
b g
c f
d e
*/


Error: WA

Fixed submission:

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

// typedef
typedef long long LL;
typedef vector<LL> VL;
typedef vector<VL> VVL;
typedef vector<string> VS;
typedef pair<LL, LL> PLL;

// conversion
inline LL toLong(string s) {
  LL v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

// math
//-------------------------------------------
template <class T> inline T sqr(T x) { return x * x; }

// container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) LL((a).size())
#define EACH(i, c) for (auto i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

// repetition
#define FOR(i, a, b) for (LL i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define UNTIL(p) while (!(p))

// constant
const double EPS = 1e-5;
const double PI = acos(-1.0);

// debug
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl;

#define PUTS(x) cout << (x) << endl;

LL used[100005] = {0};
int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  LL n, m;
  cin >> n >> m;
  LL x = 1;
  LL y = n;
  REP(i, m) {
    while (true) {
      LL a = y - x;
      LL b = n - a;
      if (used[a] == 0 && used[b] == 0) {
        used[a]++;
        used[b]++;
        break;
      }
      y--;
      if (x >= y) {
        // 通らないはず
        PUTS("????");
        break;
      }
    }
    cout << x << " " << y << endl;
    x++;
    y--;
  }
}
/*
n = 10
1 9 | 8 2
2 8 | 6 4
3 6 | 3 7
4 5 | 1 9

1 10 | 9 1
2 9  | 7 3
3 7  | 4 6
4 6  | 2 8
*/

/*
1 6 | 5 2
2 5 | 3 4
3 4 | 1 6

1 7 | 6 1
2 6 | 4 3
3 5 | 2 5

r1
a b c d e f g
1 2 3 4 5 6 7
a f
b e
c d

r2
a b c d e f g
2 3 4 5 6 7 1
e g
a d
b c

r3
a b c d e f g
3 4 5 6 7 1 2
d f
c g
a b

r4
a b c d e f g
4 5 6 7 1 2 3
c e
b f
a g

r5
a b c d e f g
5 6 7 1 2 3 4
d b
a e
f g

r6
a b c d e f g
6 7 1 2 3 4 5
a c
d g
e f

r7
a b c d e f g
7 1 2 3 4 5 6
b g
c f
d e
*/