Problem p04023 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<long long, long long> pll;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define rep(i, j, k) for (register int i = (int)(j); i <= (int)(k); i++)
#define rrep(i, j, k) for (register int i = (int)(j); i >= (int)(k); i--)

ll read() {
  ll x = 0, f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = x * 10 + c - '0';
    c = getchar();
  }
  return x * f;
}

priority_queue<pair<ll, ll>> pq;
ll n, q;
ll st[100100], cnt;
ll ans[100100];
int main() {
#ifdef LZT
//	freopen("in","r",stdin);
#endif
  n = read(), q = read();
  st[++cnt] = n;
  for (int i = 1; i <= q; i++) {
    int x = read();
    while (st[cnt] >= x)
      cnt--;
    st[++cnt] = x;
  }
  pq.push(mp(st[cnt], 1));
  for (int i = cnt; i >= 1; i--) {
    ll sum = 0, cur = st[i];
    while (!pq.empty() && pq.top().fi > cur) {
      pair<ll, ll> nw = pq.top();
      pq.pop();
      sum += nw.se * (nw.fi / cur);
      if (nw.fi % cur)
        pq.push(mp(nw.fi % cur, nw.se));
    }
    pq.push(mp(cur, sum));
  }
  while (!pq.empty())
    ans[pq.top().fi] += pq.top().se, pq.pop();
  rrep(i, n, 1) ans[i] += ans[i + 1];
  rep(i, 1, n) printf("%lld \n", ans[i]);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<long long, long long> pll;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define rep(i, j, k) for (register int i = (int)(j); i <= (int)(k); i++)
#define rrep(i, j, k) for (register int i = (int)(j); i >= (int)(k); i--)

ll read() {
  ll x = 0, f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = x * 10 + c - '0';
    c = getchar();
  }
  return x * f;
}

priority_queue<pair<ll, ll>> pq;
ll n, q;
ll st[100100], cnt;
ll ans[100100];
int main() {
#ifdef LZT
//	freopen("in","r",stdin);
#endif
  n = read(), q = read();
  st[++cnt] = n;
  for (int i = 1; i <= q; i++) {
    ll x = read();
    while (st[cnt] >= x)
      cnt--;
    st[++cnt] = x;
  }
  pq.push(mp(st[cnt], 1));
  for (int i = cnt; i >= 1; i--) {
    ll sum = 0, cur = st[i];
    while (!pq.empty() && pq.top().fi > cur) {
      pair<ll, ll> nw = pq.top();
      pq.pop();
      sum += nw.se * (nw.fi / cur);
      if (nw.fi % cur)
        pq.push(mp(nw.fi % cur, nw.se));
    }
    pq.push(mp(cur, sum));
  }
  while (!pq.empty())
    ans[pq.top().fi] += pq.top().se, pq.pop();
  rrep(i, n, 1) ans[i] += ans[i + 1];
  rep(i, 1, n) printf("%lld \n", ans[i]);
  return 0;
}