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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;

#define int long long
#define ll long long
#define vi vector<int>
#define vii vector<pair<int, int>>
#define vvi vector<vi>
#define rep(i, a, b) for (int i = a; i < b; i++)
#define repD(i, a, b) for (int i = a; i > b; i--)
#define pb push_back
#define pii pair<int, int>
#define ff first
#define ss second
#define all(a) a.begin(), a.end()
#define MOD 1000000007
#define M_PI 3.14159265358979323846

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    indexed_set;
///*X.find_by_order("index")
/// X.order_of_key("key")

///----------------------------------------------------------------------------------

int f1(int x, int y, int x1, int y1, int mid) { return x + y - x1 - y1 - mid; }

int f2(int x, int y, int x1, int y1, int mid) { return x - y - x1 + y1 - mid; }

vi sum, diff;
int n;
pii pts[200005];

void solve() {

  cin >> n;
  rep(i, 0, n) cin >> pts[i].ff >> pts[i].ss, sum.pb(pts[i].ff + pts[i].ss),
      diff.pb(pts[i].ff - pts[i].ss);
  sort(all(sum));
  sort(all(diff));

  int ans = 0;
  rep(i, 0, n) {
    int lo = 0, hi = 2 * MOD, mid, best = 0;
    while (lo <= hi) {
      mid = (lo + hi) / 2;

      int q1 = sum.end() - lower_bound(all(sum), pts[i].ff + pts[i].ss + mid),
          q2 = diff.end() - lower_bound(all(diff), pts[i].ff - pts[i].ss + mid);
      if (q1 + q2 == 0)
        hi = mid - 1;
      else
        best = mid, lo = mid + 1;
    }
    ans = max(ans, best);
  }
  cout << ans << "\\n";
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

#ifdef local
  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);
  clock_t TIMER = clock();
#endif

  int t = 1;
  // cin >> t;
  rep(a, 0, t) {
    // cout<<"Case #"<<a+1<<": ";
    solve();
#ifdef local
    cout << "\\n";
#endif
  }

#ifdef local
  {
    TIMER = clock() - TIMER;
    double time_taken = ((double)TIMER) / CLOCKS_PER_SEC;
    cerr << fixed << setprecision(5) << "\\n\\nTime Taken : " << time_taken
         << " seconds\\n";
  }
#endif
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;

#define int long long
#define ll long long
#define vi vector<int>
#define vii vector<pair<int, int>>
#define vvi vector<vi>
#define rep(i, a, b) for (int i = a; i < b; i++)
#define repD(i, a, b) for (int i = a; i > b; i--)
#define pb push_back
#define pii pair<int, int>
#define ff first
#define ss second
#define all(a) a.begin(), a.end()
#define MOD 1000000007
#define M_PI 3.14159265358979323846

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    indexed_set;
///*X.find_by_order("index")
/// X.order_of_key("key")

///----------------------------------------------------------------------------------

int f1(int x, int y, int x1, int y1, int mid) { return x + y - x1 - y1 - mid; }

int f2(int x, int y, int x1, int y1, int mid) { return x - y - x1 + y1 - mid; }

struct node {
  int lv, rv;
  int val;
  node *left, *right;
  node(int L, int R) {
    lv = L;
    rv = R;
    left = right = NULL;

    val = 0;
  }
};

void update(node *cur, int ind, int val = 1) {
  if (cur->lv == cur->rv) {
    cur->val += val;
    return;
  }
  int mid = (cur->lv + cur->rv) / 2;

  if (ind <= mid) {
    if (!cur->left)
      cur->left = new node(cur->lv, mid);
    update(cur->left, ind, val);
  } else {
    if (!cur->right)
      cur->right = new node(mid + 1, cur->rv);
    update(cur->right, ind, val);
  }

  cur->val = 0;
  if (cur->left)
    cur->val += cur->left->val;
  if (cur->right)
    cur->val += cur->right->val;
}

int query(node *cur, int l, int r) {
  if (cur->lv > r || cur->rv < l)
    return 0;
  if (cur->lv >= l && cur->rv <= r)
    return cur->val;

  int ret = 0;

  if (cur->left)
    ret += query(cur->left, l, r);

  if (cur->right)
    ret += query(cur->right, l, r);

  return ret;
}

node *sum, *diff;

int n;
pii pts[200005];

void solve() {
  sum = new node(0, 2 * MOD);
  diff = new node(0, 3 * MOD);

  cin >> n;
  rep(i, 0, n) cin >> pts[i].ff >> pts[i].ss,
      update(sum, pts[i].ff + pts[i].ss),
      update(diff, MOD + pts[i].ff - pts[i].ss);

  int ans = 0;
  rep(i, 0, n) {
    int lo = 0, hi = MOD, mid, best = 0;
    while (lo <= hi) {
      mid = (lo + hi) / 2;

      int q1 = query(sum, (pts[i].ff + pts[i].ss + mid), 2 * MOD),
          q2 = query(diff, (pts[i].ff - pts[i].ss + mid) + MOD, 3 * MOD);
      if (q1 + q2 == 0)
        hi = mid - 1;
      else
        best = mid, lo = mid + 1;
    }
    ans = max(ans, best);
  }
  cout << ans << "\\n";
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

#ifdef local
  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);
  clock_t TIMER = clock();
#endif

  int t = 1;
  // cin >> t;
  rep(a, 0, t) {
    // cout<<"Case #"<<a+1<<": ";
    solve();
#ifdef local
    cout << "\\n";
#endif
  }

#ifdef local
  {
    TIMER = clock() - TIMER;
    double time_taken = ((double)TIMER) / CLOCKS_PER_SEC;
    cerr << fixed << setprecision(5) << "\\n\\nTime Taken : " << time_taken
         << " seconds\\n";
  }
#endif
}