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

#include <bits/stdc++.h>

#define all(v) v.begin(), v.end()
#define sz(x) (int)x.size()
#define debug(x) cout << #x << ':' << x << endl;
#define rep(i, n) for (long long i = 0; i < n; i++)
#define reps(i, n) for (long long i = 1; i <= n; i++)
#define make_unique(v)                                                         \\
  sort(all(v));                                                                \\
  v.erase(unique(all(v)), v.end());
#define REMOVE(v, a) v.erase(remove(v.begin(), v.end(), a), v.end());
#define pb push_back
#define mp make_pair
#define endl '\\n'
#define fi first
#define se second
#define ajs(a) (a < 0) ? (-1 * a) : a

using namespace std;
typedef long long ll;
typedef vector<long long> vll;
typedef pair<ll, ll> pll;
const ll MOD = 1000000007;
bool cmp(int X, int Y) { return X > Y; }

ll gcd(ll a, ll b) { return !b ? a : gcd(b, a % b); }
ll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; }
ll modpow(ll a, ll b, ll m = MOD) {
  a %= m;
  ll res = 1;
  while (b) {
    if (b & 1)
      res = (res * a) % m;
    a = (a * a) % m;
    b >>= 1;
  }
  return res;
}
ll bpow(ll a, ll b) {
  if (b < 0) {
    return 0;
  }
  ll res = 1;
  while (b) {
    if (b & 1)
      res = res * a;
    a = a * a;
    b >>= 1;
  }
  return res;
}

ll ncr(ll n, ll r) {
  if (r > n - r) {
    r = n - r;
  }
  if (r == 1) {
    return n;
  }
  if (r == 0) {
    return 1;
  }

  if (r == n) {
    return 1;
  }

  return ((n * ncr(n - 1, r - 1)) / r) % MOD;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ll n;
  cin >> n;
  ll a[n];
  ll b[n];
  rep(i, n) {
    ll x, y;
    cin >> x >> y;
    a[i] = x + y;
    b[i] = x - y;
  }
  sort(a, a + n);
  sort(b, b + n);

  ll ans = max(a[n - 1] - a[0], b[n - 1] - b[0]);
  cout << ans;
}


Error: TLE

Fixed submission:

#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
#include <bits/stdc++.h>

#define all(v) v.begin(), v.end()
#define sz(x) (int)x.size()
#define debug(x) cout << #x << ':' << x << endl;
#define rep(i, n) for (long long i = 0; i < n; i++)
#define reps(i, n) for (long long i = 1; i <= n; i++)
#define make_unique(v)                                                         \\
  sort(all(v));                                                                \\
  v.erase(unique(all(v)), v.end());
#define REMOVE(v, a) v.erase(remove(v.begin(), v.end(), a), v.end());
#define pb push_back
#define mp make_pair
#define endl '\\n'
#define fi first
#define se second
#define ajs(a) (a < 0) ? (-1 * a) : a

using namespace std;
typedef long long ll;
typedef vector<long long> vll;
typedef pair<ll, ll> pll;
const ll MOD = 1e9 + 7;
bool cmp(int X, int Y) { return X > Y; }

ll gcd(ll a, ll b) { return !b ? a : gcd(b, a % b); }
ll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; }
ll modpow(ll a, ll b, ll m = MOD) {
  a %= m;
  ll res = 1;
  while (b) {
    if (b & 1)
      res = (res * a) % m;
    a = (a * a) % m;
    b >>= 1;
  }
  return res;
}
ll bpow(ll a, ll b) {
  if (b < 0) {
    return 0;
  }
  ll res = 1;
  while (b) {
    if (b & 1)
      res = res * a;
    a = a * a;
    b >>= 1;
  }
  return res;
}

ll ncr(ll n, ll r) {
  if (r > n - r) {
    r = n - r;
  }
  if (r == 1) {
    return n;
  }
  if (r == 0) {
    return 1;
  }

  if (r == n) {
    return 1;
  }

  return ((n * ncr(n - 1, r - 1)) / r) % MOD;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ll n;
  cin >> n;
  vector<pll> v;
  rep(i, n) {
    ll x, y;
    cin >> x >> y;
    v.pb({x, y});
  }
  ll ans = 0;
  for (ll i = 0; i < n - 1; i++) {
    for (ll j = i + 1; j < n; j++) {
      ll k = abs(v[i].fi - v[j].fi) + abs(v[i].se - v[j].se);
      ans = max(ans, k);
    }
  }
  cout << ans;
}

/*ll n;cin>>n;
if(n==1){
cout<<0<<endl;
return 0;
}

ll k = n-2;
ll ans = modpow(10,k,MOD);
ans%=MOD;
debug(ans)
ll sum = (n*(n-1));
sum=(sum)%MOD;
ans = ans*sum;

ans = (ans+MOD)%MOD;
cout<<ans;
* */