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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

#define _crt_secure_no_warnings
#define FREEELO                                                                \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define pb push_back
#define mp make_pair
#define INF 0x3F3F3F3F3F3F3F3F
#define MAX 300001
#define MOD 1000000007
#define ALPHABET 128
#define loop(i, n) for (int i = 1; i <= (n); i++)
#define loop0(i, n) for (int i = 0; i < (n); i++)
#define sz(x) (int)x.size()
#define all(v) v.begin(), v.end()
#define F first
#define S second
using namespace std;
typedef long long ll;
typedef double lf;
typedef pair<ll, ll> llll;
typedef pair<int, int> ii;
typedef unordered_map<int, int> umii;
typedef unordered_map<ll, ll> umll;

int main() {
  int n;
  cin >> n;
  vector<ii> v(n);
  for (auto &i : v)
    cin >> i.F >> i.S;
  int ans = 0;
  map<int, int> diff, sum;
  for (auto [a, b] : v) {
    diff[a - b]++;
    sum[a + b]++;
  }
  auto rem = [&](int v, map<int, int> &mp) {
    int t = --mp[v];
    if (!t)
      mp.erase(v);
  };
  auto read = [&](int v, map<int, int> &mp) { mp[v]++; };
  for (auto [a, b] : v) {
    rem(a - b, diff);
    rem(a + b, sum);
    if (!sum.empty()) {
      int cur = a + b;
      auto iter = sum.begin();
      ans = max(ans, cur - iter->F);
    }
    if (!diff.empty()) {
      int cur = -a + b;
      auto iter = prev(diff.end());
      ans = max(ans, cur + iter->F);
    }
    read(a - b, diff);
    read(a + b, sum);
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

#define _crt_secure_no_warnings
#define FREEELO                                                                \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define pb push_back
#define mp make_pair
#define INF 0x3F3F3F3F3F3F3F3F
#define MAX 300001
#define MOD 1000000007LL
#define ALPHABET 128
#define loop(i, n) for (int i = 1; i <= (n); i++)
#define loop0(i, n) for (int i = 0; i < (n); i++)
#define sz(x) (int)x.size()
#define all(v) v.begin(), v.end()
#define F first
#define S second
using namespace std;
typedef long long ll;
typedef double lf;
typedef pair<ll, ll> llll;
typedef pair<int, int> ii;
typedef unordered_map<int, int> umii;
typedef unordered_map<ll, ll> umll;

int f1(ii a, ii b) { return ll(a.F) + ll(a.S) < ll(b.F) + ll(b.S); }
int f2(ii a, ii b) { return abs(ll(a.F) - ll(a.S)) < abs(ll(b.F) + ll(b.S)); }

ll dist(pair<int, int> a, pair<int, int> b) {
  return abs(b.second - a.second) + abs(b.first - a.first);
}

int main() {
  int n;
  cin >> n;
  vector<ii> a(n), b(n);
  for (auto &i : a)
    cin >> i.F >> i.S;
  b = a;
  sort(all(a), f1);
  sort(all(b), f2);
  ll ans1 = dist(a[0], a[sz(a) - 1]);
  ll ans2 = dist(b[0], b[sz(b) - 1]);
  cout << max(ans1, ans2) << endl;
}