Problem p04023 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#define all(a) a.begin(), a.end()
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define pb push_back
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> P;
typedef complex<ld> com;
constexpr int inf = 1000000010;
constexpr ll INF = 1000000000000000010;
constexpr ld eps = 1e-12;
constexpr ld pi = 3.141592653589793238;
template <class T, class U> inline bool chmax(T &a, const U &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T, class U> inline bool chmin(T &a, const U &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

template <class T> class segtree {
  int n;
  vector<T> data;
  T id = P(INF, -1);
  T operation(T a, T b) {
    if (a.first < b.first)
      return P(a.first, a.second);
    return P(b.first, b.second);
  }

public:
  segtree(int _n) {
    n = 1;
    while (n < _n + 2)
      n <<= 1;
    data = vector<T>(2 * n, id);
    rep(i, 2 * n) data[i].second = i;
  }
  void change(int i, T x) {
    i += n;
    data[i] = x;
    while (i > 1) {
      i >>= 1;
      data[i] = operation(data[i << 1], data[i << 1 | 1]);
    }
  }
  void add(int i, T x) { change(i, data[i + n] + x); }
  T get(int a, int b) {
    T left = id;
    T right = id;
    a += n;
    b += n;
    while (a < b) {
      if (a & 1)
        left = operation(left, data[a++]);
      if (b & 1)
        right = operation(data[--b], right);
      a >>= 1;
      b >>= 1;
    }
    return operation(left, right);
  }
  T get_all() { return data[1]; }
  T operator[](int i) { return data[i + n]; }
};

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(20);

  int n, q;
  cin >> n >> q;
  vector<ll> a(q);
  rep(i, q) cin >> a[i];
  segtree<P> seg(q + 1);
  seg.change(0, P(n, 0));
  rep(i, q) seg.change(i + 1, P(a[i], i + 1));
  vector<ll> use;
  int idx = 0;
  while (idx <= q) {
    P p = seg.get(idx, n + 2);
    use.pb(p.first);
    idx = p.second + 1;
  }
  reverse(all(use));
  priority_queue<P> que;
  que.push(P(use[0], 1));
  for (int i = 1; i < use.size(); i++) {
    map<ll, ll> mp;
    while (!que.empty()) {
      P p = que.top();
      if (p.first < use[i])
        break;
      que.pop();
      mp[use[i]] += p.first / use[i] * p.second;
      if (p.first % use[i])
        mp[p.first % use[i]] += p.second;
    }
    for (auto j : mp)
      que.push(P(j.first, j.second));
  }
  vector<ll> ans(n + 2);
  while (!que.empty()) {
    P p = que.top();
    que.pop();
    ans[1] += p.second;
    ans[p.first + 1] -= p.second;
  }
  for (int i = 1; i <= n; i++)
    ans[i] += ans[i - 1];
  for (int i = 1; i <= n; i++)
    cout << ans[i] << '\n';
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#define all(a) a.begin(), a.end()
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define pb push_back
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> P;
typedef complex<ld> com;
constexpr int inf = 1000000010;
constexpr ll INF = 1000000000000000010;
constexpr ld eps = 1e-12;
constexpr ld pi = 3.141592653589793238;
template <class T, class U> inline bool chmax(T &a, const U &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T, class U> inline bool chmin(T &a, const U &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

template <class T> class segtree {
  int n;
  vector<T> data;
  T id = P(INF, -1);
  T operation(T a, T b) {
    if (a.first < b.first)
      return P(a.first, a.second);
    return P(b.first, b.second);
  }

public:
  segtree(int _n) {
    n = 1;
    while (n < _n + 2)
      n <<= 1;
    data = vector<T>(2 * n, id);
    rep(i, 2 * n) data[i].second = i;
  }
  void change(int i, T x) {
    i += n;
    data[i] = x;
    while (i > 1) {
      i >>= 1;
      data[i] = operation(data[i << 1], data[i << 1 | 1]);
    }
  }
  void add(int i, T x) { change(i, data[i + n] + x); }
  T get(int a, int b) {
    T left = id;
    T right = id;
    a += n;
    b += n;
    while (a < b) {
      if (a & 1)
        left = operation(left, data[a++]);
      if (b & 1)
        right = operation(data[--b], right);
      a >>= 1;
      b >>= 1;
    }
    return operation(left, right);
  }
  T get_all() { return data[1]; }
  T operator[](int i) { return data[i + n]; }
};

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(20);

  int n, q;
  cin >> n >> q;
  vector<ll> a(q);
  rep(i, q) cin >> a[i];
  segtree<P> seg(q + 1);
  seg.change(0, P(n, 0));
  rep(i, q) seg.change(i + 1, P(a[i], i + 1));
  vector<ll> use;
  int idx = 0;
  while (idx <= q) {
    P p = seg.get(idx, q + 2);
    use.pb(p.first);
    idx = p.second + 1;
  }
  reverse(all(use));
  priority_queue<P> que;
  que.push(P(use[0], 1));
  for (int i = 1; i < use.size(); i++) {
    map<ll, ll> mp;
    while (!que.empty()) {
      P p = que.top();
      if (p.first < use[i])
        break;
      que.pop();
      mp[use[i]] += p.first / use[i] * p.second;
      if (p.first % use[i])
        mp[p.first % use[i]] += p.second;
    }
    for (auto j : mp)
      que.push(P(j.first, j.second));
  }
  vector<ll> ans(n + 2);
  while (!que.empty()) {
    P p = que.top();
    que.pop();
    ans[1] += p.second;
    ans[p.first + 1] -= p.second;
  }
  for (int i = 1; i <= n; i++)
    ans[i] += ans[i - 1];
  for (int i = 1; i <= n; i++)
    cout << ans[i] << '\n';
}