Problem p04023 (C++)

Buggy submission (status: Runtime Error):
// Achen
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define For(i, a, b) for (int i = (a); i <= (b); i++)
#define Rep(i, a, b) for (int i = (a); i >= (b); i--)
const int N = 2e5 + 7;
typedef long long LL;
typedef double db;
using namespace std;
LL n, q;
LL a[N], cnt[N], ans[N];

template <typename T> void read(T &x) {
  char ch = getchar();
  x = 0;
  T f = 1;
  while (ch != '-' && (ch < '0' || ch > '9'))
    ch = getchar();
  if (ch == '-')
    f = -1, ch = getchar();
  for (; ch >= '0' && ch <= '9'; ch = getchar())
    x = x * 10 + ch - '0';
  x *= f;
}

LL sg[N << 2];
#define lc x << 1
#define rc ((x << 1) | 1)
#define mid ((l + r) >> 1)
void build(int x, int l, int r) {
  if (l == r) {
    sg[x] = a[l];
    return;
  }
  build(lc, l, mid);
  build(rc, mid + 1, r);
  sg[x] = min(sg[lc], sg[rc]);
}

LL qry(int x, int l, int r, int ql, int qr, LL v) {
  if (ql > qr)
    return 0;
  if (l >= ql && r <= qr) {
    if (sg[x] > v)
      return 0;
    if (l == r)
      return l;
    if (sg[rc] <= v)
      return qry(rc, mid + 1, r, ql, qr, v);
    if (sg[lc] <= v)
      return qry(lc, l, mid, ql, qr, v);
  }
  if (qr <= mid)
    return qry(lc, l, mid, ql, qr, v);
  LL rs = qry(rc, mid + 1, r, ql, qr, v);
  if (rs)
    return rs;
  else
    return qry(lc, l, mid, ql, qr, v);
}

LL cf[N];
void solve(int x, LL l, LL c) {
  if (!x || !l) {
    if (!x)
      cf[l] += c;
    return;
  }
  int y = qry(1, 1, q, 1, x - 1, l);
  cnt[y] += l / a[y] * c;
  solve(y, l % a[y], c);
}

// #define DEBUG
int main() {
#ifdef DEBUG
  freopen("1.in", "r", stdin);
  // freopen(".out","w",stdout);
#endif
  read(n);
  read(q);
  a[0] = n;
  For(i, 1, q) read(a[i]);
  build(1, 1, q);
  cnt[q] = 1;
  Rep(i, q, 1) if (cnt[i]) solve(i, a[i], cnt[i]);
  ans[n + 1] = cnt[0];
  Rep(i, n, 1) ans[i] = ans[i + 1] + cf[i];
  For(i, 1, n) printf("%lld\n", ans[i]);
  return 0;
}

Fixed submission:
// Achen
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define For(i, a, b) for (int i = (a); i <= (b); i++)
#define Rep(i, a, b) for (int i = (a); i >= (b); i--)
const int N = 2e5 + 7;
typedef long long LL;
typedef double db;
using namespace std;
LL n, q;
LL a[N], cnt[N], ans[N];

template <typename T> void read(T &x) {
  char ch = getchar();
  x = 0;
  T f = 1;
  while (ch != '-' && (ch < '0' || ch > '9'))
    ch = getchar();
  if (ch == '-')
    f = -1, ch = getchar();
  for (; ch >= '0' && ch <= '9'; ch = getchar())
    x = x * 10 + ch - '0';
  x *= f;
}

LL sg[N << 2];
#define lc x << 1
#define rc ((x << 1) | 1)
#define mid ((l + r) >> 1)
void build(int x, int l, int r) {
  if (l == r) {
    sg[x] = a[l];
    return;
  }
  build(lc, l, mid);
  build(rc, mid + 1, r);
  sg[x] = min(sg[lc], sg[rc]);
}

LL qry(int x, int l, int r, int ql, int qr, LL v) {
  if (ql > qr)
    return 0;
  if (l >= ql && r <= qr) {
    if (sg[x] > v)
      return 0;
    if (l == r)
      return l;
    if (sg[rc] <= v)
      return qry(rc, mid + 1, r, ql, qr, v);
    if (sg[lc] <= v)
      return qry(lc, l, mid, ql, qr, v);
  }
  if (qr <= mid)
    return qry(lc, l, mid, ql, qr, v);
  LL rs = qry(rc, mid + 1, r, ql, qr, v);
  if (rs)
    return rs;
  else
    return qry(lc, l, mid, ql, qr, v);
}

LL cf[N];
void solve(int x, LL l, LL c) {
  if (!x || !l) {
    if (!x)
      cf[l] += c;
    return;
  }
  int y = qry(1, 1, q, 1, x - 1, l);
  cnt[y] += l / a[y] * c;
  solve(y, l % a[y], c);
}

// #define DEBUG
int main() {
#ifdef DEBUG
  freopen("1.in", "r", stdin);
  // freopen(".out","w",stdout);
#endif
  read(n);
  read(q);
  a[0] = n;
  if (q == 0) {
    For(i, 1, n) puts("1");
    return 0;
  }
  For(i, 1, q) read(a[i]);
  build(1, 1, q);
  cnt[q] = 1;
  Rep(i, q, 1) if (cnt[i]) solve(i, a[i], cnt[i]);
  ans[n + 1] = cnt[0];
  Rep(i, n, 1) ans[i] = ans[i + 1] + cf[i];
  For(i, 1, n) printf("%lld\n", ans[i]);
  return 0;
}