Problem p03619 (C++)

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

typedef long long ll;
#define REP(i, n) for (int i = 0, _n = (int)(n); i < _n; ++i)
#define ALL(v) (v).begin(), (v).end()
template <class T1, class T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &a) {
  return os << "(" << a.first << "," << a.second << ")";
}
template <class T> void pv(T a, T b) {
  for (T i = a; i != b; ++i)
    cout << (*i) << " ";
  cout << endl;
}
template <class T> bool chmin(T &a, const T &b) {
  return a > b ? (a = b, 1) : 0;
}
template <class T> bool chmax(T &a, const T &b) {
  return a < b ? (a = b, 1) : 0;
}

const double PI = acos(-1);
ll nextLong() {
  ll x;
  scanf("%lld", &x);
  return x;
}
const int MAX_N = 200000 + 10;
vector<pair<ll, ll>> p(MAX_N);

void normalize(vector<pair<ll, ll>> &v, int N) {
  vector<pair<ll, int>> t(N + 2);
  {
    REP(i, N) {
      t[i].first = v[i].first;
      t[i].second = i;
    }
    sort(ALL(t));
    int r = 0;
    REP(i, N) {
      r += (i > 0 && t[i - 1].first != t[i].first);
      v[t[i].second].first = r;
    }
  }
  {
    REP(i, N) {
      t[i].first = v[i].second;
      t[i].second = i;
    }
    sort(ALL(t));
    int r = 0;
    REP(i, N) {
      r += (i > 0 && t[i - 1].first != t[i].first);
      v[t[i].second].second = r;
    }
  }
  return;
}

struct BITMAX {
  int n;
  int vs[MAX_N];
  BITMAX(int n) : n(n) { clear(); }
  void clear() { memset(vs, 0, sizeof(vs)); }
  void update(int idx, int val) {
    for (int x = idx + 1; x <= n; x += x & -x)
      vs[x] = max(vs[x], val);
  }
  int get(int idx) {
    int ma = 0;
    for (int x = idx + 1; x > 0; x -= x & -x)
      ma = max(ma, vs[x]);
    return ma;
  }
};
BITMAX bit(MAX_N + 10);

int main2() {
  ll x1 = nextLong();
  ll y1 = nextLong();
  ll x2 = nextLong();
  ll y2 = nextLong();
  int N = (int)nextLong();
  REP(i, N) {
    ll X = nextLong();
    ll Y = nextLong();
    p[i].first = X;
    p[i].second = Y;
  }

  // (x1,y1) - (x2,y2) の矩形の外にある点は除く
  {
    int nn = 0;
    REP(i, N) {
      if (min(x1, x2) <= p[i].first && p[i].first <= max(x1, x2) &&
          min(y1, y2) <= p[i].second && p[i].second <= max(y1, y2)) {
        p[nn++] = p[i];
      }
    }
    N = nn;
  }

  if (x1 == x2 && y1 == y2) {
    printf("0\n");
    return 0;
  }
  if (x1 == x2) {
    if (y1 > y2)
      swap(y1, y2);
    double ans = 100 * (double)(y2 - y1);
    REP(i, N) {
      if (p[i].first == x1 && y1 < p[i].second && p[i].second < y2)
        ans += (-20 + 10 * PI);
    }
    printf("%.12f\n", ans);
    return 0;
  }
  if (y1 == y2) {
    if (x1 > x2)
      swap(x1, x2);
    double ans = 100 * (double)(x2 - x1);
    REP(i, N) {
      if (p[i].second == y1 && x1 < p[i].first && p[i].first < x2)
        ans += (-20 + 10 * PI);
    }
    printf("%.12f\n", ans);
    return 0;
  }

  if (x1 > x2) {
    swap(x1, x2);
    REP(i, N) p[i].first *= -1;
  }
  if (y1 > y2) {
    swap(y1, y2);
    REP(i, N) p[i].second *= -1;
  }
  normalize(p, N);

  sort(p.begin(), p.begin() + N);
  bit.clear();
  // pv(p.begin(), p.begin() + N);
  int ma = 0;
  REP(i, N) {
    int b = 1 + bit.get(p[i].second);
    bit.update(p[i].second, b);
    chmax(ma, b);
  }
  double ans = (x2 - x1) * 100 + (y2 - y1) * 100 + ma * (-20 + 5 * PI);
  if (min(x2 - x1, y2 - y1) + 1 == ma) {
    ans = (x2 - x1) * 100 + (y2 - y1) * 100 + (ma - 1) * (-20 + 5 * PI) +
          (-20 + 10 * PI);
  }
  printf("%.12f\n", ans);

  return 0;
}

int main() {
  for (; !cin.eof(); cin >> ws)
    main2();
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
#define REP(i, n) for (int i = 0, _n = (int)(n); i < _n; ++i)
#define ALL(v) (v).begin(), (v).end()
template <class T1, class T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &a) {
  return os << "(" << a.first << "," << a.second << ")";
}
template <class T> void pv(T a, T b) {
  for (T i = a; i != b; ++i)
    cout << (*i) << " ";
  cout << endl;
}
template <class T> bool chmin(T &a, const T &b) {
  return a > b ? (a = b, 1) : 0;
}
template <class T> bool chmax(T &a, const T &b) {
  return a < b ? (a = b, 1) : 0;
}

const double PI = acos(-1);
ll nextLong() {
  ll x;
  scanf("%lld", &x);
  return x;
}
const int MAX_N = 200000 + 10;
vector<pair<ll, ll>> p(MAX_N);

void normalize(vector<pair<ll, ll>> &v, int N) {
  vector<pair<ll, int>> t(N);
  {
    REP(i, N) {
      t[i].first = v[i].first;
      t[i].second = i;
    }
    sort(ALL(t));
    int r = 0;
    REP(i, N) {
      r += (i > 0 && t[i - 1].first != t[i].first);
      v[t[i].second].first = r;
    }
  }
  {
    REP(i, N) {
      t[i].first = v[i].second;
      t[i].second = i;
    }
    sort(ALL(t));
    int r = 0;
    REP(i, N) {
      r += (i > 0 && t[i - 1].first != t[i].first);
      v[t[i].second].second = r;
    }
  }
  return;
}

struct BITMAX {
  int n;
  int vs[MAX_N];
  BITMAX(int n) : n(n) { clear(); }
  void clear() { memset(vs, 0, sizeof(vs)); }
  void update(int idx, int val) {
    for (int x = idx + 1; x <= n; x += x & -x)
      vs[x] = max(vs[x], val);
  }
  int get(int idx) {
    int ma = 0;
    for (int x = idx + 1; x > 0; x -= x & -x)
      ma = max(ma, vs[x]);
    return ma;
  }
};
BITMAX bit(MAX_N + 10);

int main2() {
  ll x1 = nextLong();
  ll y1 = nextLong();
  ll x2 = nextLong();
  ll y2 = nextLong();
  int N = (int)nextLong();
  REP(i, N) {
    ll X = nextLong();
    ll Y = nextLong();
    p[i].first = X;
    p[i].second = Y;
  }

  // (x1,y1) - (x2,y2) の矩形の外にある点は除く
  {
    int nn = 0;
    REP(i, N) {
      if (min(x1, x2) <= p[i].first && p[i].first <= max(x1, x2) &&
          min(y1, y2) <= p[i].second && p[i].second <= max(y1, y2)) {
        p[nn++] = p[i];
      }
    }
    N = nn;
  }

  if (x1 == x2 && y1 == y2) {
    printf("0\n");
    return 0;
  }
  if (x1 == x2) {
    if (y1 > y2)
      swap(y1, y2);
    double ans = 100 * (double)(y2 - y1);
    REP(i, N) {
      if (p[i].first == x1 && y1 < p[i].second && p[i].second < y2)
        ans += (-20 + 10 * PI);
    }
    printf("%.12f\n", ans);
    return 0;
  }
  if (y1 == y2) {
    if (x1 > x2)
      swap(x1, x2);
    double ans = 100 * (double)(x2 - x1);
    REP(i, N) {
      if (p[i].second == y1 && x1 < p[i].first && p[i].first < x2)
        ans += (-20 + 10 * PI);
    }
    printf("%.12f\n", ans);
    return 0;
  }

  if (x1 > x2) {
    swap(x1, x2);
    REP(i, N) p[i].first *= -1;
  }
  if (y1 > y2) {
    swap(y1, y2);
    REP(i, N) p[i].second *= -1;
  }
  normalize(p, N);

  sort(p.begin(), p.begin() + N);
  bit.clear();
  // pv(p.begin(), p.begin() + N);
  int ma = 0;
  REP(i, N) {
    int b = 1 + bit.get(p[i].second);
    bit.update(p[i].second, b);
    chmax(ma, b);
  }
  double ans = (x2 - x1) * 100 + (y2 - y1) * 100 + ma * (-20 + 5 * PI);
  if (min(x2 - x1, y2 - y1) + 1 == ma) {
    ans = (x2 - x1) * 100 + (y2 - y1) * 100 + (ma - 1) * (-20 + 5 * PI) +
          (-20 + 10 * PI);
  }
  printf("%.12f\n", ans);

  return 0;
}

int main() {
  for (; !cin.eof(); cin >> ws)
    main2();
  return 0;
}