Problem p04023 (C++)

Buggy submission (status: Runtime Error):
/*
 * Author: ylx
 * LANG: c++
 * PROG:
 */
#include <bits/stdc++.h>
#define ll long long
#define int long long
using namespace std;
void qmax(int &x, int y) {
  if (x < y)
    x = y;
}
void qmin(int &x, int y) {
  if (x > y)
    x = y;
}
inline ll read() {
  char s;
  ll k = 0;
  while ((s = getchar()) != EOF && !(isdigit(s)))
    ;
  if (s == EOF)
    exit(0);
  while (isdigit(s))
    k = k * 10 + (s ^ '0'), s = getchar();
  return k;
}
inline void write(int x) {
  static char cnt, num[15];
  cnt = 0;
  if (!x) {
    putchar('0');
    return;
  }
  for (; x; x /= 10)
    num[++cnt] = x % 10;
  for (; cnt; putchar(num[cnt--] + 48))
    ;
}
const int maxn = 1e5 + 100;
int n, m;
ll b, ans[maxn], t[maxn], S;
ll top, s[maxn];
int solve(ll x, int r) {
  int l = 1, res = 0;
  while (l <= r) {
    int mid = (l + r) >> 1;
    if (s[mid] <= x)
      res = mid, l = mid + 1;
    else
      r = mid - 1;
  }
  return res;
}
signed main() {
#ifdef ylx
  freopen("agc003e.in", "r", stdin);
  freopen("agc003e.out", "w", stdout);
#endif
  n = read();
  m = read();
  while (m--) {
    b = read();
    while (s[top] >= b && top >= 1)
      top--;
    top++;
    s[top] = b;
  }
  m = top;
  t[m] = 1;
  for (int i = m, p; i >= 1; i--) {
    ll k = s[i];
    p = solve(k, i - 1);
    while (p) {
      t[p] += (k / s[p]) * t[i];
      k %= s[p];
      p = solve(k, p - 1);
    }
    ans[1] += t[i];
    ans[k + 1] -= t[i];
  }
  for (int i = 1; i <= n; i++) {
    S += ans[i];
    printf("%lld\n", S);
  }
  return 0;
}
// 2018年 08月 16日 星期四 14:14:17 CST


Fixed submission:
/*
 * Author: ylx
 * LANG: c++
 * PROG:
 */
#include <bits/stdc++.h>
#define ll long long
#define int long long
using namespace std;
void qmax(int &x, int y) {
  if (x < y)
    x = y;
}
void qmin(int &x, int y) {
  if (x > y)
    x = y;
}
inline ll read() {
  char s;
  ll k = 0;
  while ((s = getchar()) != EOF && !(isdigit(s)))
    ;
  if (s == EOF)
    exit(0);
  while (isdigit(s))
    k = k * 10 + (s ^ '0'), s = getchar();
  return k;
}
inline void write(int x) {
  static char cnt, num[15];
  cnt = 0;
  if (!x) {
    putchar('0');
    return;
  }
  for (; x; x /= 10)
    num[++cnt] = x % 10;
  for (; cnt; putchar(num[cnt--] + 48))
    ;
}
const int maxn = 1e5 + 100;
int n, m;
ll b, ans[maxn], t[maxn], S;
ll top, s[maxn];
int solve(ll x, int r) {
  int l = 1, res = 0;
  while (l <= r) {
    int mid = (l + r) >> 1;
    if (s[mid] <= x)
      res = mid, l = mid + 1;
    else
      r = mid - 1;
  }
  return res;
}
signed main() {
#ifdef ylx
  freopen("agc003e.in", "r", stdin);
  freopen("agc003e.out", "w", stdout);
#endif
  n = read();
  m = read();
  s[++top] = n;
  while (m--) {
    b = read();
    while (s[top] >= b && top >= 1)
      top--;
    top++;
    s[top] = b;
  }
  m = top;
  t[m] = 1;
  for (int i = m, p; i >= 1; i--) {
    ll k = s[i];
    p = solve(k, i - 1);
    while (p) {
      t[p] += (k / s[p]) * t[i];
      k %= s[p];
      p = solve(k, p - 1);
    }
    ans[1] += t[i];
    ans[k + 1] -= t[i];
  }
  for (int i = 1; i <= n; i++) {
    S += ans[i];
    printf("%lld\n", S);
  }
  return 0;
}
// 2018年 08月 16日 星期四 14:14:17 CST