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++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

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

typedef long double lf;
typedef pair<int, int> pii;

const lf PI = 3.1415926535897932384626433832795028841971693993751058209749445923078164062862;

int X1, Y1, X2, Y2, N, Xn;
vector<pii> P;
vector<int> X;
map<int, int> dx;

struct BIT {
    vector<int> tree;
    void init() {
        tree = vector<int>(4*Xn, 0);
    }
    void upd(int idx, int val, int l, int r, int n) {
        if(idx < l || r < idx) return;
        if(l == r) {
            tree[n] = max(tree[n], val);
            return;
        }
        int m = (l + r)>>1;
        upd(idx, val, l, m, 2*n);
        upd(idx, val, m + 1, r, 2*n + 1);
        tree[n] = max(tree[2*n], tree[2*n + 1]);
    }
    int quer(int a, int b, int l, int r, int n) {
        if(b < l || r < a) return 0;
        if(a <= l && r <= b) return tree[n];
        int m = (l + r)>>1;
        int L = quer(a, b, l, m, 2*n);
        int R = quer(a, b, m + 1, r, 2*n + 1);
        return max(L, R);
    }
} bit;

pii st, ed;
vector<int> dp;

bool cmp(pii a, pii b) {
    return a.first < b.first || (a.first == b.first && (st.second < ed.second? a.second < b.second : a.second > b.second));
}

int main() {
    scanf("%d %d %d %d %d", &X1, &Y1, &X2, &Y2, &N);
    st = pii(X1, Y1);
    ed = pii(X2, Y2);
    if(X2 < X1) swap(st, ed);
    X.push_back(Y1);
    X.push_back(Y2);
    P.resize(N);
    for(int i = 0; i < N; i++) {
        scanf("%d %d", &P[i].first, &P[i].second);
        X.push_back(P[i].second);
    }

    cout<<fixed;
    cout.precision(20);

    if(X1 == X2) {
        int cnt = 0;
        for(int i = 0; i < N; i++) {
            if(P[i].first == X1 && min(Y1, Y2) <= P[i].second && P[i].second <= max(Y1, Y2)) cnt++;
        }
        lf ans = 100LL * abs(Y1 - Y2);
        ans += (10*PI - 20) * cnt;
        cout << ans;
        return 0;
    }
    if(Y1 == Y2) {
        int cnt = 0;
        for(int i = 0; i < N; i++) {
            if(P[i].second == Y1 && min(X1, X2) <= P[i].first && P[i].first <= max(X1, X2)) cnt++;
        }
        lf ans = 100LL * abs(X1 - X2);
        ans += (10*PI - 20) * cnt;
        cout << ans;
        return 0;
    }

    sort(X.begin(), X.end());
    X.resize(unique(X.begin(), X.end()) - X.begin());
    Xn = X.size();
    for(int i = 0; i < Xn; i++) dx[X[i]] = i;
    for(int i = 0; i < N; i++) P[i].second = dx[P[i].second];
    st.second = dx[st.second];
    ed.second = dx[ed.second];

    P.push_back(st);
    P.push_back(ed);
    sort(P.begin(), P.end(), cmp);

    bit.init();
    dp.resize(P.size());
    for(int i = P.size() - 1; i >= 0; i--) {
        if(ed.first < P[i].first) continue;
        if(P[i].second < min(st.second, ed.second) || max(st.second, ed.second) < P[i].second) continue;

        dp[i] = (P[i] != st && P[i] != ed) + bit.quer(min(P[i].second, ed.second), max(P[i].second, ed.second), 0, Xn - 1, 1);
        bit.upd(P[i].second, dp[i], 0, Xn - 1, 1);

        if(P[i] == st) {
            lf ans = 100LL * (abs(st.first - ed.first) + abs(X[st.second] - X[ed.second]));
            ans -= (20 - 5*PI) * dp[i];
            cout << ans;
            return 0;
        }
    }
}