Problem p04023 (C++)

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

using namespace std;

typedef long long ll;

const int __SIZE = 1 << 18;
char ibuf[__SIZE], *iS, *iT;

#define ge                                                                     \
  (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, __SIZE, stdin),               \
               (iS == iT ? EOF : *iS++))                                       \
            : *iS++)
#define ri read_int()
#define rl read_ll()
#define FILE(s) freopen(s "in", "r", stdin), freopen(s "out", "w", stdout)

template <typename T> inline void read(T &x) {
  char ch, t = 0;
  x = 0;
  while (!isdigit(ch = ge))
    t |= ch == '-';
  while (isdigit(ch))
    x = x * 10 + (ch ^ 48), ch = ge;
  x = t ? -x : x;
}
inline int read_int() {
  int x;
  return read(x), x;
}
inline ll read_ll() {
  ll x;
  return read(x), x;
}

template <typename T> inline void chkmin(T &a, T b) { a = a < b ? a : b; }

const int MAXN = 100010;

int top;
ll a[MAXN];
ll c[MAXN];
ll tim[MAXN];

inline void solve(int p, ll x, ll t) {
  tim[p] += t * (x / a[p]), x %= a[p];
  p = upper_bound(a + 1, a + 1 + p, x) - a;
  if (p == 1)
    c[1] += t, c[x + 1] -= t;
  else
    solve(p - 1, x, t);
}

int main() {
#ifdef LOCAL
  FILE("");
#endif

  int n = ri, q = ri;

  a[++top] = n;
  for (int i = 1; i <= q; i++) {
    int x = rl;
    while (a[top] >= x)
      --top;
    a[++top] = x;
  }

  tim[top] = 1;
  for (int i = top; i > 1; i--)
    solve(i - 1, a[i], tim[i]);

  for (int i = 1; i <= a[1]; i++)
    c[i] += c[i - 1];

  for (int i = 1; i <= a[1]; i++)
    printf("%lld\n", c[i] + tim[1]);
  n -= a[1];
  while (n--)
    puts("0");
  return 0;
}


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

using namespace std;

typedef long long ll;

const int __SIZE = 1 << 18;
char ibuf[__SIZE], *iS, *iT;

#define ge                                                                     \
  (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, __SIZE, stdin),               \
               (iS == iT ? EOF : *iS++))                                       \
            : *iS++)
#define ri read_int()
#define rl read_ll()
#define FILE(s) freopen(s "in", "r", stdin), freopen(s "out", "w", stdout)

template <typename T> inline void read(T &x) {
  char ch, t = 0;
  x = 0;
  while (!isdigit(ch = ge))
    t |= ch == '-';
  while (isdigit(ch))
    x = x * 10 + (ch ^ 48), ch = ge;
  x = t ? -x : x;
}
inline int read_int() {
  int x;
  return read(x), x;
}
inline ll read_ll() {
  ll x;
  return read(x), x;
}

template <typename T> inline void chkmin(T &a, T b) { a = a < b ? a : b; }

const int MAXN = 100010;

int top;
ll a[MAXN];
ll c[MAXN];
ll tim[MAXN];

inline void solve(int p, ll x, ll t) {
  tim[p] += t * (x / a[p]), x %= a[p];
  p = upper_bound(a + 1, a + 1 + p, x) - a;
  if (p == 1)
    c[1] += t, c[x + 1] -= t;
  else
    solve(p - 1, x, t);
}

int main() {
#ifdef LOCAL
  FILE("");
#endif

  int n = ri, q = ri;

  a[++top] = n;
  for (int i = 1; i <= q; i++) {
    ll x = rl;
    while (a[top] >= x && top)
      --top;
    a[++top] = x;
  }

  tim[top] = 1;
  for (int i = top; i > 1; i--)
    solve(i - 1, a[i], tim[i]);

  for (int i = 1; i <= a[1]; i++)
    c[i] += c[i - 1];

  for (int i = 1; i <= a[1]; i++)
    printf("%lld\n", c[i] + tim[1]);
  n -= a[1];
  while (n--)
    puts("0");
  return 0;
}