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



Sample input:
1 1 6 5
3
3 2
5 3
2 4


Sample output:
891.415926535897938

Submission status: Wrong Answer

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
gosa = 1.0 / 10**10
mod = 998244353

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()

class Seg():
    def __init__(self, n, default, func):
        i = 1
        while 2**i <= n:
            i += 1
        self.D = default
        self.H = i
        self.N = 2**i
        self.A = [default] * (self.N*2-1)
        self.F = func

    def find(self, i):
        return self.A[i + self.N - 1]

    def update(self, i, x):
        i += self.N - 1
        self.A[i] = x
        while i > 0:
            i = (i-1) // 2
            self.A[i] = self.merge(self.A[i*2+1], self.A[i*2+2])

    def merge(self, a, b):
        return self.F(a, b)

    def total(self):
        return self.A[0]

    def query(self, a, b):
        def _query(k,l,r):
            if r <= a or b <= l:
                return self.D
            if a <= l and r <= b:
                return self.A[k]
            m = (l+r)//2
            vl = _query(k*2+1,l,m)
            vr = _query(k*2+2,m,r)
            return self.merge(vl,vr)
        return _query(0,0,self.N)


def main():
    x1,y1,x2,y2 = LI()
    n = I()
    a = sorted([LI() for _ in range(n)])
    dx = {}
    dy = {}
    t = sorted([x1,x2] + list(map(lambda x: x[0], a)))
    dx[t[0]] = c = 1
    for i in range(1,n+2):
        if t[i-1] == t[i]:
            continue
        c += 1
        dx[t[i]] = c
    t = sorted([y1,y2] + list(map(lambda x: x[1], a)))
    dy[t[0]] = c = 1
    for i in range(1,n+2):
        if t[i-1] == t[i]:
            continue
        c += 1
        dy[t[i]] = c

    r = (abs(x2-x1) + abs(y2-y1)) * 100
    x1 = dx[x1]
    x2 = dx[x2]
    y1 = dy[y1]
    y2 = dy[y2]
    a = list(map(lambda x: [dx[x[0]], dy[x[1]]], a))

    if x1 > x2:
        x1,y1,x2,y2 = x2,y2,x1,y1
    yy1 = y1
    yy2 = y2
    if yy1 > yy2:
        yy1,yy2 = yy2,yy1

    def f(a,b):
        if a>b:
            return a
        return b

    s = Seg(n+10,0,f)

    yf = y2 < y1
    for x,y in a:
        if x < x1:
            continue
        if x > x2:
            continue
        if y < yy1:
            continue
        if y > yy2:
            continue
        if yf:
            y = n-y+1
        t = s.query(0,y)
        s.update(y,t+1)

    y = y2
    if yf:
        y = n-y2+1
    k = 20 - 5 * math.pi
    t = s.total()
    r = r-t*k
    if t >= min(abs(x2-x1),abs(y1-y2)):
        r += 5 * math.pi

    return r

print(main())

Submission status: Wrong Answer

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
gosa = 1.0 / 10**10
mod = 998244353

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()

class Seg():
    def __init__(self, n, default, func):
        i = 1
        while 2**i <= n:
            i += 1
        self.D = default
        self.H = i
        self.N = 2**i
        self.A = [default] * (self.N*2-1)
        self.F = func

    def find(self, i):
        return self.A[i + self.N - 1]

    def update(self, i, x):
        i += self.N - 1
        self.A[i] = x
        while i > 0:
            i = (i-1) // 2
            self.A[i] = self.merge(self.A[i*2+1], self.A[i*2+2])

    def merge(self, a, b):
        return self.F(a, b)

    def total(self):
        return self.A[0]

    def query(self, a, b):
        def _query(k,l,r):
            if r <= a or b <= l:
                return self.D
            if a <= l and r <= b:
                return self.A[k]
            m = (l+r)//2
            vl = _query(k*2+1,l,m)
            vr = _query(k*2+2,m,r)
            return self.merge(vl,vr)
        return _query(0,0,self.N)


def main():
    x1,y1,x2,y2 = LI()
    n = I()
    a = sorted([LI() for _ in range(n)])
    dx = {}
    dy = {}
    t = sorted([x1,x2] + list(map(lambda x: x[0], a)))
    dx[t[0]] = c = 1
    for i in range(1,n+2):
        if t[i-1] == t[i]:
            continue
        if t[i-1] + 1 == t[i]:
            c += 1
        else:
            c += 2
        dx[t[i]] = c
    t = sorted([y1,y2] + list(map(lambda x: x[1], a)))
    dy[t[0]] = c = 1
    for i in range(1,n+2):
        if t[i-1] == t[i]:
            continue
        if t[i-1] + 1 == t[i]:
            c += 1
        else:
            c += 2
        dy[t[i]] = c

    x1 = dx[x1]
    x2 = dx[x2]
    y1 = dy[y1]
    y2 = dy[y2]
    a = list(map(lambda x: [dx[x[0]], dy[x[1]]], a))

    if x1 > x2:
        x1,y1,x2,y2 = x2,y2,x1,y1
    yy1 = y1
    yy2 = y2
    if yy1 > yy2:
        yy1,yy2 = yy2,yy1

    def f(a,b):
        if a>b:
            return a
        return b

    s = Seg(n*2+10,0,f)

    yf = y2 < y1
    for x,y in a:
        if x < x1:
            continue
        if x > x2:
            continue
        if y < yy1:
            continue
        if y > yy2:
            continue
        if yf:
            y = n-y+1
        t = s.query(0,y-1)
        s.update(y,t+1)

    r = (abs(x2-x1) + abs(y2-y1)) * 100
    y = y2
    if yf:
        y = n-y2+1
    k = 20 - 5 * math.pi
    t = s.total()
    r = r-t*k
    if t >= min(abs(x2-x1),abs(y1-y2)):
        r += 5 * math.pi

    return r

print(main())

Submission status: Wrong Answer

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
gosa = 1.0 / 10**10
mod = 998244353

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()

class Seg():
    def __init__(self, n, default, func):
        i = 1
        while 2**i <= n:
            i += 1
        self.D = default
        self.H = i
        self.N = 2**i
        self.A = [default] * (self.N*2-1)
        self.F = func

    def find(self, i):
        return self.A[i + self.N - 1]

    def update(self, i, x):
        i += self.N - 1
        self.A[i] = x
        while i > 0:
            i = (i-1) // 2
            self.A[i] = self.merge(self.A[i*2+1], self.A[i*2+2])

    def merge(self, a, b):
        return self.F(a, b)

    def total(self):
        return self.A[0]

    def query(self, a, b):
        def _query(k,l,r):
            if r <= a or b <= l:
                return self.D
            if a <= l and r <= b:
                return self.A[k]
            m = (l+r)//2
            vl = _query(k*2+1,l,m)
            vr = _query(k*2+2,m,r)
            return self.merge(vl,vr)
        return _query(0,0,self.N)


def main():
    x1,y1,x2,y2 = LI()
    n = I()
    a = sorted([LI() for _ in range(n)])
    dx = {}
    dy = {}
    t = sorted([x1,x2] + list(map(lambda x: x[0], a)))
    dx[t[0]] = c = 1
    for i in range(1,n+2):
        if t[i-1] == t[i]:
            continue
        if t[i-1] + 1 == t[i]:
            c += 1
        else:
            c += 2
        dx[t[i]] = c
    t = sorted([y1,y2] + list(map(lambda x: x[1], a)))
    dy[t[0]] = c = 1
    for i in range(1,n+2):
        if t[i-1] == t[i]:
            continue
        if t[i-1] + 1 == t[i]:
            c += 1
        else:
            c += 2
        dy[t[i]] = c

    x1 = dx[x1]
    x2 = dx[x2]
    y1 = dy[y1]
    y2 = dy[y2]
    a = list(map(lambda x: [dx[x[0]], dy[x[1]]], a))

    if x1 > x2:
        x1,y1,x2,y2 = x2,y2,x1,y1
    yy1 = y1
    yy2 = y2
    if yy1 > yy2:
        yy1,yy2 = yy2,yy1

    def f(a,b):
        if a>b:
            return a
        return b

    s = Seg(n*2+10,0,f)

    yf = y2 < y1
    for x,y in a:
        if x < x1:
            continue
        if x > x2:
            continue
        if y < yy1:
            continue
        if y > yy2:
            continue
        if yf:
            y = n-y+1
        t = s.query(0,y)
        s.update(y,t+1)

    r = (abs(x2-x1) + abs(y2-y1)) * 100
    y = y2
    if yf:
        y = n-y2+1
    k = 20 - 5 * math.pi
    t = s.total()
    r = r-t*k
    if t >= min(abs(x2-x1),abs(y1-y2)):
        r += 5 * math.pi

    return r

print(main())

Submission status: Wrong Answer

Solution code:
from math import pi
x1,y1,x2,y2=map(int,raw_input().split())
N=input()
fx=[]
#fy=[]
#x=[]
y=[]
xm=min([x1,x2])
mx=max([x2,x1])
ym=min([y1,y2])
my=max([y2,y1])
for i in range(N):
    Xi,Yi=map(int,raw_input().split())
    if xm<=Xi<=mx and ym<=Yi<=my:
        #x.append(Xi)
        y.append(Yi)
        #fy.append((Yi,Xi))
        fx.append((Xi,Yi))
fx.sort(reverse=(x2<x1))
#fy.sort()
y.sort(reverse=(y2<y1))
#x.sort()
l=N
n=0
for xi,yj in fx:
    #i=x.index(xi)
    j=y.index(yj)
    del y[j]
    n=max(l-j,n)
    l-=1
flag=((x1-x2)*(y1-y2)==0)
print (abs(x1-x2)+abs(y1-y2))*100+((10*pi-20) if flag else (5*pi-20)*n)

Submission status: Wrong Answer

Solution code:
x1,y1,x2,y2 = map(int,input().split())

W,H = abs(x1-x2),abs(y1-y2)


import math

flip_x = x1 > x2
flip_y = y1 > y2

xo = min(x1,x2)
yo = min(y1,y2)

N = int(input())

F = []

for i in range(N):
  x,y = map(int,input().split())
  x -= xo
  y -= yo
  if 0 <= x <= W and 0 <= y <= H:
    if flip_x:
      x = W-x
    if flip_y:
      y = H-y
    F.append((x,y))

N = len(F)

#直線の場合
if W == 0 or H == 0:
  if F:
    L = (W+H)*100 + math.pi*10-20
  else:
    L = (W+H)*100

elif N == 0:
  L = (W+H)*100
else:
  # find LIS
  F.sort()
  F = [y for x,y in F]

  dp = [H+1]*len(F)

  from bisect import bisect

  for i,f in enumerate(F):
    j = bisect(dp,f)
    dp[j] = f

  n = bisect(dp,H)
  L = (W+H)*100 - (20-math.pi*5)*n

print(L)

Submission status: Wrong Answer

Solution code:
from sys import stdin
input = stdin.readline
from math import pi

from bisect import bisect_left as bl
def LIS(L):
    best = []
    for i in L:
        pos = bl(best, i)
        if len(best) <= pos: best.append(i)
        else: best[pos] = i
    return len(best)

flip = False
x1, y1, x2, y2 = map(int,input().split())
if x1 > x2: x1, y1, x2, y2 = x2, y2, x1, y1
if y1 > y2:
    flip = True
    y1, y2 = y2, y1
points = []
n = int(input())
for i in range(n):
    x, y = map(int,input().split())
    if x1 <= x <= x2 and y1 <= y <= y2:
        if flip: points.append((x,-y))
        else: points.append((x,y))

foun = LIS(points)
dist = 100*(x2-x1+y2-y1)
dist-= (20-5*pi)*foun
if foun == min(x2-x1+1, y2-y1+1): dist+= 5*pi
print(dist)

Submission status: Wrong Answer

Solution code:
from sys import stdin
input = stdin.readline
from decimal import Decimal as D
 
from bisect import bisect_left as bl
def LIS(L):
    best = []
    for i in L:
        pos = bl(best, i)
        if len(best) <= pos: best.append(i)
        else: best[pos] = i
    return len(best)
 
flip = False
x1, y1, x2, y2 = map(int,input().split())
if x1 > x2: x1, y1, x2, y2 = x2, y2, x1, y1
if y1 > y2:
    flip = True
    y1, y2 = y2, y1
points = []
n = int(input())
for i in range(n):
    x, y = map(int,input().split())
    if x1 <= x <= x2 and y1 <= y <= y2:
        if flip: points.append((x,-y))
        else: points.append((x,y))
 
foun = LIS(points)
dist = D(100)*(x2-x1+y2-y1)
dx = D('-4.2920367320510338076867835')*foun
if foun == min(x2-x1+1, y2-y1+1): dx+= D('15.7079632679489661923132165')
print(dist+dx)

Submission status: Wrong Answer

Solution code:
from sys import stdin
input = stdin.readline
from decimal import Decimal as D

from bisect import bisect_left as bl
def LIS(L):
    best = []
    for i in L:
        pos = bl(best, i)
        if len(best) <= pos: best.append(i)
        else: best[pos] = i
    return len(best)

flip = False
x1, y1, x2, y2 = map(int,input().split())
if x1 > x2: x1, y1, x2, y2 = x2, y2, x1, y1
if y1 > y2:
    flip = True
    y1, y2 = y2, y1
points = []
n = int(input())
for i in range(n):
    x, y = map(int,input().split())
    if x1 <= x <= x2 and y1 <= y <= y2:
        if flip: points.append((-x,-y))
        else: points.append((x,y))

foun = LIS(points)
dist = D(100)*(x2-x1+y2-y1)
dx = D('-4.2920367320510338076867835')*foun
if foun == min(x2-x1+1, y2-y1+1): dx+= D('15.7079632679489661923132165')
print(dist+dx)

Submission status: Wrong Answer

Solution code:
import bisect as bs


def solve():
    quoter = 15.707963267948966192313216916398

    x1, y1, x2, y2 = map(int, input().split())
    if x1 > x2:
        x1, y1, x2, y2 = x2, y2, x1, y1
    is_down = y1 > y2
    if is_down:
        y1, y2 = -y1, -y2

    n = int(input())
    fountains = []
    for _ in range(n):
        x, y = map(int, input().split())
        if is_down:
            y = -y
        if x < x1 or x2 < x or y < y1 or y2 < y:
            continue
        fountains.append((y, x))
    fountains.sort()

    if x1 == x2:
        base = (y2 - y1) * 100
        for y, x in fountains:
            if x == x1:
                if y1 < y < y2:
                    return base + quoter * 2 - 20
                else:
                    break
        return base
    if y1 == y2:
        base = (x2 - x1) * 100
        for y, x in fountains:
            if y == y1:
                if x1 < x < x2:
                    return base + quoter * 2 - 20
                else:
                    break
        return base

    dp = []
    for y, x in fountains:
        cdp = dp.copy()
        for idx, (cx, cy, cnt) in enumerate(dp):
            if cx > x:
                break
            bs.insort(cdp, (x, y, cnt + 1), lo=idx)
        if len(cdp) == len(dp):
            bs.insort(cdp, (x, y, 1))
        dp = cdp

    mc = max((item[2] for item in dp), default=0)
    return (x2 - x1 + y2 - y1) * 100 - mc * (20 - quoter)


print(solve())

Submission status: Wrong Answer

Solution code:
from sys import stdin
from math import pi
from bisect import bisect
from itertools import repeat
def main():
    x1, y1, x2, y2 = map(int, stdin.readline().split())
    sx = 1
    if x1 > x2:
        x1, x2 = -x1, -x2
        sx = -1
    sy = 1
    if y1 > y2:
        y1, y2 = -y1, -y2
        sy = -1
    n = int(stdin.readline())
    dat = map(int, stdin.read().split(), repeat(10, 2 * n))
    a = []
    for i in xrange(n):
        x, y = dat[i*2:i*2+2]
        x *= sx
        y *= sy
        if x1 <= x <= x2 and y1 <= y <= y2:
            a.append((x, y))
    a.sort()
    c = pi * 10 - 20
    b = [-10 ** 9] + [10 ** 9] * 200010
    m = 1
    for x, y in a:
        p = bisect(b, y, lo=0, hi=m)
        b[p] = y
        if p == m:
            m += 1
    d = pi * 5 - 20
    if m <= min(x2 - x1, y2 - y1):
        c = d
    print "%.12f" % ((x2 - x1 + y2 - y1) * 100 + (m - 2) * d + c)
main()

Submission status: Wrong Answer

Solution code:
from sys import stdin
input = stdin.readline
from decimal import Decimal as D

from bisect import bisect_left as bl
def LISFind(L):
    if not L: return []
    best = []; ind = []; prev = []
    for i in range(len(L)):
        x = L[i]; pos = bl(best, x)
        if len(best) <= pos: best.append(x); ind.append(i)
        else: best[pos] = x; ind[pos] = i
        if pos == 0: prev.append(None)
        else: prev.append(ind[pos-1])
    res = []; k = ind[-1]
    while k != None: res.append(L[k]); k = prev[k]
    return res[::-1]

flip = False
x1, y1, x2, y2 = map(int,input().split())
if x1 > x2: x1, y1, x2, y2 = x2, y2, x1, y1
if y1 > y2:
    flip = True
    y1, y2 = y2, y1
points = []
n = int(input())
for i in range(n):
    x, y = map(int,input().split())
    if x1 <= x <= x2 and y1 <= y <= y2:
        if flip: points.append((-x,-y))
        else: points.append((x,y))

foun = LISFind(points)
dist = D(100)*(x2-x1+y2-y1)
if x1 == x2 or y1 == y2: dx = D('11.4159265358979323846')*len(foun)
else:
    dx = D('-4.2920367320510338076867835')*len(foun)
    if foun and foun[0][0] != x1 and foun[-1][1] == y2:
        dx+= D('15.7079632679489661923132165')
print(dist+dx)

Submission status: Wrong Answer

Solution code:
from sys import stdin
from math import pi
from bisect import bisect
from itertools import repeat
def main():
    x1, y1, x2, y2 = map(int, stdin.readline().split())
    sx = 1
    if x1 > x2:
        x1, x2 = -x1, -x2
        sx = -1
    sy = 1
    if y1 > y2:
        y1, y2 = -y1, -y2
        sy = -1
    n = int(stdin.readline())
    dat = map(int, stdin.read().split(), repeat(10, 2 * n))
    a = []
    for i in xrange(n):
        x, y = dat[i*2:i*2+2]
        x *= sx
        y *= sy
        if x1 <= x <= x2 and y1 <= y <= y2:
            a.append((x, y))
    a.sort()
    c = pi * 5 - 10
    if x1 == x2 or y1 == y2:
        if a:
            print "%.12f" % ((x2 - x1 + y2 - y1) * 100 + 2 * c)
        else:
            print "%.12f" % ((x2 - x1 + y2 - y1) * 100)
        return
    b = [-10 ** 9] + [10 ** 9] * 200010
    m = 1
    for x, y in a:
        p = bisect(b, y, lo=0, hi=m)
        b[p] = y
        if p == m:
            m += 1
    c = pi * 5 - 20
    print "%.12f" % ((x2 - x1 + y2 - y1) * 100 + (m - 1) * c)
main()

Submission status: Wrong Answer

Solution code:
from sys import stdin
input = stdin.readline
from decimal import Decimal as D

from bisect import bisect_left as bl
def LIS(L):
    best = []
    for i in L:
        pos = bl(best, i)
        if len(best) <= pos: best.append(i)
        else: best[pos] = i
    return len(best)

flip = False
x1, y1, x2, y2 = map(int,input().split())
if x1 > x2: x1, y1, x2, y2 = x2, y2, x1, y1
if y1 > y2:
    flip = True
    y1, y2 = y2, y1
points = []
n = int(input())
for i in range(n):
    x, y = map(int,input().split())
    if x1 <= x <= x2 and y1 <= y <= y2:
        if flip: points.append((-x,-y))
        else: points.append((x,y))

foun = LIS(points)
dist = D(100)*(x2-x1+y2-y1)
if x1 == x2 or y1 == y2: dx = D('11.4159265358979323846')*foun
else: dx = D('-4.2920367320510338076867835')*foun
print(dist+dx)

Submission status: Time Limit Exceeded

Solution code:
import sys
pi = 3.1415926535897932384626

def input():
	return sys.stdin.buffer.readline()[:-1]

class Rmax():
	def __init__(self, size):
		#the number of nodes is 2n-1
		self.n = 1
		while self.n < size:
			self.n *= 2
		self.node = [0] * (2*self.n-1)

	def Access(self, x):
		return self.node[x+self.n-1]

	def Update(self, x, val):
		x += self.n-1
		self.node[x] = val
		while x > 0:
			x = (x-1)//2
			self.node[x] = max(self.node[2*x+1], self.node[2*x+2])
		return

	#[l, r)
	def Get(self, l, r):
		L, R = l+self.n, r+self.n
		s = 0
		while L<R:
			if R & 1:
				R -= 1
				s = max(s, self.node[R-1])
			if L & 1:
				s = max(s, self.node[L-1])
				L += 1
			L >>= 1
			R >>= 1
		return s

x1, y1, x2, y2 = map(int, input().split())
n = int(input())
f = [tuple(map(int, input().split())) for _ in range(n)]

if x1 == x2:
	flg = False
	for x, y in f:
		if x == x1 and min(y1, y2) <= y <= max(y1, y2):
			flg = True
	if flg:
		print(abs(y1-y2) * 100 + 10*pi - 20)
	else:
		print(abs(y1-y2) * 100)
elif y1 == y2:
	flg = False
	for x, y in f:
		if y == y1 and min(x1, x2) <= x <= max(x1, x2):
			flg = True
	if flg:
		print(abs(x1-x2) * 100 + 10*pi - 20)
	else:
		print(abs(x1-x2) * 100)
else:
	if x1 > x2 and y1 > y2:
		x1, x2, y1, y2 = x2, x1, y2, y1
		f = [[x[0], x[1], i] for i, x in enumerate(f) if min(x1, x2) <= x[0] <= max(x1, x2) and min(y1, y2) <= x[1] <= max(y1, y2)]
	elif x1 > x2:
		x1, x2 = 10**8-x1, 10**8-x2
		f = [[10**8-x[0], x[1], i] for i, x in enumerate(f) if min(x1, x2) <= 10**8-x[0] <= max(x1, x2) and min(y1, y2) <= x[1] <= max(y1, y2)]
	elif y1 > y2:
		y1, y2 = 10**8-y1, 10**8-y2
		f = [[x[0], 10**8-x[1], i] for i, x in enumerate(f) if min(x1, x2) <= x[0] <= max(x1, x2) and min(y1, y2) <= 10**8-x[1] <= max(y1, y2)]
	#print(f)
	else:
		f = [[x[0], x[1], i] for i, x in enumerate(f) if min(x1, x2) <= x[0] <= max(x1, x2) and min(y1, y2) <= x[1] <= max(y1, y2)]
	n = len(f)
	if n == 0:
		print((abs(x1-x2) + abs(y1-y2)) * 100)
		sys.exit()
	f.sort(key=lambda x: x[1])
	f = [[x[0], i] for i, x in enumerate(f)]
	f.sort(reverse=True)
	st = Rmax(n)
	for x, y in f:
		st.Update(y, st.Get(y, n) + 1)
	num = st.Get(0, n)
	ans = (abs(x1-x2) + abs(y1-y2)) * 100 + num * 5 * pi - num * 20
	if num == min(abs(x1-x2), abs(y1-y2)) + 1:
		ans += 5 * pi
	print("{0:.15f}".format(ans))

Submission status: Time Limit Exceeded

Solution code:
from math import pi

input1 = list(map(int,input("").split(" ")))
N = int(input(""))
xs = [ input() for _ in range(N) ]

src_x = input1[0]
src_y = input1[1]
dist_x = input1[2]
dist_y = input1[3]
spring = []
for i in range(N):
	temp = list(map(int,xs[i].split(" ")))
	temp_x = temp[0]
	temp_y = temp[1]
	if (src_x <= temp_x) and (temp_x <= dist_x) or (dist_x <= temp_x) and (temp_x <= src_x):
		if (src_y <= temp_y) and (temp_y <= dist_y) or (dist_y <= temp_y) and (temp_y <= src_y):
			spring.append({'x':temp_x, 'y':temp_y})

# 基礎点
path = (abs(src_x-dist_x) + abs(src_y-dist_y)) * 100
n = len(spring)
if n > 0:
	r = 10
	if src_x == dist_x or src_y == dist_y :
		#１直線半円あり
		path += r*pi - 2*r 
	else:
		#1/4円あり
		if (src_x-dist_x)*(src_y-dist_y) > 0:
			spring.sort(key=lambda k: k['x'])
		else:
			spring.sort(key=lambda k: k['x'], reverse=True)

		#最長増加列問題
		L = [None] * n
		L[0] = spring[0]['y']
		length = 1
		for i in range(1,n):
			if L[length-1] < spring[i]['y']:
				L[length] = spring[i]['y']
				length += 1
			else:
				# 2分木探査風
				start = 0
				last = length - 1
				while abs(last-start) > 0:
					j = int((start+length)/2)
					if spring[i]['y'] < L[j] :
						last = j
					else:
						start = j
				L[last] = spring[i]['y']
		
		#半円があるかないか	
		if length < min(abs(src_x-dist_x),abs(src_y-dist_y)) + 1:
			path += (r*pi/2 - 2*r) * length
		else:
			path += (r*pi/2 - 2*r) * (length-1) + (r*pi - 2*r)
print(path)

Submission status: Time Limit Exceeded

Solution code:
from math import pi

input1 = list(map(int,input("").split(" ")))
N = int(input(""))
xs = [ input() for _ in range(N) ]

src_x = input1[0]
src_y = input1[1]
dist_x = input1[2]
dist_y = input1[3]
spring = []
for i in range(N):
	temp = list(map(int,xs[i].split(" ")))
	if (src_x <= temp[0]) and (temp[0] <= dist_x) or (dist_x <= temp[0]) and (temp[0] <= src_x):
		if (src_y <= temp[1]) and (temp[1] <= dist_y) or (dist_y <= temp[1]) and (temp[1] <= src_y):
			spring.append({'x':temp[0], 'y':temp[1]})

# 基礎点
path = (abs(src_x-dist_x) + abs(src_y-dist_y)) * 100
n = len(spring)
if n > 0:
	r = 10
	if src_x == dist_x or src_y == dist_y :
		#１直線半円あり
		path += r*pi - 2*r 
	else:
		#1/4円あり
		if (src_x-dist_x)*(src_y-dist_y) > 0:
			spring.sort(key=lambda k: k['x'])
		else:
			spring.sort(key=lambda k: k['x'], reverse=True)

		#最長増加列問題
		L = [None] * n
		L[0] = spring[0]['y']
		length = 1
		for i in range(1,n):
			if L[length-1] < spring[i]['y']:
				L[length] = spring[i]['y']
				length += 1
			else:
				# 2分木探査風
				start = 0
				last = length - 1
				while abs(last-start) > 0:
					j = int((start+length)/2)
					if spring[i]['y'] < L[j] :
						last = j
					else:
						start = j
				L[last] = spring[i]['y']
		
		#半円があるかないか	
		if length < min(abs(src_x-dist_x),abs(src_y-dist_y)) + 1:
			path += (r*pi/2 - 2*r) * length
		else:
			path += (r*pi/2 - 2*r) * (length-1) + (r*pi - 2*r)
print(path)

Submission status: Time Limit Exceeded

Solution code:
from math import pi

input1 = list(map(int,input("").split(" ")))
N = int(input(""))
xs = [ input() for _ in range(N) ]

src_x = input1[0]
src_y = input1[1]
dist_x = input1[2]
dist_y = input1[3]
spring = []
for i in range(N):
	temp = list(map(int,xs[i].split(" ")))
	if (src_x <= temp[0]) and (temp[0] <= dist_x) or (dist_x <= temp[0]) and (temp[0] <= src_x):
		if (src_y <= temp[1]) and (temp[1] <= dist_y) or (dist_y <= temp[1]) and (temp[1] <= src_y):
			spring.append({'x':temp[0], 'y':temp[1]})

# 基礎点
path = (abs(src_x-dist_x) + abs(src_y-dist_y)) * 100
n = len(spring)
if n > 0:
	r = 10
	if src_x == dist_x or src_y == dist_y :
		#１直線半円あり
		path += r*pi - 2*r 
	else:
		#1/4円あり
		if (src_x-dist_x)*(src_y-dist_y) > 0:
			spring.sort(key=lambda k: k['x'])
		else:
			spring.sort(key=lambda k: k['x'], reverse=True)

		#最長路問題
		L = [None] * n
		L[0] = spring[0]['y']
		length = 1
		for i in range(1,n):
			if L[length-1] < spring[i]['y']:
				L[length] = spring[i]['y']
				length += 1
			else:
				# 2分木探査風
				start = 0
				last = length - 1
				while abs(last-start) > 0:
					j = int((start+length)/2)
					if spring[i]['y'] < L[j] :
						last = j
					else:
						start = j
				L[last] = spring[i]['y']
		
		#半円があるかないか	
		if length < min(abs(src_x-dist_x),abs(src_y-dist_y)) + 1:
			path += (r*pi/2 - 2*r) * length
		else:
			path += (r*pi/2 - 2*r) * (length-1) + (r*pi - 2*r)
print(path)

Submission status: Time Limit Exceeded

Solution code:
from math import pi

input1 = list(map(int,input("").split(" ")))
N = int(input(""))
src = {"x":input1[0],"y":input1[1]}
dist = {"x":input1[2],"y":input1[3]}

spring = []
for i in range(N):
	temp = list(map(int,input("").split(" ")))
	if (src['x'] <= temp[0]) and (temp[0] <= dist['x']) or (dist['x'] <= temp[0]) and (temp[0] <= src['x']):
		if (src['y'] <= temp[1]) and (temp[1] <= dist['y']) or (dist['y'] <= temp[1]) and (temp[1] <= src['y']):
			spring.append({'x':temp[0], 'y':temp[1]})

# 基礎点
path = (abs(src['x']-dist['x']) + abs(src['y']-dist['y'])) * 100
n = len(spring)
if n > 0:
	r = 10
	if src['x'] == dist['x'] or src['y'] == dist['y'] :
		#１直線半円あり
		path += r*pi - 2*r 
	else:
		#1/4円あり
		if (src['x']-dist['x'])*(src['y']-dist['y']) > 0:
			spring.sort(key=lambda k: k['x'])
		else:
			spring.sort(key=lambda k: k['x'], reverse=True)

		#最長路問題
		L = [None] * n
		L[0] = spring[0]['y']
		length = 1
		for i in range(1,n):
			if L[length-1] < spring[i]['y']:
				L[length] = spring[i]['y']
				length += 1
			else:
				# 2分木探査風
				start = 0
				last = length - 1
				while abs(last-start) > 0:
					j = int((start+length)/2)
					if spring[i]['y'] < L[j] :
						last = j
					else:
						start = j
				
				L[last] = spring[i]['y']
		
		#半円があるかないか	
		if length < min(abs(src['x']-dist['x']),abs(src['y']-dist['y'])) + 1:
			path += (r*pi/2 - 2*r) * length
		else:
			path += (r*pi/2 - 2*r) * (length-1) + (r*pi - 2*r)
print(path)

Submission status: Time Limit Exceeded

Solution code:
from math import pi

input1 = list(map(int,input("").split(" ")))
N = int(input(""))
src = {"x":input1[0],"y":input1[1]}
dist = {"x":input1[2],"y":input1[3]}

spring = []
for i in range(N):
	temp = list(map(int,input("").split(" ")))
	if (src['x'] <= temp[0]) and (temp[0] <= dist['x']) or (dist['x'] <= temp[0]) and (temp[0] <= src['x']):
		if (src['y'] <= temp[1]) and (temp[1] <= dist['y']) or (dist['y'] <= temp[1]) and (temp[1] <= src['y']):
			spring.append({'x':temp[0], 'y':temp[1]})

# 基礎点
path = (abs(src['x']-dist['x']) + abs(src['y']-dist['y'])) * 100
if len(spring) > 0:
	r = 10
	if src['x'] == dist['x'] or src['y'] == dist['y'] :
		#１直線半円あり
		path += r*pi - 2*r 
	else:
		#1/4円あり
		if (src['x']-dist['x'])*(src['y']-dist['y']) > 0:
			spring.sort(key=lambda k: k['x'])
		else:
			spring.sort(key=lambda k: k['x'], reverse=True)
		#最長路問題
		L = [spring[0]['y']]
		length = 1
		for i in range(1,len(spring)):
			if L[length-1] < spring[i]['y']:
				L.append(spring[i]['y'])
				length += 1
			else:
				# 2分木探査風
				start = 0
				last = length - 1
				while abs(last-start) > 0:
					j = int((start+length)/2)
					if spring[i]['y'] < L[j] :
						last = j
					else:
						start = j
				
				L[last] = spring[i]['y']
		
		#半円があるかないか	
		if length < min(abs(src['x']-dist['x']),abs(src['y']-dist['y'])) + 1:
			path += (r*pi/2 - 2*r) * length
		else:
			path += (r*pi/2 - 2*r) * (length-1) + (r*pi - 2*r)
print(path)

Submission status: Time Limit Exceeded

Solution code:
from math import pi

input1 = list(map(int,input("").split(" ")))
N = int(input(""))
src = {"x":input1[0],"y":input1[1]}
dist = {"x":input1[2],"y":input1[3]}

spring = []
for i in range(N):
	temp = list(map(int,input("").split(" ")))
	if temp[0] in range(min(src['x'],dist['x']),max(src['x'],dist['x'])+1):
		if temp[1] in range(min(src['y'],dist['y']),max(src['y'],dist['y'])+1):
			spring.append({'x':temp[0], 'y':temp[1]})

# 基礎点
path = (abs(src['x']-dist['x']) + abs(src['y']-dist['y'])) * 100
if len(spring) > 0:
	r = 10
	if src['x'] == dist['x'] or src['y'] == dist['y'] :
		#１直線半円あり
		path += r*pi - 2*r 
	else:
		#1/4円あり
		if (src['x']-dist['x'])*(src['y']-dist['y']) > 0:
			spring.sort(key=lambda k: k['x'])
		else:
			spring.sort(key=lambda k: k['x'], reverse=True)
		#最長路問題
		L = [spring[0]['y']]
		length = 1
		for i in range(1,len(spring)):
			if L[length-1] < spring[i]['y']:
				L.append(spring[i]['y'])
				length += 1
			else:
				# 2分木探査風
				start = 0
				last = length - 1
				while abs(last-start) > 0:
					j = int((start+length)/2)
					if spring[i]['y'] < L[j] :
						last = j
					else:
						start = j
				
				L[last] = spring[i]['y']
		
		#半円があるかないか	
		if length < min(abs(src['x']-dist['x']),abs(src['y']-dist['y'])) + 1:
			path += (r*pi/2 - 2*r) * length
		else:
			path += (r*pi/2 - 2*r) * (length-1) + (r*pi - 2*r)
print(path)