Problem p03619 (C++)

Buggy submission (status: Runtime Error):
// Why am I so dumb? :ca
#include <bits/stdc++.h>

#define pb push_back
#define mp make_pair

#define all(x) (x).begin(), (x).end()

#define fi first
#define se second

using namespace std;

typedef long long ll;

const double PI = acos(-1.0);
const int INF = (int)1e9;

vector<pair<int, int>> arr;

int ax, ay, bx, by;

double ans;

int n;

double best(vector<pair<int, int>> vv) {
  vector<int> dp(vv.size() + 1, INF);
  dp[0] = -INF;

  for (int i = 0; i < vv.size(); ++i) {
    int p = upper_bound(all(dp), vv[i].se) - dp.begin();

    if (dp[p - 1] < vv[i].se && vv[i].se < dp[p]) {
      dp[p] = vv[i].se;
    }
  }

  int cnt = 1;

  while (cnt <= vv.size() && dp[cnt] != INF) {
    ++cnt;
  }

  return (cnt - 1) * (20.0 - 5.0 * PI);
}

double calc(vector<pair<int, int>> vv) {
  if (vv.empty()) {
    return 0.0;
  }

  double ret = -INF;

  for (int step = 0; step < 2; ++step) {
    sort(all(vv));
    int cnt = 1;

    for (int i = 1; i < vv.size(); ++i) {
      cnt += (vv[i].se > vv[i - 1].se);
    }

    if (cnt == bx - ax + 1) {
      return (20.0 - 5.0 * PI) * (bx - ax) + (20.0 - 10.0 * PI);
    }

    ret = max(ret, best(vv));

    for (pair<int, int> &x : vv) {
      swap(x.fi, x.se);
    }

    swap(ax, ay);
    swap(bx, by);
  }

  return ret;
}

void solve() {
  scanf("%d %d %d %d", &ax, &ay, &bx, &by);
  scanf("%d", &n);

  if (ax > bx) {
    swap(ax, bx);
    swap(ay, by);
  }

  for (int i = 1; i <= n; ++i) {
    pair<int, int> p;
    scanf("%d %d", &p.fi, &p.se);

    if (ax <= p.fi && p.fi <= bx) {
      if (min(ay, by) <= p.se && p.se <= max(ay, by)) {
        arr.pb(p);
      }
    }
  }

  ans = 100.0 * (bx - ax + abs(ay - by));

  if (ax == bx || ay == by) {
    if (!arr.empty()) {
      ans = ans - 20.0 + PI * 10.0;
    }

    printf("%.13f\n", ans);
    return;
  }

  assert(arr.size() <= 5000);

  if (ay > by) {
    swap(ay, by);

    for (pair<int, int> &x : arr) {
      x.se *= -1;
    }
  }

  ans -= calc(arr);
  printf("%.13f\n", ans);
}

int main() {
  int tt = 1;

  while (tt--) {
    solve();
  }

  return 0;
}

Fixed submission:
// Why am I so dumb? :ca
#include <bits/stdc++.h>

#define pb push_back
#define mp make_pair

#define all(x) (x).begin(), (x).end()

#define fi first
#define se second

using namespace std;

typedef long long ll;

const double PI = acos(-1.0);
const int INF = (int)1e9;

vector<pair<int, int>> arr;

int ax, ay, bx, by;

double ans;

int n;

double best(vector<pair<int, int>> vv) {
  vector<int> dp(vv.size() + 1, INF);
  dp[0] = -INF;

  for (int i = 0; i < vv.size(); ++i) {
    int p = upper_bound(all(dp), vv[i].se) - dp.begin();

    if (dp[p - 1] < vv[i].se && vv[i].se < dp[p]) {
      dp[p] = vv[i].se;
    }
  }

  int cnt = 1;

  while (cnt <= vv.size() && dp[cnt] != INF) {
    ++cnt;
  }

  return (cnt - 1) * (20.0 - 5.0 * PI);
}

double calc(vector<pair<int, int>> vv) {
  if (vv.empty()) {
    return 0.0;
  }

  double ret = -INF;

  for (int step = 0; step < 2; ++step) {
    sort(all(vv));
    int cnt = 1;

    for (int i = 1; i < vv.size(); ++i) {
      cnt += (vv[i].se > vv[i - 1].se);
    }

    if (cnt == bx - ax + 1) {
      return (20.0 - 5.0 * PI) * (bx - ax) + (20.0 - 10.0 * PI);
    }

    ret = max(ret, best(vv));

    for (pair<int, int> &x : vv) {
      swap(x.fi, x.se);
    }

    swap(ax, ay);
    swap(bx, by);
  }

  return ret;
}

void solve() {
  scanf("%d %d %d %d", &ax, &ay, &bx, &by);
  scanf("%d", &n);

  if (ax > bx) {
    swap(ax, bx);
    swap(ay, by);
  }

  for (int i = 1; i <= n; ++i) {
    pair<int, int> p;
    scanf("%d %d", &p.fi, &p.se);

    if (ax <= p.fi && p.fi <= bx) {
      if (min(ay, by) <= p.se && p.se <= max(ay, by)) {
        arr.pb(p);
      }
    }
  }

  ans = 100.0 * (bx - ax + abs(ay - by));

  if (ax == bx || ay == by) {
    if (!arr.empty()) {
      ans = ans - 20.0 + PI * 10.0;
    }

    printf("%.13f\n", ans);
    return;
  }

  if (ay > by) {
    swap(ay, by);

    for (pair<int, int> &x : arr) {
      x.se *= -1;
    }
  }

  ans -= calc(arr);
  printf("%.13f\n", ans);
}

int main() {
  int tt = 1;

  while (tt--) {
    solve();
  }

  return 0;
}