Problem p03619 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

#define all(a) (a).begin(), (a).end()
#define pb push_back
#define sz(a) (int)(a).size()
#define mp make_pair

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef long double ld;

const ll inf = 1e18;
const ld pi = 4 * atan(1.0);

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  // ifstream cin("input.txt");
  // freopen("input.txt", "r", stdin);
  // ofstream cout("output.txt");

  ll xs, ys, xt, yt;
  cin >> xs >> ys >> xt >> yt;

  int n;
  cin >> n;

  vector<ll> x(n), y(n);
  for (int i = 0; i < n; ++i) {
    cin >> x[i] >> y[i];
  }

  ld bad = 0;

  vector<pll> v;
  for (int i = 0; i < n; ++i) {
    if (x[i] >= min(xs, xt) && x[i] <= max(xs, xt) && y[i] >= min(ys, yt) &&
        y[i] <= max(ys, yt)) {
      v.pb({x[i], y[i]});
      if (xs == xt) {
        bad = abs(ys - yt) * 100 + (pi - 2) * 10;
        break;
      }
      if (ys == yt) {
        bad = abs(xs - xt) * 100 + (pi - 2) * 10;
        break;
      }
    }
  }

  if (bad != 0) {
    cout << fixed;
    cout.precision(12);
    cout << bad << "\n";
    return 0;
  }

  sort(all(v));

  ll xmin = min(xs, xt);
  ll xmax = max(xs, xt);
  ll ymin = min(ys, yt);
  ll ymax = max(ys, yt);
  /*ll dx = 0, dy = 0;
  bool cons = true;
  if (sz(v) <= 1) {
          cons = false;
  }
  if (sz(v) > 1) {
          dy = v[1].second - v[0].second;
          dx = v[1].first - v[0].first;
  }
  if (abs(dx) != 1) {
          cons = false;
  }
  if (abs(dy) != 1) {
          cons = false;
  }
  if (cons) {
          for (int i = 2; i < sz(v); ++i) {
                  if (v[i].first - v[i - 1].first != dx || v[i].second - v[i -
  1].second != dy) { cons = false;
                  }
          }
  }*/

  bool f = false;
  if (v[0].first == xmin && v.back().first == xmax &&
      sz(v) == xmax - xmin + 1) {
    f = true;
  }
  if (min(v[0].second, v.back().second) == ymin &&
      max(v[0].second, v.back().second) == ymax && sz(v) == ymax - ymin + 1) {
    f = true;
  }

  bool inc;
  if (ys < yt && xs < xt) {
    inc = true;
  } else if (ys < yt) {
    inc = false;
  } else if (ys > yt && xs < xt) {
    inc = false;
  } else {
    inc = true;
  }

  if (!inc) {
    for (int i = 0; i < sz(v); ++i) {
      v[i].second = -v[i].second;
    }
  }

  vector<ll> dp;
  dp.pb(-inf);
  for (int i = 0; i < sz(v); ++i) {
    int lf = 0, rg = sz(dp);
    while (rg - lf > 1) {
      int md = (lf + rg) >> 1;
      if (dp[md] < v[i].second) {
        lf = md;
      } else {
        rg = md;
      }
    }
    if (lf + 1 == sz(dp)) {
      dp.pb(v[i].second);
    } else {
      dp[lf + 1] = v[i].second;
    }
  }

  ll k = sz(dp) - 1;
  // cout << xs << " " << xt << " " << ys << " " << yt << "\n";
  ld ans = abs(xs - xt) + abs(ys - yt);
  ans *= 100;
  ans -= ld(k) * (2 - pi / 2) * 10;

  if (f && sz(v) == k) {
    ans += pi / 2 * 10;
  }

  cout << fixed;
  cout.precision(20);
  cout << ans << "\n";
}


Fixed submission:
#include <bits/stdc++.h>

#define all(a) (a).begin(), (a).end()
#define pb push_back
#define sz(a) (int)(a).size()
#define mp make_pair

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef long double ld;

const ll inf = 1e18;
const ld pi = 4 * atan(1.0);

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  // ifstream cin("input.txt");
  // freopen("input.txt", "r", stdin);
  // ofstream cout("output.txt");

  ll xs, ys, xt, yt;
  cin >> xs >> ys >> xt >> yt;

  int n;
  cin >> n;

  vector<ll> x(n), y(n);
  for (int i = 0; i < n; ++i) {
    cin >> x[i] >> y[i];
  }

  ld bad = 0;

  vector<pll> v;
  for (int i = 0; i < n; ++i) {
    if (x[i] >= min(xs, xt) && x[i] <= max(xs, xt) && y[i] >= min(ys, yt) &&
        y[i] <= max(ys, yt)) {
      v.pb({x[i], y[i]});
      if (xs == xt) {
        bad = abs(ys - yt) * 100 + (pi - 2) * 10;
        break;
      }
      if (ys == yt) {
        bad = abs(xs - xt) * 100 + (pi - 2) * 10;
        break;
      }
    }
  }

  if (bad != 0) {
    cout << fixed;
    cout.precision(12);
    cout << bad << "\n";
    return 0;
  }

  sort(all(v));

  ll xmin = min(xs, xt);
  ll xmax = max(xs, xt);
  ll ymin = min(ys, yt);
  ll ymax = max(ys, yt);
  /*ll dx = 0, dy = 0;
  bool cons = true;
  if (sz(v) <= 1) {
          cons = false;
  }
  if (sz(v) > 1) {
          dy = v[1].second - v[0].second;
          dx = v[1].first - v[0].first;
  }
  if (abs(dx) != 1) {
          cons = false;
  }
  if (abs(dy) != 1) {
          cons = false;
  }
  if (cons) {
          for (int i = 2; i < sz(v); ++i) {
                  if (v[i].first - v[i - 1].first != dx || v[i].second - v[i -
  1].second != dy) { cons = false;
                  }
          }
  }*/

  if (sz(v) == 0) {
    cout << abs(xs - xt) * 100 + abs(ys - yt) * 100 << "\n";
    return 0;
  }

  bool f = false;
  if (v[0].first == xmin && v.back().first == xmax &&
      sz(v) == xmax - xmin + 1) {
    f = true;
  }
  if (min(v[0].second, v.back().second) == ymin &&
      max(v[0].second, v.back().second) == ymax && sz(v) == ymax - ymin + 1) {
    f = true;
  }

  bool inc;
  if (ys < yt && xs < xt) {
    inc = true;
  } else if (ys < yt) {
    inc = false;
  } else if (ys > yt && xs < xt) {
    inc = false;
  } else {
    inc = true;
  }

  if (!inc) {
    for (int i = 0; i < sz(v); ++i) {
      v[i].second = -v[i].second;
    }
  }

  vector<ll> dp;
  dp.pb(-inf);
  for (int i = 0; i < sz(v); ++i) {
    int lf = 0, rg = sz(dp);
    while (rg - lf > 1) {
      int md = (lf + rg) >> 1;
      if (dp[md] < v[i].second) {
        lf = md;
      } else {
        rg = md;
      }
    }
    if (lf + 1 == sz(dp)) {
      dp.pb(v[i].second);
    } else {
      dp[lf + 1] = v[i].second;
    }
  }

  ll k = sz(dp) - 1;
  // cout << xs << " " << xt << " " << ys << " " << yt << "\n";
  ld ans = abs(xs - xt) + abs(ys - yt);
  ans *= 100;
  ans -= ld(k) * (2 - pi / 2) * 10;

  if (f && sz(v) == k) {
    ans += pi / 2 * 10;
  }

  cout << fixed;
  cout.precision(20);
  cout << ans << "\n";
}