Problem p03267 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define whole(x) (x).begin(), (x).end()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())
using P = pair<int, int>;
#define debug(var) cerr << "[" << #var << "] " << var << endl
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
const ll mod = 1000000007;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};

int main() {
  int l;
  cin >> l;
  if (l < 10)
    return -1;
  l--;
  int keta = 0;
  while (l >> keta) {
    keta++;
  }
  int n = keta;

  int m = (n - 1) * 2 + __builtin_popcount(l);
  int num = 0;
  cout << n << " " << m << endl;
  if (m > 60)
    return -1;

  rep(i, n - 1) {
    cout << i + 1 << " " << i + 2 << " " << 0 << endl;
    cout << i + 1 << " " << i + 2 << " " << (1 << (n - 2 - i)) << endl;
    num += 2;
  }
  /*
  int x = 1<<(keta-1);
  for (int i=keta-2; i>=0; i--) {
      if ((l>>i&1)==0) continue;
      cout << 1 << " " << 1+(keta-1-i) << " " << x <<  endl;
      x += 1<<i;
  }


  cout << 1 << " " << n << " " << x << endl;
  */
  int x = 0;
  for (int i = keta - 1; i >= 0; i--) {
    if ((l >> i & 1) == 0)
      continue;
    x += 1 << i;
    int index = i - 1;
    while (index >= 0) {
      if (l >> index & 1)
        break;
      index--;
    }
    if (index < 0)
      index = 0;
    cout << 1 << " " << keta - index << " " << x << endl;
    num++;
  }
  if (num != m)
    return -1;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define whole(x) (x).begin(), (x).end()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())
using P = pair<int, int>;
#define debug(var) cerr << "[" << #var << "] " << var << endl
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
const ll mod = 1000000007;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};

int main() {
  int l;
  cin >> l;
  if (l == 2) {
    printf("2 2\n");
    printf("1 2 0\n1 2 1\n");
    return 0;
  }
  l--;
  int keta = 0;
  while (l >> keta) {
    keta++;
  }
  int n = keta;

  int m = (n - 1) * 2 + __builtin_popcount(l);
  int num = 0;
  cout << n << " " << m << endl;
  if (m > 60)
    return -1;

  rep(i, n - 1) {
    cout << i + 1 << " " << i + 2 << " " << 0 << endl;
    cout << i + 1 << " " << i + 2 << " " << (1 << (n - 2 - i)) << endl;
    num += 2;
  }
  /*
  int x = 1<<(keta-1);
  for (int i=keta-2; i>=0; i--) {
      if ((l>>i&1)==0) continue;
      cout << 1 << " " << 1+(keta-1-i) << " " << x <<  endl;
      x += 1<<i;
  }


  cout << 1 << " " << n << " " << x << endl;
  */
  int x = 0;
  for (int i = keta - 1; i >= 0; i--) {
    if ((l >> i & 1) == 0)
      continue;
    x += 1 << i;
    int index = i - 1;
    while (index >= 0) {
      if (l >> index & 1)
        break;
      index--;
    }
    if (index < 0)
      index = 0;
    cout << 1 << " " << keta - index << " " << x << endl;
    num++;
  }
  if (num != m)
    return -1;

  return 0;
}