Problem p03224 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef pair<int, int> pii;
const int inf = 0x3f3f3f3f, oo = inf;
#define pi 3.14159265358979323846
#define IL inline
#define RG register
#define rep(i, a, b) for (RG int i = (a); i < int(b); ++i)
#define Rep(i, a, b) for (RG int i = (a); i <= (b); ++i)
#define Dep(i, a, b) for (RG int i = (a); i >= (b); --i)
#define pc putchar
#define gc getchar
IL ll read() {
  RG ll x = 0;
  char f = 0;
  RG char c = gc();
  for (; !isdigit(c); c = gc())
    f |= (c == '-');
  for (; isdigit(c); c = gc())
    x = (x << 1) + (x << 3) + (c ^ 48);
  return f ? -x : x;
}
IL void write(ll x) {
  if (x < 0)
    x = -x, pc('-');
  if (x >= 10)
    write(x / 10);
  pc(x % 10 + '0');
}
IL void writeln(ll x) {
  write(x);
  puts("");
}
IL void writeln(ll x, char c, ll y) {
  write(x);
  pc(c);
  writeln(y);
}
IL void writeln(ll x, char c, ll y, char d, ll z) {
  write(x);
  pc(c);
  write(y);
  pc(d);
  writeln(z);
}
#define debug(x) printf(#x " = %d\n", x);
#define rd() read()
#define rdb() readdb()
#define mem(x, v) memset(x, v, sizeof(x))
#define pb push_back
#define mp make_pair
#define sqr(x) ((x) * (x))
#define lowbit(x) ((x) & (-(x)))
#define fin(x) freopen(#x ".in", "r", stdin)
#define fout(x) freopen(#x ".out", "w", stdout)
#define y1 ____y1
#define markh __markh
#define union _union
const int maxn = 1e5 + 233;
int n, a[353][maxn];
int vis[maxn], mark[maxn], cnt[maxn];
void solve(int k) {
  puts("Yes");
  printf("%d\n", k + 1);
  mem(vis, 0);
  Rep(i, 1, n) cnt[i] = 2;
  for (int i = 1; i <= k + 1; ++i) {
    printf("%d ", k);
    mem(mark, true);
    Rep(j, 1, n) if (cnt[j] == 0) mark[j] = false;
    int res = 0;
    for (int j = 1; j <= n; ++j) {
      if (mark[j]) {
        a[i][++a[i][0]] = j;
        --cnt[j];
        printf("%d ", j);
        if (vis[j] == 0)
          vis[j] = i;
        else {
          for (int w = 1; w <= a[vis[j]][0]; ++w) {
            mark[a[vis[j]][w]] = false;
          }
        }
        if (++res == k)
          break;
      }
    }
    puts("");
  }
} // k+1个长度为k的元素
int main() {
  n = read();
  for (int k = 1; k * (k + 1) <= 2 * n; ++k) {
    if (k * (k + 1) == 2 * n) {
      solve(k);
      return 0;
    }
  }
  puts("No");
  return 0;
}
// 长度为k
// k*(k+1)=2*n
/*n = 6
1 2 3
    3 4 5
1     4   6
  2     5 6*/

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef pair<int, int> pii;
const int inf = 0x3f3f3f3f, oo = inf;
#define pi 3.14159265358979323846
#define IL inline
#define RG register
#define rep(i, a, b) for (RG int i = (a); i < int(b); ++i)
#define Rep(i, a, b) for (RG int i = (a); i <= (b); ++i)
#define Dep(i, a, b) for (RG int i = (a); i >= (b); --i)
#define pc putchar
#define gc getchar
IL ll read() {
  RG ll x = 0;
  char f = 0;
  RG char c = gc();
  for (; !isdigit(c); c = gc())
    f |= (c == '-');
  for (; isdigit(c); c = gc())
    x = (x << 1) + (x << 3) + (c ^ 48);
  return f ? -x : x;
}
IL void write(ll x) {
  if (x < 0)
    x = -x, pc('-');
  if (x >= 10)
    write(x / 10);
  pc(x % 10 + '0');
}
IL void writeln(ll x) {
  write(x);
  puts("");
}
IL void writeln(ll x, char c, ll y) {
  write(x);
  pc(c);
  writeln(y);
}
IL void writeln(ll x, char c, ll y, char d, ll z) {
  write(x);
  pc(c);
  write(y);
  pc(d);
  writeln(z);
}
#define debug(x) printf(#x " = %d\n", x);
#define rd() read()
#define rdb() readdb()
#define mem(x, v) memset(x, v, sizeof(x))
#define pb push_back
#define mp make_pair
#define sqr(x) ((x) * (x))
#define lowbit(x) ((x) & (-(x)))
#define fin(x) freopen(#x ".in", "r", stdin)
#define fout(x) freopen(#x ".out", "w", stdout)
#define y1 ____y1
#define markh __markh
#define union _union
const int maxn = 1e5 + 233;
int n, a[853][maxn];
int vis[maxn], mark[maxn], cnt[maxn];
void solve(int k) {
  puts("Yes");
  printf("%d\n", k + 1);
  mem(vis, 0);
  Rep(i, 1, n) cnt[i] = 2;
  for (int i = 1; i <= k + 1; ++i) {
    printf("%d ", k);
    mem(mark, true);
    Rep(j, 1, n) if (cnt[j] == 0) mark[j] = false;
    int res = 0;
    for (int j = 1; j <= n; ++j) {
      if (mark[j]) {
        a[i][++a[i][0]] = j;
        --cnt[j];
        printf("%d ", j);
        if (vis[j] == 0)
          vis[j] = i;
        else {
          for (int w = 1; w <= a[vis[j]][0]; ++w) {
            mark[a[vis[j]][w]] = false;
          }
        }
        if (++res == k)
          break;
      }
    }
    puts("");
  }
} // k+1个长度为k的元素
int main() {
  n = read();
  for (int k = 1; k * (k + 1) <= 2 * n; ++k) {
    if (k * (k + 1) == 2 * n) {
      solve(k);
      return 0;
    }
  }
  puts("No");
  return 0;
}
// 长度为k
// k*(k+1)=2*n
/*n = 6
1 2 3
    3 4 5
1     4   6
  2     5 6*/