Problem:
Score : 900 points

Problem Statement

In the city of Nevermore, there are 10^8 streets and 10^8 avenues, both numbered from 0 to 10^8-1.
All streets run straight from west to east, and all avenues run straight from south to north.
The distance between neighboring streets and between neighboring avenues is exactly 100 meters.

Every street intersects every avenue. Every intersection can be described by pair (x, y), where x is avenue ID and y is street ID.

There are N fountains in the city, situated at intersections (X_i, Y_i).
Unlike normal intersections, there's a circle with radius 10 meters centered at the intersection, and there are no road parts inside this circle.

The picture below shows an example of how a part of the city with roads and fountains may look like.

City governors don't like encountering more than one fountain while moving along the same road.
Therefore, every street contains at most one fountain on it, as well as every avenue.

Citizens can move along streets, avenues and fountain perimeters.
What is the shortest distance one needs to cover in order to get from intersection (x_1, y_1) to intersection (x_2, y_2)?

Constraints

0 \leq x_1, y_1, x_2, y_2 < 10^8

1 \leq N \leq 200,000

0 \leq X_i, Y_i < 10^8

X_i \neq X_j for i \neq j

Y_i \neq Y_j for i \neq j

Intersections (x_1, y_1) and (x_2, y_2) are different and don't contain fountains.

All input values are integers.

Input

Input is given from Standard Input in the following format:

x_1 y_1 x_2 y_2
N
X_1 Y_1
X_2 Y_2
:
X_N Y_N

Output

Print the shortest possible distance one needs to cover in order to get from intersection (x_1, y_1) to intersection (x_2, y_2), in meters.
Your answer will be considered correct if its absolute or relative error doesn't exceed 10^{-11}.

Sample Input 1

1 1 6 5
3
3 2
5 3
2 4

Sample Output 1

891.415926535897938

One possible shortest path is shown on the picture below. The path starts at the blue point, finishes at the purple point and follows along the red line.

Sample Input 2

3 5 6 4
3
3 2
5 3
2 4

Sample Output 2

400.000000000000000

Sample Input 3

4 2 2 2
3
3 2
5 3
2 4

Sample Output 3

211.415926535897938

Solution in C++ (GCC 9.2.1) (judge verdict: Runtime Error)

#include <bits/stdc++.h>
#define inf 1e8
using namespace std;

// 0-indexed
template <class T, class E>
struct SegmentTreeLaze {
  // a,b:T c,d:E e:E(unit)
  // g(f(a,b),c) = f(g(a,c),g(b,c))
  // g(g(a,c),d) = g(a,h(c,d))
  // g(a,e) = a
  typedef function<T(T, T)> F;
  typedef function<T(T, E)> G;
  typedef function<E(E, E)> H;
  int n, height;
  F f;
  G g;
  H h;
  T tunit;
  E eunit;
  vector<T> dat;
  vector<E> laz;
  SegmentTreeLaze(){};
  SegmentTreeLaze(int newn, F f, G g, H h, T nt, E ne)
      : f(f), g(g), h(h), tunit(nt), eunit(ne) {
    init(newn);
  }
  SegmentTreeLaze(const vector<T> &v, F f, G g, H h, T nt, E ne)
      : f(f), g(g), h(h), tunit(nt), eunit(ne) {
    int _n = v.size();
    init(v.size());
    for (int i = 0; i < _n; ++i) dat[n + i] = v[i];
    for (int i = n - 1; i; --i) dat[i] = f(dat[i << 1], dat[(i << 1) | 1]);
  }
  void init(int newn) {
    n = 1, height = 0;
    while (n < newn) n <<= 1, ++height;
    dat.assign(n << 1, tunit);
    laz.assign(n << 1, eunit);
  }

  inline T reflect(int k) {
    return laz[k] == eunit ? dat[k] : g(dat[k], laz[k]);
  }

  inline void eval(int k) {
    if (laz[k] == eunit) return;
    laz[k << 1] = h(laz[k << 1], laz[k]);
    laz[(k << 1) | 1] = h(laz[(k << 1) | 1], laz[k]);
    dat[k] = reflect(k);
    laz[k] = eunit;
  }

  inline void thrust(int k) {
    for (int i = height; i; --i) eval(k >> i);
    // reset query
    // dat[k] = reflect(k);
    // laz[k] = eunit;
  }

  void recalc(int k) {
    while (k >>= 1) dat[k] = f(reflect(k << 1), reflect((k << 1) | 1));
  }
  // [a,b)
  void update(int a, int b, E newdata) {
    thrust(a += n);
    thrust(b += n - 1);
    for (int l = a, r = b + 1; l < r; l >>= 1, r >>= 1) {
      if (l & 1) laz[l] = h(laz[l], newdata), l++;
      if (r & 1) --r, laz[r] = h(laz[r], newdata);
    }
    recalc(a);
    recalc(b);
  }

  void set_val(int k, T newdata) {
    thrust(k += n);
    dat[k] = newdata;
    laz[k] = eunit;
    recalc(k);
  }

  // [a,b)
  T query(int a, int b) {
    thrust(a += n);
    thrust(b += n - 1);
    T vl = tunit, vr = tunit;
    for (int l = a, r = b + 1; l < r; l >>= 1, r >>= 1) {
      if (l & 1) vl = f(vl, reflect(l++));
      if (r & 1) vr = f(reflect(--r), vr);
    }
    return f(vl, vr);
  }
};

using P = pair<long long, long long>;
using Pd = pair<long double, bool>;
const long double PI = acos(-1.0L);
int n;
P s, t;
vector<P> v;
map<long long, int> mp;
SegmentTreeLaze<long double, Pd> seg;

long double solve();

int main() {
  cout << fixed << setprecision(15);
  cin >> s.first >> s.second >> t.first >> t.second >> n;
  if (s > t) swap(s, t);
  for (int i = 0; i < n; ++i) {
    P p;
    cin >> p.first >> p.second;
    if (s.first <= p.first && p.first <= t.first &&
        min(s.second, t.second) <= p.second &&
        p.second <= max(s.second, t.second))
      v.push_back(p);
  }
  n = v.size();
  if (s.second > t.second) {
    s.second = (long long)1e8 - 1 - s.second;
    t.second = (long long)1e8 - 1 - t.second;
    for (auto &p : v) p.second = (long long)1e8 - 1 - p.second;
  }
  sort(v.begin(), v.end());
  {
    vector<long long> tmp;
    tmp.push_back(s.second);
    tmp.push_back(t.second);
    for (auto p : v) tmp.push_back(p.second);
    sort(tmp.begin(), tmp.end());
    tmp.erase(unique(tmp.begin(), tmp.end()), tmp.end());
    for (int i = 0; i < tmp.size(); ++i) mp[tmp[i]] = i;
  }
  {
    auto f = [](long double l, long double r) { return min(l, r); };
    auto g = [](long double l, Pd r) {
      if (r.second) return r.first;
      return min(l, r.first);
    };
    auto h = [](Pd l, Pd r) {
      if (r.second) return r;
      return Pd(min(l.first, r.first), 1);
    };
    seg = SegmentTreeLaze<long double, Pd>(mp.size(), f, g, h, inf, Pd(inf, 0));
    if (n && v[0].first == s.first)
      for (int i = 0; i < v[0].second; ++i) seg.set_val(i, 0);
    else
      for (int i = 0; i < mp.size(); ++i) seg.set_val(i, 0);
  }
  cout << solve() << endl;
  return 0;
}

long double solve() {
  if (!n) return (t.first + t.second - s.first - s.second) * 100;
  const long double quart = 5.0L * (PI - 4.0L);
  const long double half = 5.0L * (2.0L * PI - 4.0L);
  for (int i = 0; i < n; ++i) {
    int id = mp[v[i].second];
    long double now = inf;
    {  // over
      if (id != 0) now = min(now, seg.query(0, id) + half);
      now = min(now, seg.query(id, id + 1) + quart);
      if (id + 1 != mp.size()) seg.update(id + 1, mp.size(), Pd(now, 0));
    }
    {  // just
      now = inf;
      now = min(now, seg.query(id, id + 1) + half);
      if (id != 0) now = min(now, seg.query(0, id) + quart);
      seg.update(id, id + 1, Pd(now, 1));
    }
  }
  long double res = inf;
  if (n && v[n - 1].first == t.first)
    res = seg.query(mp[v[n - 1].second], mp.size());
  else
    res = seg.query(0, mp.size());
  return res + (t.first + t.second - s.first - s.second) * 100;
}