Problem p03934 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <set>
using namespace std;
const int M = (int)1e5 + 5;
const long long INF = (long long)1e17 + 7;
template <class T> void Rd(T &res) {
  res = 0;
  static char p;
  while (p = getchar(), p < '0')
    ;
  do {
    res = (res * 10) + (p ^ 48);
  } while (p = getchar(), p >= '0');
}
template <class T> void Ps(T x) {
  static int stk[65], sz;
  sz = 0;
  while (x)
    stk[sz++] = x % 10, x /= 10;
  if (sz == 0)
    stk[sz++] = 0;
  while (sz--)
    putchar(stk[sz] ^ 48);
}
int n, q;
struct W {
  int l, r;
  long long v;
  bool operator<(const W &a) const {
    if (r != a.r)
      return r < a.r;
    if (l != a.l)
      return l < a.l;
    return v < a.v;
  }
} A, B;
set<W> st;
set<W>::iterator it, it1;
int main() {
  Rd(n), Rd(q);
  long long x, y, ned;
  st.insert((W){0, 0, INF});
  st.insert((W){1, n, 0});
  for (int i = 1; i <= q; i++) {
    Rd(x), Rd(y);
    while (y) {
      it = st.lower_bound((W){0, (int)x, 0ll});
      A = (*it);
      st.erase(it);
      if (A.r > x)
        st.insert((W){(int)x + 1, A.r, A.v}), A.r = x;
      it1 = st.lower_bound((W){-1, A.l - 1, 0ll});
      B = (*it1);
      ned = (A.r - A.l + 1) * (B.v - A.v);
      if (ned <= y) {
        st.erase(it1);
        B.r = A.r;
        st.insert(B);
        y -= ned;
      } else {
        ned = A.r - A.l + 1;
        if (y % ned == 0) {
          A.v += y / ned;
          st.insert(A);
        } else {
          B.l = A.l, B.r = B.l + y % ned - 1, B.v = A.v + y / ned + 1;
          st.insert(B);
          A.l = B.r + 1, A.v = B.v - 1;
          st.insert(A);
        }
        y = 0;
      }
    }
  }
  for (it = ++st.begin(), it1 = st.end(); it != it1; it++)
    for (int j = (*it).l; j <= (*it).r; j++)
      Ps((*it).v), putchar('\n');
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <set>
using namespace std;
const int M = (int)1e5 + 5;
const long long INF = (long long)1e17 + 7;
template <class T> void Rd(T &res) {
  res = 0;
  static char p;
  while (p = getchar(), p < '0')
    ;
  do {
    res = (res * 10) + (p ^ 48);
  } while (p = getchar(), p >= '0');
}
template <class T> void Ps(T x) {
  static int stk[65], sz;
  sz = 0;
  while (x)
    stk[sz++] = x % 10, x /= 10;
  if (sz == 0)
    stk[sz++] = 0;
  while (sz--)
    putchar(stk[sz] ^ 48);
}
int n, q;
struct W {
  int l, r;
  long long v;
  bool operator<(const W &a) const {
    if (r != a.r)
      return r < a.r;
    if (l != a.l)
      return l < a.l;
    return v < a.v;
  }
} A, B;
set<W> st;
set<W>::iterator it, it1;
int main() {
  Rd(n), Rd(q);
  long long x, y, ned;
  st.insert((W){0, 0, INF});
  st.insert((W){1, n, 0});
  for (int i = 1; i <= q; i++) {
    Rd(x), Rd(y);
    while (y) {
      it = st.lower_bound((W){0, (int)x, 0ll});
      A = (*it);
      st.erase(it);
      if (A.r > x)
        st.insert((W){(int)x + 1, A.r, A.v}), A.r = x;
      it1 = st.lower_bound((W){-1, A.l - 1, 0ll});
      B = (*it1);
      if (B.v != INF)
        ned = (A.r - A.l + 1) * (B.v - A.v);
      else
        ned = INF;
      if (ned < 0)
        ned = INF;
      if (ned <= y) {
        st.erase(it1);
        B.r = A.r;
        st.insert(B);
        y -= ned;
      } else {
        ned = A.r - A.l + 1;
        if (y % ned == 0) {
          A.v += y / ned;
          st.insert(A);
        } else {
          B.l = A.l, B.r = B.l + y % ned - 1, B.v = A.v + y / ned + 1;
          st.insert(B);
          A.l = B.r + 1, A.v = B.v - 1;
          st.insert(A);
        }
        y = 0;
      }
    }
  }
  for (it = ++st.begin(), it1 = st.end(); it != it1; it++)
    for (int j = (*it).l; j <= (*it).r; j++)
      Ps((*it).v), putchar('\n');
  return 0;
}