Problem p03619 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define double long double
#define mp make_pair
#define fi first
#define se second
using namespace std;
const int N = 4e5 + 10;
const double pi = acos(-1);
typedef long long ll;
typedef pair<int, int> pii;
int gi() {
  int x = 0, o = 1;
  char ch = getchar();
  while ((ch < '0' || ch > '9') && ch != '-')
    ch = getchar();
  if (ch == '-')
    o = -1, ch = getchar();
  while (ch >= '0' && ch <= '9')
    x = x * 10 + ch - '0', ch = getchar();
  return x * o;
}
int c[N], st[N], tp = 0, f[N];
void modify(int p, int v) {
  while (p <= tp)
    c[p] = max(c[p], v), p += p & -p;
}
int query(int p) {
  int ret = -1e9;
  while (p)
    ret = max(ret, c[p]), p -= p & -p;
  return ret;
}
pii a[N];
int main() {
  freopen("c.in", "r", stdin);
  freopen("c.out", "w", stdout);
  int n, x1, y1, x2, y2;
  x1 = gi(), y1 = gi(), x2 = gi(), y2 = gi(), n = gi();
  for (int i = 1; i <= n; i++)
    a[i].fi = gi(), a[i].se = gi();
  if (x1 > x2) {
    x1 = -x1, x2 = -x2;
    for (int i = 1; i <= n; i++)
      a[i].fi = -a[i].fi;
  }
  if (y1 > y2) {
    y1 = -y1, y2 = -y2;
    for (int i = 1; i <= n; i++)
      a[i].se = -a[i].se;
  }
  memset(c, 0xc0, sizeof(c));
  a[++n] = mp(x1, y1), a[++n] = mp(x2, y2);
  sort(a + 1, a + n + 1);
  for (int i = 1; i <= n; i++)
    st[++tp] = a[i].se;
  sort(st + 1, st + tp + 1);
  tp = unique(st + 1, st + tp + 1) - st - 1;
  for (int i = 1; i <= n; i++)
    a[i].se = lower_bound(st + 1, st + tp + 1, a[i].se) - st;
  double ans = x2 - x1 + y2 - y1;
  ans *= 100.0;
  int mn = min(x2 - x1 + 1, y2 - y1 + 1);
  y1 = lower_bound(st + 1, st + tp + 1, y1) - st;
  y2 = lower_bound(st + 1, st + tp + 1, y2) - st;
  int cnt = 0;
  for (int i = 1; i <= n; i++) {
    f[i] = query(a[i].se) + 1;
    if (a[i] == mp(x1, y1))
      f[i] = 0;
    else if (a[i] == mp(x2, y2))
      cnt = f[i] - 1;
    modify(a[i].se, f[i]);
  }
  ans += (pi - 4) * 5 * cnt;
  if (cnt == mn)
    ans += pi * 5;
  printf("%.20Lf\n", ans);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define double long double
#define mp make_pair
#define fi first
#define se second
using namespace std;
const int N = 4e5 + 10;
const double pi = acos(-1);
typedef long long ll;
typedef pair<int, int> pii;
int gi() {
  int x = 0, o = 1;
  char ch = getchar();
  while ((ch < '0' || ch > '9') && ch != '-')
    ch = getchar();
  if (ch == '-')
    o = -1, ch = getchar();
  while (ch >= '0' && ch <= '9')
    x = x * 10 + ch - '0', ch = getchar();
  return x * o;
}
int c[N], st[N], tp = 0, f[N];
void modify(int p, int v) {
  while (p <= tp)
    c[p] = max(c[p], v), p += p & -p;
}
int query(int p) {
  int ret = -1e9;
  while (p)
    ret = max(ret, c[p]), p -= p & -p;
  return ret;
}
pii a[N];
int main() {
  int n, x1, y1, x2, y2;
  x1 = gi(), y1 = gi(), x2 = gi(), y2 = gi(), n = gi();
  for (int i = 1; i <= n; i++)
    a[i].fi = gi(), a[i].se = gi();
  if (x1 > x2) {
    x1 = -x1, x2 = -x2;
    for (int i = 1; i <= n; i++)
      a[i].fi = -a[i].fi;
  }
  if (y1 > y2) {
    y1 = -y1, y2 = -y2;
    for (int i = 1; i <= n; i++)
      a[i].se = -a[i].se;
  }
  memset(c, 0xc0, sizeof(c));
  a[++n] = mp(x1, y1), a[++n] = mp(x2, y2);
  sort(a + 1, a + n + 1);
  for (int i = 1; i <= n; i++)
    st[++tp] = a[i].se;
  sort(st + 1, st + tp + 1);
  tp = unique(st + 1, st + tp + 1) - st - 1;
  for (int i = 1; i <= n; i++)
    a[i].se = lower_bound(st + 1, st + tp + 1, a[i].se) - st;
  double ans = x2 - x1 + y2 - y1;
  ans *= 100.0;
  int mn = min(x2 - x1 + 1, y2 - y1 + 1);
  y1 = lower_bound(st + 1, st + tp + 1, y1) - st;
  y2 = lower_bound(st + 1, st + tp + 1, y2) - st;
  int cnt = 0;
  for (int i = 1; i <= n; i++) {
    f[i] = query(a[i].se) + 1;
    if (a[i] == mp(x1, y1))
      f[i] = 0;
    else if (a[i] == mp(x2, y2))
      cnt = f[i] - 1;
    modify(a[i].se, f[i]);
  }
  ans += (pi - 4) * 5 * cnt;
  if (cnt == mn)
    ans += pi * 5;
  printf("%.20Lf\n", ans);
  return 0;
}