Problem p04023 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int, int>
#define mp make_pair
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define eps 1e-10
#define MAXN 100005
// #define ivorysi
using namespace std;
typedef long long int64;
typedef unsigned int u32;
typedef double db;
template <class T> void read(T &res) {
  res = 0;
  T f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    res = res * 10 + c - '0';
    c = getchar();
  }
  res *= f;
}
template <class T> void out(T x) {
  if (x < 0) {
    x = -x;
    putchar('-');
  }
  if (x >= 10) {
    out(x / 10);
  }
  putchar('0' + x % 10);
}
int N, Q, top;
int64 sta[MAXN];
int64 cnt[MAXN], ans[MAXN];
void Solve() {
  read(N);
  read(Q);
  sta[++top] = N;
  int p;
  for (int i = 1; i <= Q; ++i) {
    read(p);
    while (top && sta[top] >= p)
      --top;
    sta[++top] = p;
  }
  cnt[top] = 1;
  for (int i = top; i >= 1; --i) {
    if (i != top)
      cnt[i] += cnt[i + 1] * (sta[i + 1] / sta[i]);
    if (i == 1) {
      ans[sta[1]] += cnt[1];
      continue;
    }
    int64 r = sta[i] % sta[i - 1];
    while (r > sta[1]) {
      int t = upper_bound(sta + 1, sta + top + 1, r) - sta - 1;
      cnt[t] += cnt[i] * (r / sta[t]);
      r %= sta[t];
    }
    ans[r] += cnt[i];
  }
  for (int i = N; i >= 1; --i)
    ans[i] += ans[i + 1];
  for (int i = 1; i <= N; ++i) {
    out(ans[i]);
    enter;
  }
}
int main() {
#ifdef ivorysi
  freopen("f1.in", "r", stdin);
#endif
  Solve();
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int, int>
#define mp make_pair
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define eps 1e-10
#define MAXN 100005
// #define ivorysi
using namespace std;
typedef long long int64;
typedef unsigned int u32;
typedef double db;
template <class T> void read(T &res) {
  res = 0;
  T f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    res = res * 10 + c - '0';
    c = getchar();
  }
  res *= f;
}
template <class T> void out(T x) {
  if (x < 0) {
    x = -x;
    putchar('-');
  }
  if (x >= 10) {
    out(x / 10);
  }
  putchar('0' + x % 10);
}
int N, Q, top;
int64 sta[MAXN];
int64 cnt[MAXN], ans[MAXN];
void Solve() {
  read(N);
  read(Q);
  sta[++top] = N;
  int64 p;
  for (int i = 1; i <= Q; ++i) {
    read(p);
    while (top && sta[top] >= p)
      --top;
    sta[++top] = p;
  }
  cnt[top] = 1;
  for (int i = top; i >= 1; --i) {
    if (i != top)
      cnt[i] += cnt[i + 1] * (sta[i + 1] / sta[i]);
    if (i == 1) {
      ans[sta[1]] += cnt[1];
      continue;
    }
    int64 r = sta[i] % sta[i - 1];
    while (r > sta[1]) {
      int t = upper_bound(sta + 1, sta + top + 1, r) - sta - 1;
      cnt[t] += cnt[i] * (r / sta[t]);
      r %= sta[t];
    }
    ans[r] += cnt[i];
  }
  for (int i = N; i >= 1; --i)
    ans[i] += ans[i + 1];
  for (int i = 1; i <= N; ++i) {
    out(ans[i]);
    enter;
  }
}
int main() {
#ifdef ivorysi
  freopen("f1.in", "r", stdin);
#endif
  Solve();
  return 0;
}