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

#include <bits/stdc++.h>
#define rep(i, a, b) for (auto i = (a); i < (b); i++)
#define per(i, a, b) for (auto i = (a); i >= (b); i--)
#define Min(a, b) ((a) < (b) ? (a) : (b))
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define int_inf 0x3f3f3f3f
#define int_max 2147483647
#define ll_max 9223372036854775807
#define ull_max 18446744073709551615
#define ll_inf 0x7f7f7f7f7f7f7f7f
#define lowbit(x) (x) & (-x)
#define mp make_pair
#define fi first
#define se second
#define pb push_back
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
const ll mod = 1e9 + 7;
template <class T> inline T read() {
  T x = 0, f = 1;
  char s = getchar();
  while (s < '0' || s > '9') {
    if (s == '-')
      f = -1;
    s = getchar();
  };
  while (s >= '0' && s <= '9') {
    x = (x << 3) + (x << 1) + s - 48;
    s = getchar();
  };
  return x * f;
}
template <typename T> inline void wri(T x) {
  if (x < 0) {
    x = -x;
    putchar('-');
  };
  if (x / 10)
    wri(x / 10);
  putchar(x % 10 + 48);
}
template <typename T> inline void print(T x, char s) {
  wri(x);
  putchar(s);
}
template <typename T> T gcd(T a, T b) { return b == 0 ? a : gcd(b, a % b); }
// 关闭流同步就不能手写读入优化啦
int a, b;
int cnt = 1;
int vis[100010];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);
  cin >> a >> b;
  if (b % 2 == 1) {
    int l1 = 1, r1 = b, l2 = b + 1, r2 = b * 2 + 1;
    int cnt = 0;
    while (l1 < r1) {
      if (cnt >= b)
        break;
      cout << l1 << " " << r1 << endl;
      cnt++;
      l1++, r1--;
    }
    while (l2 < r2) {
      if (cnt >= b)
        break;
      cout << l2 << " " << r2 << endl;
      cnt++;
      l2++, r2--;
    }
  } else {
    int l1 = 1, r1 = b + 1, l2 = b + 2, r2 = b * 2 + 1;
    int cnt = 0;
    while (l1 < r1) {
      if (cnt >= b)
        break;
      cout << l1 << " " << r1 << endl;
      cnt++;
      l1++, r1--;
    }
    while (l2 < r2) {
      if (cnt >= b)
        break;
      cout << l2 << " " << r2 << endl;
      cnt++;
      l2++, r2--;
    }
  }
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, a, b) for (auto i = (a); i < (b); i++)
#define per(i, a, b) for (auto i = (a); i >= (b); i--)
#define Min(a, b) ((a) < (b) ? (a) : (b))
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define int_inf 0x3f3f3f3f
#define int_max 2147483647
#define ll_max 9223372036854775807
#define ull_max 18446744073709551615
#define ll_inf 0x7f7f7f7f7f7f7f7f
#define lowbit(x) (x) & (-x)
#define mp make_pair
#define fi first
#define se second
#define pb push_back
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
const ll mod = 1e9 + 7;
template <class T> inline T read() {
  T x = 0, f = 1;
  char s = getchar();
  while (s < '0' || s > '9') {
    if (s == '-')
      f = -1;
    s = getchar();
  };
  while (s >= '0' && s <= '9') {
    x = (x << 3) + (x << 1) + s - 48;
    s = getchar();
  };
  return x * f;
}
template <typename T> inline void wri(T x) {
  if (x < 0) {
    x = -x;
    putchar('-');
  };
  if (x / 10)
    wri(x / 10);
  putchar(x % 10 + 48);
}
template <typename T> inline void print(T x, char s) {
  wri(x);
  putchar(s);
}
template <typename T> T gcd(T a, T b) { return b == 0 ? a : gcd(b, a % b); }
// 关闭流同步就不能手写读入优化啦
int a, b;
int cnt = 1;
int vis[100010];
void get(int delta) {
  int cnt = 1;
  int flag = 0;
  while ((vis[cnt] || vis[cnt + delta]) && cnt + delta <= a) {
    cnt++;
  }
  if (cnt + delta != a + 1) {
    vis[cnt] = vis[cnt + delta] = 1;
    cout << cnt << " " << cnt + delta << endl;
  } else {
    cnt = 1;
    while ((vis[cnt] || vis[cnt + a - delta]) && cnt + a - delta <= a) {
      cnt++;
    }
    vis[cnt] = vis[cnt + a - delta] = 1;
    cout << cnt << " " << cnt + a - delta << endl;
  }
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);
  cin >> a >> b;
  rep(i, 1, b + 1) { get(i); }
  return 0;
}