Problem p02556 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using namespace std::chrono;
#define idfc                                                                   \\
  ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
//:/
#define pb push_back
#define mp make_pair
#define nt _ll128
#define ld double
long double PI = 3.14159265;
using ll = long long;

const ll modo = 1e9 + 7;
const ll ms = 2e5 + 5;
const ll inf = 1e17;

vector<pair<ll, ll>> vv;

map<ll, ll> mm;
map<ll, ll> m2;

int main() {
  idfc;

  ll n;
  cin >> n;
  ll i;
  ll a, b;

  for (i = 0; i < n; i++) {
    cin >> a >> b;
    vv.pb({a, b});
  }
  sort(vv.begin(), vv.end());

  mm[vv[0].second] = vv[0].first + vv[0].second;
  m2[vv[0].second] = vv[0].first - vv[0].second;

  ll ans = 0;
  ll x;
  for (i = 1; i < n; i++) {
    a = vv[i].first;
    b = vv[i].second;
    auto it = mm.upper_bound(b);
    if (it != mm.begin()) {
      it--;
      x = a + b - ((*it).second);
      ans = max(ans, x);
    }
    auto ii = m2.lower_bound(b);
    if (ii != m2.end()) {
      x = (a - b) - ((*ii).second);
      ans = max(ans, x);
    }

    while (!mm.empty() && mm.lower_bound(b) != mm.end()) {
      x = (*mm.lower_bound(b)).second;
      if (x >= (a + b)) {
        mm.erase((*mm.lower_bound(b)).first);
      } else
        break;
    }
    if (mm.empty()) {
      mm[b] = a + b;
    } else {
      it = mm.upper_bound(b);
      if (it != mm.begin()) {
        it--;
        if ((*it).second > (a + b)) {
          mm[b] = a + b;
        }
      } else {
        mm[b] = a + b;
      }
    }

    while (!m2.empty() && m2.upper_bound(b) != m2.begin()) {
      auto ip = m2.upper_bound(b);
      ip--;
      x = (*ip).second;
      if (x >= (a - b)) {
        m2.erase((*ip).first);
      } else
        break;
    }

    if (m2.empty()) {
      m2[b] = a - b;
    } else {
      it = m2.lower_bound(b);
      if (it != m2.end()) {
        if ((*it).second > (a - b)) {
          m2[b] = a - b;
        }
      } else
        m2[b] = a - b;
    }
  }
  cout << ans;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using namespace std::chrono;
#define idfc                                                                   \\
  ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
//:/
#define pb push_back
#define mp make_pair
#define nt _ll128
#define ld double
long double PI = 3.14159265;
using ll = long long;

const ll modo = 1e9 + 7;
const ll ms = 2e5 + 5;
const ll inf = 1e17;

vector<pair<ll, ll>> vv;

map<int, ll> mm;
map<int, ll> m2;

int main() {
  idfc;

  int n;
  cin >> n;
  int i;
  ll a, b;

  for (i = 0; i < n; i++) {
    cin >> a >> b;
    vv.pb({a, b});
  }
  sort(vv.begin(), vv.end());

  mm[vv[0].second] = vv[0].first + vv[0].second;
  m2[vv[0].second] = vv[0].first - vv[0].second;

  ll ans = 0;
  ll x;
  for (i = 1; i < n; i++) {
    a = vv[i].first;
    b = vv[i].second;
    auto it = mm.upper_bound(b);
    if (it != mm.begin()) {
      it--;
      x = a + b - ((*it).second);
      ans = max(ans, x);
    }
    auto ii = m2.lower_bound(b);
    if (ii != m2.end()) {
      x = (a - b) - ((*ii).second);
      ans = max(ans, x);
    }

    while (!mm.empty() && mm.lower_bound(b) != mm.end()) {
      x = (*mm.lower_bound(b)).second;
      if (x >= (a + b)) {
        mm.erase((*mm.lower_bound(b)).first);
      } else
        break;
    }
    if (!mm.count(b)) {
      mm[b] = a + b;
    }

    while (!m2.empty() && m2.upper_bound(b) != m2.begin()) {
      auto ip = m2.upper_bound(b);
      ip--;
      x = (*ip).second;
      if (x >= (a - b)) {
        m2.erase((*ip).first);
      }
    }

    if (!m2.count(b))
      m2[b] = a - b;
  }
  cout << ans;
  return 0;
}