Problem p02977 (C++)

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

int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i <= 30; i++)
    if (n == (1 << i)) {
      std::cout << "No" << std::endl;
      return 0;
    }
  int nn;
  if (n % 4 == 3)
    nn = n;
  else
    nn = n / 4 * 4 - 1;
  std::vector<int> all;
  for (int i = 1; i <= nn; i++)
    all.push_back(i);
  std::vector<int> xorsum(nn + 1);
  for (int i = 0; i < nn; i++)
    xorsum[i + 1] = xorsum[i] ^ all[i];
  std::vector<std::pair<int, int>> add;
  if (n % 4 == 3) {
  } else if (n % 4 == 0) {
    int r0 = nn;
    int r1 = -1;
    for (int i = 0; i <= nn; i++) {
      if (xorsum[i] == ((nn + 1) ^ nn)) {
        r1 = i ? n + i : nn;
        break;
      }
    }
    if (r1 == -1) {
      std::cout << "No" << std::endl;
      return 0;
    }
    add.push_back({r0, nn + 1});
    add.push_back({r1, nn + n + 1});
  } else {
    add.push_back({n + 1, nn + 1});
    add.push_back({n + 1, nn + 2});
    add.push_back({nn + 1, n + nn + 2});
    add.push_back({nn + 2, n + nn + 1});
    if (n % 4 == 3) {
      int r0 = nn;
      int r1 = -1;
      for (int i = 0; i <= nn; i++) {
        if (xorsum[i] == ((nn + 3) ^ nn)) {
          r1 = i ? n + i : nn;
          break;
        }
      }
      if (r1 == -1) {
        std::cout << "No" << std::endl;
        return 0;
      }
      add.push_back({r0, nn + 3});
      add.push_back({r1, nn + 3 + n});
    }
  }
  std::cout << "Yes" << std::endl;
  for (int i = 1; i < nn; i++)
    add.push_back({i, i + 1});
  add.push_back({nn, n + 1});
  for (int i = n + 1; i < n + nn; i++)
    add.push_back({i, i + 1});
  assert(add.size() == 2 * n - 1);
  for (auto i : add)
    std::cout << i.first << " " << i.second << std::endl;
  return 0;
}


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

int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i <= 30; i++)
    if (n == (1 << i)) {
      std::cout << "No" << std::endl;
      return 0;
    }
  int nn;
  if (n % 4 == 3)
    nn = n;
  else
    nn = n / 4 * 4 - 1;
  std::vector<int> all;
  for (int i = 1; i <= nn; i++)
    all.push_back(i);
  std::vector<int> xorsum(nn + 1);
  for (int i = 0; i < nn; i++)
    xorsum[i + 1] = xorsum[i] ^ all[i];
  std::vector<std::pair<int, int>> add;
  if (n % 4 == 3) {
  } else if (n % 4 == 0) {
    int r0 = nn;
    int r1 = -1;
    for (int i = 0; i <= nn; i++) {
      if (xorsum[i] == ((nn + 1) ^ nn)) {
        r1 = i ? n + i : nn;
        break;
      }
    }
    if (r1 == -1) {
      std::cout << "No" << std::endl;
      return 0;
    }
    add.push_back({r0, nn + 1});
    add.push_back({r1, nn + n + 1});
  } else {
    add.push_back({n + 1, nn + 1});
    add.push_back({n + 1, nn + 2});
    add.push_back({nn + 1, n + nn + 2});
    add.push_back({nn + 2, n + nn + 1});
    if (n % 4 == 2) {
      add.push_back({nn + 3, n + 2});
      add.push_back({n + nn + 3, nn + 2});
    }
  }
  std::cout << "Yes" << std::endl;
  for (int i = 1; i < nn; i++)
    add.push_back({i, i + 1});
  add.push_back({nn, n + 1});
  for (int i = n + 1; i < n + nn; i++)
    add.push_back({i, i + 1});
  assert(add.size() == 2 * n - 1);
  for (auto i : add)
    std::cout << i.first << " " << i.second << std::endl;
  return 0;
}