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


// Problem : E - Dist Max
// Contest : AtCoder - AtCoder Beginner Contest 178
// URL : https://atcoder.jp/contests/abc178/tasks/abc178_e
// Memory Limit : 1024 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

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

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef map<int, int> mii;
typedef map<int, string> mis;
typedef map<string, int> msi;
typedef set<int> si;
typedef set<ll> sl;
typedef map<ll, ll> mll;
typedef queue<int> qi;
typedef queue<ii> qii;
typedef vector<string> vs;
typedef pair<ll, ll> iil;
typedef priority_queue<int> pqi;
typedef priority_queue<ii> pqii;
typedef priority_queue<ll> pqil;
typedef priority_queue<iil> pqiil;
typedef vector<iil> viil;
typedef vector<vi> vvi;
typedef long double ld;
typedef pair<int, ii> iii;
typedef pair<iil, ll> iiil;
typedef vector<pair<pair<int, int>, int>> viii;
typedef vector<pair<pair<ll, ll>, ll>> viiil;
typedef vector<vl> vvl;

#define pb push_back
#define mp make_pair
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF 2000000000

ll MOD = 1000000007;

ll dir(ii a, ii b, ii c) {
  return (ll)a.first * (b.second - c.second) +
         (ll)b.first * (c.second - a.second) +
         (ll)c.first * (a.second - b.second);
}

ll ds(ii a, ii b) {
  return ((ll)abs(a.first - b.first)) + abs(a.second - b.second);
}

void solve() {

  int n;
  cin >> n;

  map<ii, bool> seen;

  vii c;
  rep(i, n) {
    int x, y;
    cin >> x >> y;
    if (!seen[mp(x, y)]) {
      c.pb(mp(x, y));
      seen[mp(x, y)] = true;
    }
  }

  sort(c.begin(), c.end());

  if (c.size() <= 1) {
    cout << 0 << endl;
    return;
  }

  if (c.size() == 2) {
    cout << abs(c[0].first - c[1].first) + abs(c[0].second - c[1].second)
         << endl;
    return;
  }

  vi bot;
  bot.pb(0);
  bot.pb(1);

  for (int i = 2; i < c.size(); i++) {
    bot.pb(i);
    while (bot.size() >= 3 &&
           dir(c[bot[bot.size() - 3]], c[bot[bot.size() - 2]],
               c[bot[bot.size() - 1]]) < 0) {
      bot.pop_back();
      bot.pop_back();
      bot.pb(i);
    }
  }

  vi top;
  top.pb(0);
  top.pb(1);

  for (int i = 2; i < c.size(); i++) {
    top.pb(i);
    while (top.size() >= 3 &&
           dir(c[top[top.size() - 3]], c[top[top.size() - 2]],
               c[top[top.size() - 1]]) > 0) {
      top.pop_back();
      top.pop_back();
      top.pb(i);
    }
  }

  vi h;
  rep(i, bot.size()) h.pb(bot[i]);
  for (int i = top.size() - 2; i >= 1; i--) {
    h.pb(top[i]);
  }

  ll mx = 0;

  int nm = h.size();

  rep(i, nm) {
    int l = i + 1;
    int r = i + n - 1;
    int ct = 0;
    while (abs(l - r) > 1) {
      // cout << l << " " << r << endl;
      int m1 = l + (r - l) / 3;
      int m2 = l + ((r - l) * 2) / 3;
      ll d1 = ds(c[h[i]], c[h[m1 % nm]]);
      ll d2 = ds(c[h[i]], c[h[m2 % nm]]);
      if (d1 < d2) {
        l = m1 + 1;
      } else {
        r = m2 - 1;
      }
      ct++;
    }

    mx = max(mx, ds(c[h[i]], c[h[l % nm]]));
    mx = max(mx, ds(c[h[i]], c[h[r % nm]]));
  }

  cout << mx << endl;
}

void querySolve() {
  int t;
  cin >> t;
  for (int i = 0; i < t; i++) {
    solve();
  }
}

int main() {
  solve();
  // querySolve();
}


Error: 0

Fixed submission:


// Problem : E - Dist Max
// Contest : AtCoder - AtCoder Beginner Contest 178
// URL : https://atcoder.jp/contests/abc178/tasks/abc178_e
// Memory Limit : 1024 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

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

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef map<int, int> mii;
typedef map<int, string> mis;
typedef map<string, int> msi;
typedef set<int> si;
typedef set<ll> sl;
typedef map<ll, ll> mll;
typedef queue<int> qi;
typedef queue<ii> qii;
typedef vector<string> vs;
typedef pair<ll, ll> iil;
typedef priority_queue<int> pqi;
typedef priority_queue<ii> pqii;
typedef priority_queue<ll> pqil;
typedef priority_queue<iil> pqiil;
typedef vector<iil> viil;
typedef vector<vi> vvi;
typedef long double ld;
typedef pair<int, ii> iii;
typedef pair<iil, ll> iiil;
typedef vector<pair<pair<int, int>, int>> viii;
typedef vector<pair<pair<ll, ll>, ll>> viiil;
typedef vector<vl> vvl;

#define pb push_back
#define mp make_pair
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF 2000000000

ll MOD = 1000000007;

int dir(ii a, ii b, ii c) {
  return a.first * (b.second - c.second) + b.first * (c.second - a.second) +
         c.first * (a.second - b.second);
}

ll ds(ii a, ii b) {
  return ((ll)abs(a.first - b.first)) + abs(a.second - b.second);
}

void solve() {

  int n;
  cin >> n;

  map<ii, bool> seen;

  vii c;
  rep(i, n) {
    int x, y;
    cin >> x >> y;
    if (!seen[mp(x, y)]) {
      c.pb(mp(x, y));
      seen[mp(x, y)] = true;
    }
  }

  sort(c.begin(), c.end());

  if (c.size() <= 1) {
    cout << 0 << endl;
    return;
  }

  if (c.size() == 2) {
    cout << abs(c[0].first - c[1].first) + abs(c[0].second - c[1].second)
         << endl;
    return;
  }

  vi bot;
  bot.pb(0);
  bot.pb(1);

  for (int i = 2; i < n; i++) {
    bot.pb(i);
    while (bot.size() >= 3 &&
           dir(c[bot[bot.size() - 3]], c[bot[bot.size() - 2]],
               c[bot[bot.size() - 1]]) < 0) {
      bot.pop_back();
      bot.pop_back();
      bot.pb(i);
    }
  }

  vi top;
  top.pb(0);
  top.pb(1);

  for (int i = 2; i < n; i++) {
    top.pb(i);
    while (top.size() >= 3 &&
           dir(c[top[top.size() - 3]], c[top[top.size() - 2]],
               c[top[top.size() - 1]]) > 0) {
      top.pop_back();
      top.pop_back();
      top.pb(i);
    }
  }

  vi h;
  rep(i, bot.size()) h.pb(bot[i]);
  for (int i = top.size() - 2; i >= 1; i--) {
    h.pb(top[i]);
  }

  ll mx = 0;

  int nm = h.size();

  rep(i, nm) {
    int l = i + 1;
    int r = i + n - 1;
    int ct = 0;
    while (abs(l - r) > 1) {
      // cout << l << " " << r << endl;
      int m1 = l + (r - l) / 3;
      int m2 = l + ((r - l) * 2) / 3;
      ll d1 = ds(c[h[i]], c[h[m1 % nm]]);
      ll d2 = ds(c[h[i]], c[h[m2 % nm]]);
      if (d1 < d2) {
        l = m1 + 1;
      } else {
        r = m2 - 1;
      }
      ct++;
    }

    mx = max(mx, ds(c[h[i]], c[h[l % nm]]));
    mx = max(mx, ds(c[h[i]], c[h[r % nm]]));
  }

  cout << mx << endl;
}

void querySolve() {
  int t;
  cin >> t;
  for (int i = 0; i < t; i++) {
    solve();
  }
}

int main() {
  solve();
  // querySolve();
}