Problem p04023 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

int N, Q;
vector<ll> query, stk;
set<pair<ll, ll>> st;
ll ans[100010];

void print() {
  cout << "print\n";
  for (set<pair<ll, ll>>::iterator it = st.begin(); it != st.end(); it++) {
    cout << it->first << ' ' << it->second << endl;
  }
}
void Back_add(ll x) {
  set<pair<ll, ll>>::iterator it = st.end();
  it--;

  pair<ll, ll> tmp = *it;
  st.erase(st.find(tmp));
  tmp.second += x;
  st.insert(tmp);
}
void Part_add(pair<ll, ll> x) {
  set<pair<ll, ll>>::iterator it = st.lower_bound(make_pair(x.first, 0));
  if (it != st.end() && it->first == x.first) {
    pair<ll, ll> tmp = *it;
    st.erase(st.find(tmp));
    x.second += tmp.second;
    st.insert(x);
  } else
    st.insert(x);
}

int main() {
  scanf("%d %d", &N, &Q);

  query.resize(Q);
  for (int i = 0; i < Q; i++) {
    scanf("%lld", &query[i]);
  }

  stk.push_back(N);
  for (int i = 0; i < Q; i++) {
    while (stk.size() && stk.back() >= query[i])
      stk.pop_back();
    stk.push_back(query[i]);
  }

  query = stk;
  Q = query.size();

  st.insert(make_pair(query.back(), 1));

  int pos = Q - 1;
  while (pos) {
    int nxt = pos;
    while (nxt >= 1 && query[nxt - 1] * 2 >= query[pos])
      nxt--;

    // cout << query[pos] << endl;
    // print();

    if (pos == nxt) {
      nxt = pos - 1;
      Part_add(make_pair(query[nxt], 0));

      ll cur = (query[pos] - 1) / query[nxt] * query[nxt];
      ll sum = 0;

      while (1) {
        set<pair<ll, ll>>::iterator it = st.end();
        it--;

        pair<ll, ll> tmp = *it;

        ll succ = (it->first - 1) / query[nxt] * query[nxt];

        if (succ < cur) {
          Part_add(make_pair(cur, 0));
          Back_add(sum);
          Part_add(make_pair(query[nxt], (cur - succ - 1) / query[nxt] * sum));
          sum = 0;
          cur = succ;
        }

        if (tmp.first <= query[nxt])
          break;

        it = st.end();
        it--;
        tmp = *it;
        st.erase(st.find(tmp));

        tmp.first %= query[nxt];
        if (tmp.first == 0)
          tmp.first = query[nxt];
        Part_add(tmp);

        sum += tmp.second;
      }

      pos = nxt;
      continue;
    }

    for (int i = pos - 1; i >= nxt; i--) {
      Part_add(make_pair(query[i], 0));

      ll sum = 0;
      while (1) {
        set<pair<ll, ll>>::iterator it = st.end();
        it--;

        if (it->first <= query[i])
          break;

        pair<ll, ll> tmp = *it;
        st.erase(st.find(tmp));

        tmp.first -= query[i];
        if (tmp.first == 0)
          tmp.first = query[nxt];

        sum += tmp.second;

        Part_add(tmp);
      }

      Back_add(sum);
    }

    pos = nxt;
  }

  // print();

  for (set<pair<ll, ll>>::iterator it = st.begin(); it != st.end(); it++) {
    ans[it->first] = it->second;
  }

  for (int i = N; i >= 1; i--) {
    ans[i] += ans[i + 1];
  }
  for (int i = 1; i <= N; i++) {
    printf("%lld\n", ans[i]);
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

int N, Q;
vector<ll> query, stk;
set<pair<ll, ll>> st;
ll ans[100010];

void print() {
  cout << "print\n";
  for (set<pair<ll, ll>>::iterator it = st.begin(); it != st.end(); it++) {
    cout << it->first << ' ' << it->second << endl;
  }
}
void Back_add(ll x) {
  set<pair<ll, ll>>::iterator it = st.end();
  it--;

  pair<ll, ll> tmp = *it;
  st.erase(st.find(tmp));
  tmp.second += x;
  st.insert(tmp);
}
void Part_add(pair<ll, ll> x) {
  set<pair<ll, ll>>::iterator it = st.lower_bound(make_pair(x.first, 0));
  if (it != st.end() && it->first == x.first) {
    pair<ll, ll> tmp = *it;
    st.erase(st.find(tmp));
    x.second += tmp.second;
    st.insert(x);
  } else
    st.insert(x);
}

int main() {
  scanf("%d %d", &N, &Q);

  query.resize(Q);
  for (int i = 0; i < Q; i++) {
    scanf("%lld", &query[i]);
  }

  stk.push_back(N);
  for (int i = 0; i < Q; i++) {
    while (stk.size() && stk.back() >= query[i])
      stk.pop_back();
    stk.push_back(query[i]);
  }

  query = stk;
  Q = query.size();

  st.insert(make_pair(query.back(), 1));

  int pos = Q - 1;
  while (pos) {
    int nxt = pos;
    while (nxt >= 1 && query[nxt - 1] * 2 >= query[pos])
      nxt--;

    // cout << query[pos] << endl;
    // print();

    if (pos == nxt) {
      nxt = pos - 1;
      Part_add(make_pair(query[nxt], 0));

      ll cur = (query[pos] - 1) / query[nxt] * query[nxt];
      ll sum = 0;

      while (1) {
        set<pair<ll, ll>>::iterator it = st.end();
        it--;

        pair<ll, ll> tmp = *it;

        ll succ = (it->first - 1) / query[nxt] * query[nxt];

        if (succ < cur) {
          Part_add(make_pair(succ + query[nxt], 0));
          Back_add(sum);
          Part_add(make_pair(query[nxt], (cur - succ - 1) / query[nxt] * sum));
          sum = 0;
          cur = succ;
        }

        if (tmp.first <= query[nxt])
          break;

        it = st.end();
        it--;
        tmp = *it;
        st.erase(st.find(tmp));

        tmp.first %= query[nxt];
        if (tmp.first == 0)
          tmp.first = query[nxt];
        Part_add(tmp);

        sum += tmp.second;
      }

      pos = nxt;
      continue;
    }

    for (int i = pos - 1; i >= nxt; i--) {
      Part_add(make_pair(query[i], 0));

      ll sum = 0;
      while (1) {
        set<pair<ll, ll>>::iterator it = st.end();
        it--;

        if (it->first <= query[i])
          break;

        pair<ll, ll> tmp = *it;
        st.erase(st.find(tmp));

        tmp.first -= query[i];
        if (tmp.first == 0)
          tmp.first = query[nxt];

        sum += tmp.second;

        Part_add(tmp);
      }

      Back_add(sum);
    }

    pos = nxt;
  }

  // print();

  for (set<pair<ll, ll>>::iterator it = st.begin(); it != st.end(); it++) {
    ans[it->first] = it->second;
  }

  for (int i = N; i >= 1; i--) {
    ans[i] += ans[i + 1];
  }
  for (int i = 1; i <= N; i++) {
    printf("%lld\n", ans[i]);
  }
}