Problem p03934 (C++)

Buggy submission (status: Runtime Error):
// start:
// finish:
#include <bits/stdc++.h>
#define maxn 100005
using namespace std;
typedef long long ll;
ll a[maxn * 4], flag[maxn];
int n, Q;
void down(int x, int l, int r) {
  int mid = (l + r) / 2;
  if (!flag[x])
    return;
  flag[x * 2] = flag[x];
  flag[x * 2 + 1] = flag[x];
  a[x * 2] = 1ll * flag[x] * (mid - l + 1);
  a[x * 2 + 1] = 1ll * flag[x] * (r - mid);
  flag[x] = 0;
}
ll query(int x, int l, int r, int tx, int ty) {
  if (l >= tx && r <= ty)
    return a[x];
  down(x, l, r);
  ll ret = 0, mid = (l + r) >> 1;
  if (mid >= tx)
    ret += query(x * 2, l, mid, tx, ty);
  if (mid < ty)
    ret += query(x * 2 + 1, mid + 1, r, tx, ty);
  return ret;
}
void modify(int x, int l, int r, int tx, int ty, ll val) {
  if (l >= tx && r <= ty) {
    flag[x] = val;
    a[x] = 1ll * (r - l + 1) * val;
    return;
  }
  int mid = (l + r) / 2;
  down(x, l, r);
  if (mid >= tx)
    modify(x * 2, l, mid, tx, ty, val);
  if (mid < ty)
    modify(x * 2 + 1, mid + 1, r, tx, ty, val);
  a[x] = a[x * 2] + a[x * 2 + 1];
}

int main() {
  scanf("%d%d", &n, &Q);
  while (Q--) {
    int x;
    ll y;
    scanf("%d%lld", &x, &y);
    if (x == 1) {
      modify(1, 1, n, 1, 1, query(1, 1, n, x, x) + y);
      continue;
    }
    if (query(1, 1, n, x - 1, x - 1) - query(1, 1, n, x, x) >= y) {
      modify(1, 1, n, x, x, query(1, 1, n, x, x) + y);
      continue;
    }
    int l = 2, r = x, pos;
    while (l <= r) {
      int mid = (l + r) / 2;
      if (query(1, 1, n, mid - 1, mid - 1) * (x - mid + 1) -
              query(1, 1, n, mid, x) <=
          y) {
        pos = mid;
        r = mid - 1;
      } else {
        l = mid + 1;
      }
    }
    //	cout<<" "<<pos<<" "<<query(1,1,n,1,1)<<endl;
    ll val = query(1, 1, n, pos - 1, pos - 1);
    ll he = query(1, 1, n, pos, x);
    modify(1, 1, n, pos, x, val);
    y -= val * (x - pos + 1) - he;
    int num = x - pos + 2;
    if (y / num) {
      modify(1, 1, n, pos - 1, x, val + y / num);
    }
    if (y % num) {
      modify(1, 1, n, pos - 1, pos - 2 + y % num, val + y / num + 1);
    }
  }
  for (int i = 1; i <= n; i++) {
    printf("%lld\n", query(1, 1, n, i, i));
  }
  return 0;
}

Fixed submission:
// start:
// finish:
#include <bits/stdc++.h>
#define maxn 100005
using namespace std;
typedef long long ll;
ll a[maxn * 4], flag[maxn * 4];
int n, Q;
void down(int x, int l, int r) {
  int mid = (l + r) / 2;
  if (!flag[x])
    return;
  flag[x * 2] = flag[x];
  flag[x * 2 + 1] = flag[x];
  a[x * 2] = 1ll * flag[x] * (mid - l + 1);
  a[x * 2 + 1] = 1ll * flag[x] * (r - mid);
  flag[x] = 0;
}
ll query(int x, int l, int r, int tx, int ty) {
  if (l >= tx && r <= ty)
    return a[x];
  down(x, l, r);
  ll ret = 0, mid = (l + r) >> 1;
  if (mid >= tx)
    ret += query(x * 2, l, mid, tx, ty);
  if (mid < ty)
    ret += query(x * 2 + 1, mid + 1, r, tx, ty);
  return ret;
}
void modify(int x, int l, int r, int tx, int ty, ll val) {
  if (l >= tx && r <= ty) {
    flag[x] = val;
    a[x] = 1ll * (r - l + 1) * val;
    return;
  }
  int mid = (l + r) / 2;
  down(x, l, r);
  if (mid >= tx)
    modify(x * 2, l, mid, tx, ty, val);
  if (mid < ty)
    modify(x * 2 + 1, mid + 1, r, tx, ty, val);
  a[x] = a[x * 2] + a[x * 2 + 1];
}

int main() {
  scanf("%d%d", &n, &Q);
  while (Q--) {
    int x;
    ll y;
    scanf("%d%lld", &x, &y);
    if (x == 1) {
      modify(1, 1, n, 1, 1, query(1, 1, n, x, x) + y);
      continue;
    }
    if (query(1, 1, n, x - 1, x - 1) - query(1, 1, n, x, x) >= y) {
      modify(1, 1, n, x, x, query(1, 1, n, x, x) + y);
      continue;
    }
    int l = 2, r = x, pos;
    while (l <= r) {
      int mid = (l + r) / 2;
      if (query(1, 1, n, mid - 1, mid - 1) * (x - mid + 1) -
              query(1, 1, n, mid, x) <=
          y) {
        pos = mid;
        r = mid - 1;
      } else {
        l = mid + 1;
      }
    }
    //	cout<<" "<<pos<<" "<<query(1,1,n,1,1)<<endl;
    ll val = query(1, 1, n, pos - 1, pos - 1);
    ll he = query(1, 1, n, pos, x);
    modify(1, 1, n, pos, x, val);
    y -= val * (x - pos + 1) - he;
    int num = x - pos + 2;
    if (y / num) {
      modify(1, 1, n, pos - 1, x, val + y / num);
    }
    if (y % num) {
      modify(1, 1, n, pos - 1, pos - 2 + y % num, val + y / num + 1);
    }
  }
  for (int i = 1; i <= n; i++) {
    printf("%lld\n", query(1, 1, n, i, i));
  }
  return 0;
}