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

#include <bits/stdc++.h>
#define MAX_N 1111111
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const ll INF = 0;

int n;
ll dat[2 * MAX_N - 1];

void init(int n_) {
  n = 1;
  while (n < n_)
    n *= 2;
  for (int i = 0; i < 2 * n - 1; i++)
    dat[i] = INF;
}

void update(int k, ll a) {
  k += n - 1;
  dat[k] = a;
  while (k > 0) {
    k = (k - 1) / 2;
    dat[k] = dat[k * 2 + 1] + dat[k * 2 + 2];
  }
}

ll query(int a, int b, int k, int l, int r) {
  if (b <= a)
    return 0;
  if (r <= a || b <= l)
    return INF;
  if (a <= l && r <= b)
    return dat[k];
  else {
    ll vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
    ll vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
    return vl + vr;
  }
}

int main(void) {
  ll T;
  int N;
  cin >> T >> N;
  vector<P> s(N);
  vector<P> t;
  init(N);
  for (int i = 0; i < N; i++) {
    cin >> s[i].first >> s[i].second;
    update(i, s[i].second - s[i].first);
  }
  for (int i = 0; i < N; i++) {
    t.push_back(P(s[i].first, 0));
    t.push_back(P(s[i].second, 1));
  }

  ll ans = 0;

  for (int s_ind = 0; s_ind < N; s_ind++) {
    int idx =
        upper_bound(t.begin(), t.end(), P(s[s_ind].first + T, 0)) - t.begin();
    if (idx == 2 * N)
      idx--;
    else if (t[idx].second == 1) {
      idx -= 2;
    } else {
      idx -= 1;
    }
    //   idx = max(0, idx+1);
    ll r = query(s_ind, (idx + 1) / 2, 0, 0, n);

    ans = max(ans, r + (idx + 1 >= 2 * N
                            ? 0
                            : max(0ll, s[s_ind].first + T - t[idx + 1].first)));
  }

  cout << ans << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define MAX_N 111111
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const ll INF = 0;

int n;
ll dat[2 * MAX_N - 1];

void init(int n_) {
  n = 1;
  while (n < n_)
    n *= 2;
  for (int i = 0; i < 2 * n - 1; i++)
    dat[i] = INF;
}

void update(int k, ll a) {
  k += n - 1;
  dat[k] = a;
  while (k > 0) {
    k = (k - 1) / 2;
    dat[k] = dat[k * 2 + 1] + dat[k * 2 + 2];
  }
}

ll query(int a, int b, int k, int l, int r) {
  if (b <= a)
    return 0;
  if (r <= a || b <= l)
    return INF;
  if (a <= l && r <= b)
    return dat[k];
  else {
    ll vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
    ll vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
    return vl + vr;
  }
}

int main(void) {
  ll T;
  int N;
  cin >> T >> N;
  vector<P> s(N);
  vector<P> t;
  init(N);
  for (int i = 0; i < N; i++) {
    cin >> s[i].first >> s[i].second;
    update(i, s[i].second - s[i].first);
  }
  for (int i = 0; i < N; i++) {
    t.push_back(P(s[i].first, 0));
    t.push_back(P(s[i].second, 1));
  }

  ll ans = 0;

  for (int s_ind = 0; s_ind < N; s_ind++) {
    int idx =
        upper_bound(t.begin(), t.end(), P(s[s_ind].first + T, 0)) - t.begin();
    if (idx == 2 * N)
      idx--;
    else if (t[idx].second == 1) {
      idx -= 2;
    } else {
      idx -= 1;
    }
    //   idx = max(0, idx+1);
    ll r = query(s_ind, (idx + 1) / 2, 0, 0, n);

    ans = max(ans, r + (idx + 1 >= 2 * N
                            ? 0
                            : max(0ll, s[s_ind].first + T - t[idx + 1].first)));
  }

  cout << ans << endl;

  return 0;
}