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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (long long i = 0; i < (long long)(n); i++)
#define REP(i, k, n) for (long long i = k; i < (long long)(n); i++)
#define all(a) a.begin(), a.end()
#define eb emplace_back
#define lb(v, k) (lower_bound(all(v), k) - v.begin())
#define ub(v, k) (upper_bound(all(v), k) - v.begin())
#define fi first
#define se second
#define pi M_PI
#define PQ(T) priority_queue<T>
#define SPQ(T) priority_queue<T, vector<T>, greater<T>>
using ll = long long;
using P = pair<ll, ll>;
using PP = tuple<ll, ll, ll>;
using S = multiset<ll>;
using vi = vector<ll>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
const ll inf = 1001001001001001;
const int INF = 1001001001;
const int mod = 1000000007;
const double eps = 1e-10;
template <class T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> void out(T a) { cout << a << '\\n'; }
template <class T> void outp(T a) {
  cout << '(' << a.fi << ',' << a.se << ')' << '\\n';
}
template <class T> void outvp(T v) {
  rep(i, v.size()) cout << '(' << v[i].fi << ',' << v[i].se << ')';
  cout << '\\n';
}
template <class T> void outv(T v) {
  rep(i, v.size()) {
    if (i)
      cout << ' ';
    cout << v[i];
  }
  cout << '\\n';
}
template <class T> void outvv(T v) { rep(i, v.size()) outv(v[i]); }
template <class T> bool isin(T x, T l, T r) { return (l) <= (x) && (x) <= (r); }
template <class T> void YesNo(T b) {
  if (b)
    out("Yes");
  else
    out("No");
}
void decimal(int x) { cout << fixed << setprecision(x); }
ll GCD(ll a, ll b) {
  if (b == 0)
    return a;
  return GCD(b, a % b);
}
ll LCM(ll a, ll b) { return (a / GCD(a, b)) * (b / GCD(a, b)) * GCD(a, b); }
ll POW(ll a, ll b) {
  a %= mod;
  if (b == 0)
    return 1;
  if (b & 1)
    return a * POW(a, b - 1) % mod;
  ll k = POW(a, b / 2);
  return k * k % mod;
}
vi calc(ll x) {
  vi res;
  while (x > 0) {
    res.eb(x % 10);
    x /= 10;
  }
  reverse(all(res));
  return res;
}

// d桁の集合->d+1桁の集合
vi foo(const vi &v) {
  vi res;
  for (auto p : v) {
    REP(j, -1, 2) {
      int add = (p % 10) + j;
      if (isin(add, 0, 9))
        res.eb(p * 10 + add);
    }
  }
  return res;
}

void solve() {
  int k;
  cin >> k;
  vi a;   // 全列挙
  vi cur; // 1桁
  REP(v, 1, 10) {
    cur.eb(v);
    a.eb(v);
  }
  REP(d, 1, 10) {
    cur = foo(cur);
    for (auto p : cur)
      a.eb(p);
  }
  out(a[k - 1]);
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  solve();
}


Error: -6
/usr/include/c++/12/debug/vector:442:
In function:
    std::debug::vector<_Tp, _Allocator>::reference std::debug::vector<_Tp, 
    _Allocator>::operator[](size_type) [with _Tp = long long int; _Allocator 
    = std::allocator<long long int>; reference = long long int&; size_type = 
    long unsigned int]

Error: attempt to subscript container with out-of-bounds index -1, but 
container only holds 10 elements.

Objects involved in the operation:
    sequence "this" @ 0x55dd209a0618 {
      type = std::debug::vector<long long, std::allocator<long long> >;
    }


Fixed submission:

#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;

#define rep(i, n) for (long long i = 0; i < (long long)(n); i++)
#define REP(i, k, n) for (long long i = k; i < (long long)(n); i++)
#define all(a) a.begin(), a.end()
#define NP next_permutation
#define pb push_back
#define eb emplace_back
#define lb(v, k) (lower_bound(all(v), k) - v.begin())
#define ub(v, k) (upper_bound(all(v), k) - v.begin())
#define fi first
#define se second
#define pi M_PI
typedef long long ll;
typedef pair<ll, ll> P;
typedef tuple<ll, ll, ll> PP;
typedef multiset<ll> S;
typedef priority_queue<ll> PQ;
typedef priority_queue<P, vector<P>, greater<P>> SPQ;
using vi = vector<ll>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;

const ll inf = 1001001001001001;
const int INF = 1001001001;
const int mod = 1000000007;

template <class T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> void decimal(T a) { cout << fixed << setprecision(a); }
template <class T> void out(T a) { cout << a << endl; }
template <class T> void outp(T a) {
  cout << '(' << a.fi << ',' << a.se << ')' << endl;
}
template <class T> void outvp(T v) { rep(i, v.size()) outp(v[i]); }
template <class T> void outv(T v) {
  rep(i, v.size()) {
    if (i)
      cout << ' ';
    cout << v[i];
  }
  cout << endl;
}
template <class T> void outvv(T v) {
  rep(i, v.size()) {
    rep(j, v[i].size()) {
      if (j)
        cout << ' ';
      cout << v[i][j];
    }
    cout << endl;
  }
}

void YesNo(bool a) {
  if (a)
    out("Yes");
  else
    out("No");
}
void yesno(bool a) {
  if (a)
    out("yes");
  else
    out("no");
}
void YESNO(bool a) {
  if (a)
    out("YES");
  else
    out("NO");
}
ll GCD(ll a, ll b) {
  if (b == 0)
    return a;
  return GCD(b, a % b);
}
ll LCM(ll a, ll b) { return (a / GCD(a, b)) * (b / GCD(a, b)) * GCD(a, b); }
ll POW(ll a, ll b) {
  ll c = 1;
  while (b > 0) {
    if (b & 1) {
      c = a * c % mod;
    }
    a = a * a % mod;
    b >>= 1;
  }
  return c;
}
vi calc(ll x) {
  vi res;
  while (x > 0) {
    res.eb(x % 10);
    x /= 10;
  }
  reverse(all(res));
  return res;
}

vi v;

void cut(vvi d, ll &p, ll &k) {
  ll c;
  REP(i, 1, 10) {
    k -= d[p][i];
    if (k <= 0) {
      k += d[p][i];
      c = i;
      break;
    }
  }
  p--;
  v.eb(c);
}

void cut2(vvi d, ll &p, ll &k) {
  if (p == 0)
    return;
  ll c;
  rep(i, 10) {
    k -= d[p][i];
    if (k <= 0) {
      k += d[p][i];
      c = i;
      break;
    }
  }
  p--;
  v.eb(c);
  cut2(d, p, k);
}

void solve() {
  ll k;
  cin >> k;
  vvvi dp(20, vvi(10, vi(10)));
  REP(i, 1, 10) dp[1][i][i]++;
  REP(t, 2, 20) {
    REP(i, 1, 10) {
      rep(j, 10) {
        dp[t][i][j - 1] += dp[t - 1][i][j];
        dp[t][i][j] += dp[t - 1][i][j];
        if (j == 9)
          continue;
        dp[t][i][j + 1] += dp[t - 1][i][j];
      }
    }
  }
  // outvv(dp);
  vvi d(20, vi(10, 0));
  REP(i, 1, 20) {
    REP(j, 1, 10) {
      ll q = 0;
      rep(k, 10) { q += dp[i][j][k]; }
      d[i][j] = q;
    }
  }
  d[1][0] = 1;
  REP(i, 2, 20) { d[i][0] += d[i - 1][0] + d[i - 1][1]; }
  // outvv(d);
  vi s(20, 0);
  REP(i, 1, 20) {
    ll q = 0;
    REP(j, 1, 10) q += d[i][j];
    s[i] = q;
  }
  // outv(s);
  ll p = 0;
  REP(i, 1, 20) {
    k -= s[i];
    if (k < 0) {
      k += s[i];
      p = i;
      break;
    }
  }
  // cout<<p<<" "<<k<<endl;
  cut(d, p, k);
  // cout<<p<<" "<<k<<endl;
  cut2(d, p, k);
  rep(i, (ll)v.size()) cout << v[i];
  cout << endl;
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  solve();
}