Problem p04023 (C++)

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

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define pb push_back
#define mp make_pair
#define FOR(i, a, b) for (i = a; i <= b; i++)
#define DBG printf("Hi\n")
#define i64 long long int
#define ui64 unsigned long long int
#define xx first
#define yy second
#define ln 17
#define off 2002

#define IN freopen("circular_circles_input.txt", "r", stdin)
#define OUT freopen("output.txt", "w", stdout)
#define sq(x) ((x) * (x))

#define FASTIO                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL)

using namespace __gnu_pbds;
using namespace std;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef tree<i64, null_type, less<i64>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
typedef pair<int, int> pii;

#define log 20
#define mod 1000000007LL
#define INF 1000000000000000000LL
#define maxn 1000005

const long double eps = 1e-9;

i64 a[maxn];

int main() {
  int n, q;

  scanf("%d %d", &n, &q);

  vector<i64> v;
  v.pb(n);

  for (int i = 1; i <= q; i++) {
    i64 x;
    scanf("%lld", &x);
    while (v.size() > 0 && v.back() >= x)
      v.pop_back();
    v.push_back(x);
  }

  map<i64, i64> cnt;

  cnt[v.back()] = 1;

  reverse(v.begin(), v.end());

  for (int i = 0; i + 1 < v.size(); i++) {
    //  printf("-- %lld\n",v[i]) ;
    i64 full = v[i + 1];
    i64 fullCnt = 0;

    while (1) {
      map<i64, i64>::iterator it = cnt.end();
      it--;

      if (it->xx <= v[i + 1])
        break;

      pair<i64, i64> p = *it;
      cnt.erase(it);

      fullCnt += (p.xx / full) * p.yy;
      if (p.xx % full != 0) {
        i64 sz = p.xx % full;
        if (cnt.find(sz) == cnt.end())
          cnt[sz] = 0;
        cnt[sz] += p.yy;
      }
    }
    if (cnt.find(full) == cnt.end())
      cnt[full] = 0;
    cnt[full] += fullCnt;
  }

  for (auto p : cnt)
    a[p.xx] += p.yy;

  for (int i = n; i >= 1; i--)
    a[i] += a[i + 1];

  for (int i = 1; i <= n; i++)
    printf("%lld\n", a[i]);

  return 0;
}


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

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define pb push_back
#define mp make_pair
#define FOR(i, a, b) for (i = a; i <= b; i++)
#define DBG printf("Hi\n")
#define i64 long long int
#define ui64 unsigned long long int
#define xx first
#define yy second
#define ln 17
#define off 2002

#define IN freopen("circular_circles_input.txt", "r", stdin)
#define OUT freopen("output.txt", "w", stdout)
#define sq(x) ((x) * (x))

#define FASTIO                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL)

using namespace __gnu_pbds;
using namespace std;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef tree<i64, null_type, less<i64>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
typedef pair<int, int> pii;

#define log 20
#define mod 1000000007LL
#define INF 1000000000000000000LL
#define maxn 1000005

const long double eps = 1e-9;

i64 a[maxn];

int main() {
  int n, q;

  scanf("%d %d", &n, &q);

  vector<i64> v;
  v.pb(n);

  for (int i = 1; i <= q; i++) {
    i64 x;
    scanf("%lld", &x);
    while (v.size() > 0 && v.back() >= x)
      v.pop_back();
    v.push_back(x);
  }

  map<i64, i64> cnt;

  cnt[v.back()] = 1;

  reverse(v.begin(), v.end());

  for (int i = 0; i + 1 < v.size(); i++) {
    //  printf("-- %lld\n",v[i]) ;
    i64 full = v[i + 1];
    i64 fullCnt = 0;

    while (cnt.size() > 0) {
      map<i64, i64>::iterator it = cnt.end();
      it--;

      if (it->xx <= v[i + 1])
        break;

      pair<i64, i64> p = *it;
      cnt.erase(it);

      fullCnt += (p.xx / full) * p.yy;
      if (p.xx % full != 0) {
        i64 sz = p.xx % full;
        if (cnt.find(sz) == cnt.end())
          cnt[sz] = 0;
        cnt[sz] += p.yy;
      }
    }
    if (cnt.find(full) == cnt.end())
      cnt[full] = 0;
    cnt[full] += fullCnt;
  }

  for (auto p : cnt)
    a[p.xx] += p.yy;

  for (int i = n; i >= 1; i--)
    a[i] += a[i + 1];

  for (int i = 1; i <= n; i++)
    printf("%lld\n", a[i]);

  return 0;
}