Problem p02556 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>

// using namespace __gnu_pbds;
using namespace std;

#define pb push_back
#define mp make_pair
#define mt make_tuple
#define ff first
#define ss second
#define null 0
#define pi 3.14159265358979323846
#define all(x) x.begin(), x.end()
#define d0(x) cout << x << " ";
#define d1(x) cout << x << "\n";
#define d2(x, y) cout << x << " " << y << "\n";
#define d3(x, y, z) cout << x << " " << y << " " << z << "\n";
#define d4(x, y, z, w) cout << x << " " << y << " " << z << " " << w << "\n";

// #define ordered_set tree<pair<ll,ll>, null_type,less<pair<ll,ll>>,
// rb_tree_tag,tree_order_statistics_node_update> #define ordered_set tree<int,
// null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>

template <typename T, typename U> static inline void amin(T &x, U y) {
  if (y < x)
    x = y;
}
template <typename T, typename U> static inline void amax(T &x, U y) {
  if (y > x)
    x = y;
}

typedef long long int ll;
typedef unsigned long long int ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const ll mod = 1000000007;
const long long inf = (ll)1e18 + 17;
const int inff = 1500000000;
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};
ll tree[100010], aa[200010];
void build(int node, int start, int end) {
  if (start == end) {
    tree[node] = aa[start];
  } else {
    int mid = (start + end) / 2;
    build(2 * node, start, mid);
    build(2 * node + 1, mid + 1, end);
    tree[node] = max(tree[2 * node], tree[2 * node + 1]);
  }
}

void update(int node, int start, int end, int idx, int val) {
  if (start == end) {
    aa[idx] = val;
    tree[node] = val;
  } else {
    int mid = (start + end) / 2;
    if (start <= idx and idx <= mid) {
      update(2 * node, start, mid, idx, val);
    } else {
      update(2 * node + 1, mid + 1, end, idx, val);
    }
    tree[node] = max(tree[2 * node], tree[2 * node + 1]);
  }
}

ll query(int node, int start, int end, int l, int r) {
  if (r < start or end < l) {
    return -1;
  }
  if (l <= start and end <= r) {
    return tree[node];
  }
  int mid = (start + end) / 2;
  ll p1 = query(2 * node, start, mid, l, r);
  ll p2 = query(2 * node + 1, mid + 1, end, l, r);
  return max(p1, p2);
}

ll n;
pair<pair<ll, ll>, ll> a[200010], b[200010];

int test_case = 0;
void solve() {
  cin >> n;
  vector<pll> ok;
  for (int i = 0; i < n; i++) {
    int x, y;
    cin >> x >> y;
    ok.pb({x, y});
    a[i] = {{x, y}, i};
    b[i] = {{y, x}, i};
  }
  int ai[n + 10], bi[n + 10];
  sort(a, a + n);
  sort(b, b + n);
  for (int i = 0; i < n; i++)
    ai[a[i].ss] = i, bi[a[i].ss] = i;
  for (int i = 0; i < n; i++)
    aa[i] = a[i].ff.ff + a[i].ff.ss;
  build(1, 0, n - 1);
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    int j = b[i].ss;
    j = ai[j];
    ll op = query(1, 0, n - 1, j, n - 1);
    amax(ans, op - b[i].ff.ff - b[i].ff.ss);
    update(1, 0, n - 1, j, -1);
  }
  for (int i = 0; i < n; i++) {
    int x = ok[i].ff, y = ok[i].ss;
    y = 1000000001 - y;
    a[i] = {{x, y}, i};
    b[i] = {{y, x}, i};
  }
  sort(a, a + n);
  sort(b, b + n);
  for (int i = 0; i < n; i++)
    ai[a[i].ss] = i, bi[a[i].ss] = i;
  for (int i = 0; i < n; i++)
    aa[i] = a[i].ff.ff + a[i].ff.ss;
  build(1, 0, n - 1);
  for (int i = 0; i < n; i++) {
    int j = b[i].ss;
    j = ai[j];
    ll op = query(1, 0, n - 1, j, n - 1);
    amax(ans, op - b[i].ff.ff - b[i].ff.ss);
    update(1, 0, n - 1, j, -1);
  }
  cout << ans << endl;
}

int main() {
  clock_t tt;
  tt = clock();
  // ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
  int t = 1;
  if (test_case == 1)
    cin >> t;
  // pre();
  for (int i = 1; i <= t; i++) {
    // cout<<"Case #"<<i<<": ";
    solve();
  }
  tt = clock() - tt;
  double time_taken = ((double)tt) / CLOCKS_PER_SEC;
  // printf("code took %f seconds to execute \n", time_taken);
  return 0;
}

// ============== Notes =============== //
// common bugs
// - remove ios_base for interactive problems
// - int overflow (especially intermediate expressionns)
// - array bounds (indices bigger than MAXN?)
// - special cases (n=1? graph not connected?)
// - re-initialize efficiently between test cases


Fixed submission:
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>

// using namespace __gnu_pbds;
using namespace std;

#define pb push_back
#define mp make_pair
#define mt make_tuple
#define ff first
#define ss second
#define null 0
#define pi 3.14159265358979323846
#define all(x) x.begin(), x.end()
#define d0(x) cout << x << " ";
#define d1(x) cout << x << "\n";
#define d2(x, y) cout << x << " " << y << "\n";
#define d3(x, y, z) cout << x << " " << y << " " << z << "\n";
#define d4(x, y, z, w) cout << x << " " << y << " " << z << " " << w << "\n";

// #define ordered_set tree<pair<ll,ll>, null_type,less<pair<ll,ll>>,
// rb_tree_tag,tree_order_statistics_node_update> #define ordered_set tree<int,
// null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>

template <typename T, typename U> static inline void amin(T &x, U y) {
  if (y < x)
    x = y;
}
template <typename T, typename U> static inline void amax(T &x, U y) {
  if (y > x)
    x = y;
}

typedef long long int ll;
typedef unsigned long long int ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const ll mod = 1000000007;
const long long inf = (ll)1e18 + 17;
const int inff = 1500000000;
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};
ll tree[1000010], aa[200010];
void build(int node, int start, int end) {
  if (start == end) {
    tree[node] = aa[start];
  } else {
    int mid = (start + end) / 2;
    build(2 * node, start, mid);
    build(2 * node + 1, mid + 1, end);
    tree[node] = max(tree[2 * node], tree[2 * node + 1]);
  }
}

void update(int node, int start, int end, int idx, int val) {
  if (start == end) {
    aa[idx] = val;
    tree[node] = val;
  } else {
    int mid = (start + end) / 2;
    if (start <= idx and idx <= mid) {
      update(2 * node, start, mid, idx, val);
    } else {
      update(2 * node + 1, mid + 1, end, idx, val);
    }
    tree[node] = max(tree[2 * node], tree[2 * node + 1]);
  }
}

ll query(int node, int start, int end, int l, int r) {
  if (r < start or end < l) {
    return -1;
  }
  if (l <= start and end <= r) {
    return tree[node];
  }
  int mid = (start + end) / 2;
  ll p1 = query(2 * node, start, mid, l, r);
  ll p2 = query(2 * node + 1, mid + 1, end, l, r);
  return max(p1, p2);
}

ll n;
pair<pair<ll, ll>, ll> a[200010], b[200010];

int test_case = 0;
void solve() {
  cin >> n;
  vector<pll> ok;
  for (int i = 0; i < n; i++) {
    int x, y;
    cin >> x >> y;
    ok.pb({x, y});
    a[i] = {{x, y}, i};
    b[i] = {{y, x}, i};
  }
  int ai[n + 10], bi[n + 10];
  sort(a, a + n);
  sort(b, b + n);
  for (int i = 0; i < n; i++)
    ai[a[i].ss] = i, bi[a[i].ss] = i;
  for (int i = 0; i < n; i++)
    aa[i] = a[i].ff.ff + a[i].ff.ss;
  build(1, 0, n - 1);
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    int j = b[i].ss;
    j = ai[j];
    ll op = query(1, 0, n - 1, j, n - 1);
    amax(ans, op - b[i].ff.ff - b[i].ff.ss);
    update(1, 0, n - 1, j, -1);
  }
  for (int i = 0; i < n; i++) {
    int x = ok[i].ff, y = ok[i].ss;
    y = 1000000001 - y;
    a[i] = {{x, y}, i};
    b[i] = {{y, x}, i};
  }
  sort(a, a + n);
  sort(b, b + n);
  for (int i = 0; i < n; i++)
    ai[a[i].ss] = i, bi[a[i].ss] = i;
  for (int i = 0; i < n; i++)
    aa[i] = a[i].ff.ff + a[i].ff.ss;
  build(1, 0, n - 1);
  for (int i = 0; i < n; i++) {
    int j = b[i].ss;
    j = ai[j];
    ll op = query(1, 0, n - 1, j, n - 1);
    amax(ans, op - b[i].ff.ff - b[i].ff.ss);
    update(1, 0, n - 1, j, -1);
  }
  cout << ans << endl;
}

int main() {
  clock_t tt;
  tt = clock();
  // ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
  int t = 1;
  if (test_case == 1)
    cin >> t;
  // pre();
  for (int i = 1; i <= t; i++) {
    // cout<<"Case #"<<i<<": ";
    solve();
  }
  tt = clock() - tt;
  double time_taken = ((double)tt) / CLOCKS_PER_SEC;
  // printf("code took %f seconds to execute \n", time_taken);
  return 0;
}

// ============== Notes =============== //
// common bugs
// - remove ios_base for interactive problems
// - int overflow (especially intermediate expressionns)
// - array bounds (indices bigger than MAXN?)
// - special cases (n=1? graph not connected?)
// - re-initialize efficiently between test cases