Problem p01885 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#define rep(i, x, n) for (int i = x; i < n; i++)
#define per(i, x, n) for (int i = n - 1; i >= x; i--)
using namespace std;
#define ll long long
// head
int n, L;
int l, r, mid;
struct Bottle {
  int up, down, val;
  bool operator<(const Bottle a) const { return val > a.val; }
} a[100005];
ll b[100005], c[100005], sum[100005];
bool check(int x) {
  x--;
  rep(k, 1, n + 1) {
    if (c[k] <= x)
      continue;
    ll p = L - a[k].up, go = 0;
    if (k <= x && sum[x + 1] - a[k].val >= p)
      return 1;
    if (k > x && sum[x] >= p)
      return 1;
  }
  return 0;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int mx = 0;
  cin >> n >> L;
  rep(i, 1, n + 1) cin >> a[i].up >> a[i].down, a[i].val = a[i].up - a[i].down,
                                                mx = max(a[i].val, mx);
  if (mx >= L) {
    cout << 1 << endl;
    return 0;
  }
  rep(i, 1, n + 1) cin >> b[i];
  sort(a + 1, a + n + 1);
  rep(i, 1, n + 1) sum[i] = sum[i - 1] + a[i].val;
  ll cnt = 1, dis = 0;
  rep(i, 2, n + 1) {
    dis += (a[i].val - b[cnt]);
    if (dis <= 0)
      break;
    cnt++;
  }
  c[1] = cnt;
  int k = 2;
  for (; k <= n; k++) {
    if (cnt < k - 1 || cnt == n)
      break;
    dis += (a[k - 1].val - a[k].val);
    while (dis > 0 && cnt < n)
      cnt++, dis += (a[cnt + 1].val - b[cnt]);
    c[k] = cnt;
  }
  for (; k <= n; k++)
    c[k] = cnt;
  int day = n;
  rep(i, 1, n + 1) if (a[i].val <= 0) {
    day = i;
    break;
  } // for(int i=1;a[i].val>0&&i<=n;i++,day++);
  l = 1, r = day;
  if (l > r) {
    cout << -1 << endl;
    return 0;
  }
  while (l < r) {
    mid = (l + r) / 2;
    if (check(mid))
      r = mid;
    else
      l = mid + 1;
  }
  if (check(l))
    cout << l << endl;
  else
    cout << -1 << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
using namespace std;
typedef long long ll;
const ll inf = 1e15;
const int N = 111111 << 2;
struct pt {
  ll x, y;
  pt() {}
  friend bool operator<(pt a, pt b) { return a.x - a.y > b.x - b.y; }
};

ll mn[N], tag[N];
ll c[N], L, b[N];
int tot;
void pd(int rt) {
  if (tag[rt]) {
    mn[rt << 1] += tag[rt];
    mn[rt << 1 | 1] += tag[rt];
    tag[rt << 1] += tag[rt];
    tag[rt << 1 | 1] += tag[rt];
    tag[rt] = 0;
  }
}
void up(int rt) { mn[rt] = min(mn[rt << 1], mn[rt << 1 | 1]); }
void build(int l, int r, int rt) {
  if (l == r) {
    mn[rt] = c[l];
    return;
  }
  int m = l + r >> 1;
  build(lson);
  build(rson);
  up(rt);
}

void add(int L, int R, ll x, int l, int r, int rt) {
  if (L <= l && r <= R) {
    mn[rt] += x;
    tag[rt] += x;
    return;
  }
  pd(rt);
  int m = l + r >> 1;
  if (L <= m)
    add(L, R, x, lson);
  if (R > m)
    add(L, R, x, rson);
  up(rt);
}

ll ask(int L, int R, int l, int r, int rt) {
  if (L <= l && r <= R)
    return mn[rt];
  pd(rt);
  int m = l + r >> 1;
  ll ans = inf;
  if (L <= m)
    ans = min(ans, ask(L, R, lson));
  if (R > m)
    ans = min(ans, ask(L, R, rson));
  return ans;
}

int n;
pt a[N];
int ans = 1e8;
void doit(int i) {
  // if (b[n] < L - a[i].x) return;
  int ps = lower_bound(b + 1, b + n + 1, L - a[i].x) - b;
  if (ps >= i) {
    // if (b[n] < L - a[i].y) return;
    ps = lower_bound(b + 1, b + n + 1, L - a[i].y) - b - 1;
  }
  // if (ps > tot) return;
  // cout << ask(1, 4, 1, tot, 1) << endl;
  // cout << i << "  " << ps << endl;
  if (ask(1, ps, 1, tot, 1) > 0)
    ans = min(ans, ps);
}
ll ct[N];
int main() {
  scanf("%d%lld", &n, &L);
  for (int i = 1; i <= n; ++i)
    scanf("%lld%lld", &a[i].x, &a[i].y);
  for (int i = 1; i <= n; ++i)
    scanf("%lld", &ct[i]);
  sort(a + 1, a + n + 1);

  tot = n - 1;
  for (int i = 1; i <= n; ++i)
    b[i] = a[i].x - a[i].y;

  /*for (int i = 1; i <= n; ++i) {
          cout << a[i].x << " " << a[i].y << endl;
  }*/

  for (int i = 1; i <= tot; ++i)
    c[i] = c[i - 1] + b[i + 1] - ct[i];
  /*
  for (int i = 1; i <= tot; ++i) {
          cout << c[i] << endl;
  }*/

  build(1, tot, 1);

  for (int i = 1; i <= n; ++i)
    b[i] += b[i - 1];

  /*	for (int i = 1; i <= n; ++i) {
                  cout << b[i] << endl;
          }*/

  doit(1);
  for (int i = 2; i <= n; ++i) {
    add(i - 1, tot, -(a[i].x - a[i].y), 1, tot, 1);
    add(i - 1, tot, a[i - 1].x - a[i - 1].y, 1, tot, 1);
    doit(i);
  }

  if (ans == 1e8 || ans == n)
    puts("-1");
  else
    printf("%d\\n", ans + 1);
  return 0;
}