Problem p03350 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define Fast_IO ios::sync_with_stdio(false);
#define DEBUG                                                                  \
  fprintf(stderr, "Running on Line %d in Function %s\n", __LINE__, __FUNCTION__)
// mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
#define fir first
#define sec second
#define mod 998244353
#define ll long long
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f3f
inline int read() {
  char ch = getchar();
  int nega = 1;
  while (!isdigit(ch)) {
    if (ch == '-')
      nega = -1;
    ch = getchar();
  }
  int ans = 0;
  while (isdigit(ch)) {
    ans = ans * 10 + ch - 48;
    ch = getchar();
  }
  if (nega == -1)
    return -ans;
  return ans;
}
inline char rdchar() {
  char ch = getchar();
  while (!(ch >= 'a' && ch <= 'z') && !(ch >= 'A' && ch <= 'Z') &&
         !(ch >= '0' && ch <= '9'))
    ch = getchar();
  return ch;
}
typedef pair<int, int> pii;
#define N ((1 << 21) + 5)
int fir[N][2];
int *f[N];
int pool[N * 22], *p = pool;
int n, k;
pair<string, int> get(int x) {
  for (int j = 22; j >= 0; j--) {
    if (x >> j & 1) {
      string ans;
      for (int i = j - 1; i >= 0; i--) {
        if (x >> i & 1)
          ans += '1';
        else
          ans += '0';
      }
      return mp(ans, j);
    }
  }
  assert(0);
}
int rev(int x) {
  for (int j = 22; j >= 0; j--) {
    if (x >> j & 1) {
      int res = 1 << j;
      for (int i = 0; i < j; i++) {
        if (x >> i & 1)
          res |= (1 << (j - i));
      }
      return res;
    }
  }
  assert(0);
}
signed main() {
  cin >> n >> k;
  int cur = 1;
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j < (1 << i); j++) {
      // printf("%d %d\n",i,j);
      int x = rdchar() - '0';
      int ct = (1 << (n - i + 1)) + 2;
      f[cur] = p;
      p += ct;
      if (x)
        f[1][cur] = 1;
      fir[cur][0] = fir[cur][1] = 25;
      for (int k = 0; k <= i - 1; k++)
        fir[cur][j >> k & 1] = k;
      // printf("%d %d %d\n",cur,fir[cur][0],fir[cur][1]);
      cur++;
    }
  }
  cur = 1;
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j < (1 << i); j++) {
      int qaq = 1;
      for (int k = 0; k <= n - i; k++) {
        for (int c = 0; c < (1 << k); c++) {
          if (fir[qaq][0] != 25)
            f[cur * 2][((c) & ((1 << (fir[qaq][0])) - 1)) +
                       (1 << (fir[qaq][0]))] += f[cur][qaq];
          if (fir[qaq][1] != 25) {
            // printf("%d %d
            // %d\n",qaq,c,((c)&((1<<(fir[qaq][1])-1)))+(1<<(fir[qaq][1])));
            f[cur * 2 + 1][((c) & ((1 << (fir[qaq][1])) - 1)) +
                           (1 << (fir[qaq][1]))] += f[cur][qaq];
          }
          qaq++;
        }
      }
      cur++;
    }
  }
  cur = 1;
  string ans = "";
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j < (1 << i); j++) {
      int maxn = 0, qaq = 1;
      for (int k = 0; k <= n - i; k++) {
        for (int c = 0; c < (1 << k); c++) {
          // printf("%d %d : %d\n",cur,qaq,f[cur][qaq]);
          maxn += f[cur][qaq];
          qaq++;
        }
      }
      if (maxn >= k) {
        // cout<<cur<<endl;
        auto [str, len] = get(cur);
        // cout<<str<<endl;
        if (len > (int)ans.length() || str < ans)
          ans = str;
      }
      cur++;
    }
  }
  cout << ans << endl;
  return 0;
}
/*
-, 1, 00, 10, 11, 001, 100, 101, 110
*/


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define Fast_IO ios::sync_with_stdio(false);
#define DEBUG                                                                  \
  fprintf(stderr, "Running on Line %d in Function %s\n", __LINE__, __FUNCTION__)
// mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
#define fir first
#define sec second
#define mod 998244353
#define ll long long
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f3f
inline int read() {
  char ch = getchar();
  int nega = 1;
  while (!isdigit(ch)) {
    if (ch == '-')
      nega = -1;
    ch = getchar();
  }
  int ans = 0;
  while (isdigit(ch)) {
    ans = ans * 10 + ch - 48;
    ch = getchar();
  }
  if (nega == -1)
    return -ans;
  return ans;
}
inline char rdchar() {
  char ch = getchar();
  while (!(ch >= 'a' && ch <= 'z') && !(ch >= 'A' && ch <= 'Z') &&
         !(ch >= '0' && ch <= '9'))
    ch = getchar();
  return ch;
}
typedef pair<int, int> pii;
#define N ((1 << 21) + 5)
int fir[N][2];
int *f[N];
int pool[N * 23], *p = pool;
int n, k;
pair<string, int> get(int x) {
  for (int j = 22; j >= 0; j--) {
    if (x >> j & 1) {
      string ans;
      for (int i = j - 1; i >= 0; i--) {
        if (x >> i & 1)
          ans += '1';
        else
          ans += '0';
      }
      return mp(ans, j);
    }
  }
  assert(0);
}
int rev(int x) {
  for (int j = 22; j >= 0; j--) {
    if (x >> j & 1) {
      int res = 1 << j;
      for (int i = 0; i < j; i++) {
        if (x >> i & 1)
          res |= (1 << (j - i));
      }
      return res;
    }
  }
  assert(0);
}
signed main() {
  cin >> n >> k;
  int cur = 1;
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j < (1 << i); j++) {
      // printf("%d %d\n",i,j);
      int x = rdchar() - '0';
      int ct = (1 << (n - i + 1)) + 2;
      f[cur] = p;
      p += ct;
      if (x)
        f[1][cur] = 1;
      fir[cur][0] = fir[cur][1] = 25;
      for (int k = 0; k <= i - 1; k++)
        fir[cur][j >> k & 1] = k;
      // printf("%d %d %d\n",cur,fir[cur][0],fir[cur][1]);
      cur++;
    }
  }
  cur = 1;
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j < (1 << i); j++) {
      int qaq = 1;
      for (int k = 0; k <= n - i; k++) {
        for (int c = 0; c < (1 << k); c++) {
          if (fir[qaq][0] != 25)
            f[cur * 2][((c) & ((1 << (fir[qaq][0])) - 1)) +
                       (1 << (fir[qaq][0]))] += f[cur][qaq];
          if (fir[qaq][1] != 25) {
            // printf("%d %d
            // %d\n",qaq,c,((c)&((1<<(fir[qaq][1])-1)))+(1<<(fir[qaq][1])));
            f[cur * 2 + 1][((c) & ((1 << (fir[qaq][1])) - 1)) +
                           (1 << (fir[qaq][1]))] += f[cur][qaq];
          }
          qaq++;
        }
      }
      cur++;
    }
  }
  cur = 1;
  string ans = "";
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j < (1 << i); j++) {
      int maxn = 0, qaq = 1;
      for (int k = 0; k <= n - i; k++) {
        for (int c = 0; c < (1 << k); c++) {
          // printf("%d %d : %d\n",cur,qaq,f[cur][qaq]);
          maxn += f[cur][qaq];
          qaq++;
        }
      }
      if (maxn >= k) {
        // cout<<cur<<endl;
        auto [str, len] = get(cur);
        // cout<<str<<endl;
        if (len > (int)ans.length() || str < ans)
          ans = str;
      }
      cur++;
    }
  }
  cout << ans << endl;
  return 0;
}
/*
-, 1, 00, 10, 11, 001, 100, 101, 110
*/