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: Accepted

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

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



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)

from math import pi
from bisect import bisect


#直線の場合
L = (W+H)*100

if (W == 0 or H == 0) and F:
  L += pi*10-20
elif N > 0:
  # find LIS
  F.sort()
  F = [y for x,y in F]

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

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

  n = bisect(dp,H)

  L -= (20-pi*5)*n
  if n == min(W,H) + 1:
    L += pi*5

print(L)

Submission status: Accepted

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)

xflip = 1
yflip = 1
x1, y1, x2, y2 = map(int,input().split())
if x1 > x2:
    xflip = -1
    x1, x2 = x2, x1
if y1 > y2:
    yflip = -1
    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:
        points.append((x*xflip,y*yflip))
points = [y for x,y in sorted(points)]
 
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: Runtime Error

Solution code:
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>

using namespace std;

const double PI = acos(-1.0);

template<typename T>
int LIS(const vector<T>& v){
    if(v.empty()) return 0;
	vector<T> a(v.size(), v[0]);
    auto end = a.begin();
    ++end;
	for(int i=1;i<v.size();i++){
		auto l = lower_bound(a.begin(), end, v[i]);
        if(l == end) ++end;
        *l = v[i];
	}
	return distance(a.begin(), end);
}

int main(){
    int x1, y1, x2, y2;
    while(cin >> x1 >> y1 >> x2 >> y2){
        if(y1 > y2){
            swap(x1, x2);
            swap(y1, y2);
        }
        int sgn = (x1 <= x2 ? 1 : -1);
        x1 *= sgn;
        x2 *= sgn;
        int N; cin >> N;
        vector<pair<int, int>> vp;
        for(int i=0;i<N;i++){
            int x, y; cin >> x >> y;
            x *= sgn;
            if(x1 <= x && x <= x2 && y1 <= y && y <= y2){
                vp.emplace_back(x, y);
            }
        }
        int width = min(x2-x1, y2-y1) + 1;
        double res = 1e12;
        for(int _=0;_<2;_++){
            sort(vp.begin(), vp.end());
            vector<int> v(vp.size());
            for(int i=0;i<v.size();i++) v[i] = vp[i].second;
            int l = LIS(v);
            cout << _ << " " << l << endl;
            if(l == width){
                res = min(100.0*(x2-x1+y2-y1) + 5*PI + l * (5*PI - 20), res);
            } else {
                res = min(100.0*(x2-x1+y2-y1) + l * (5*PI - 20), res);
            }
            for(auto& p : vp) swap(p.first, p.second);
        }
        printf("%.12lf\n", res);
    }
}

Submission status: Runtime Error

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 = -INF
		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:
			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:
			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
	if 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)
	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)
	print((abs(x1-x2) + abs(y1-y2)) * 100 + num * 5 * pi - num * 20)

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda:sys.stdin.readline().rstrip()
from math import pi
from bisect import bisect_left
def resolve():
    x1,y1,x2,y2=map(int,input().split())
    if(x1>x2): x1,y1,x2,y2=x2,y2,x1,y1 # x1<=x2
    trans=False
    if(y1>y2):
        y1,y2=10**8-y1,10**8-y2 # y1<=y2
        trans=True

    n=int(input())
    XY=[]
    for i in range(n):
        x,y=map(int,input().split())
        if(trans): y=10**8-y
        if(x1<=x<=x2 and y1<=y<=y2):
            XY.append((x,y))

    d=(x2-x1)+(y2-y1) # マンハッタン
    d*=100

    if(x1==x2 or y1==y2):
        assert(False)
        if(XY): d+=10*pi-20
        print(d)
        return

    # XYのLISを求めて、その個数*(20-5pi)を引けばよい
    XY.sort()
    C=[INF]*(n+1)
    for x,y in XY:
        i=bisect_left(C,y)
        C[i]=y
    k=C.index(INF)
    d-=k*(20-5*pi)
    print(d)
resolve()

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda:sys.stdin.readline().rstrip()
from math import pi
from bisect import bisect_left
def resolve():
    x1,y1,x2,y2=map(int,input().split())
    if(x1>x2): x1,y1,x2,y2=x2,y2,x1,y1 # x1<=x2
    trans=False
    if(y1>y2):
        y1,y2=10**8-y1,10**8-y2 # y1<=y2
        trans=True

    n=int(input())
    XY=[]
    for i in range(n):
        x,y=map(int,input().split())
        if(trans): x,y=10**8-x,10**8-y
        if(x1<=x<=x2 and y1<=y<=y2):
            XY.append((x,y))

    d=(x2-x1)+(y2-y1) # マンハッタン
    d*=100

    if(x1==x2 or y1==y2):
        if(XY): d+=10*pi-20
        print(d)
        return

    # XYのLISを求めて、その個数*(20-5pi)を引けばよい
    XY.sort()
    C=[INF]*n
    for x,y in XY:
        i=bisect_left(C,y)
        C[i]=y
    k=C.index(INF)
    d-=k*(20-5*pi)
    print(d)
resolve()

Submission status: Runtime Error

Solution code:
import bisect
import sys
from math import atan
def lis(L):
    seq = [L[0]]
    for i in L[1:]:
        if seq[-1] < i:
            seq.append(i)
        else:
            seq[bisect.bisect_left(seq, i)] = i
    return len(seq)

def mhd(x, y):
    return abs(x) + abs(y)

x1, y1, x2, y2 = map(int, sys.stdin.readline().split())
        
N = int(sys.stdin.readline())

if x1 == x2 or y1 == y2:
    if x1 == x2:
        if y1 > y2:
            y1, y2 = y2, y1
        for _ in range(N):
            X, Y = map(int, sys.stdin.readline().split())
            if X != x1:
                continue
            else:
                if y1 <= Y <= y2:
                    print(100*mhd(0, y1-y2) - 20 + 40*atan(1))
                    sys.exit()
                else:
                    print(100*mhd(0, y1-y2))
                    sys.exit()
    else:
        if x1 > x2:
            x1, x2 = x2, x1
        for _ in range(N):
            X, Y = map(int, sys.stdin.readline().split())
            if Y != y1:
                continue
            else:
                if x1 <= X <= x2:
                    print(100*mhd(x1-x2, 0) - 20 + 40*atan(1))
                    sys.exit()
                else:
                    print(100*mhd(x1-x2, 0))
                    sys.exit()
if x1 > x2:
    y1, y2 = y2, y1
          

geta = 19**9+7
B = []
if y1 < y2:
    for _ in range(N):
        X, Y = map(int, sys.stdin.readline().split())
        if x1 <= X <= x2 and y1 <= Y <= y2:
            B.append(X*geta + Y)
    B.sort()
    B = [b%geta for b in B]
    print(100*mhd(x1-x2, y1-y2)*100 + lis(B)*(20*atan(1)-20))
    
else:
    for _ in range(N):
        X, Y = map(int, sys.stdin.readline().split())
        if x1 <= X <= x2 and y2 <= Y <= y1:
            B.append(X*geta - Y)
    B.sort()
    B = [b%geta for b in B]
    print(100*mhd(x1-x2, y1-y2)*100 + lis(B)*(20*atan(1)-20))

Submission status: Runtime Error

Solution code:
import math
from bisect import bisect
x1,y1,x2,y2 = map(int,input().split())
N = int(input())
src = [tuple(map(int,input().split())) for i in range(N)]
src.sort()
if x1 > x2:
    x1,x2 = x2,x1
    y1,y2 = y2,y1

arc = 5 * math.pi

if x1 == x2:
    if y1 > y2:
        y1,y2 = y2,y1
    ans = 100 * (y2-y1)
    for x,y in src:
        if x == x1 and y1 < y < y2:
            ans += arc*2 - 20
            break
    print(ans)
    exit()

if y1 == y2:
    if x1 > x2:
        x1,x2 = x2,x1
    ans = 100 * (x2-x1)
    for x,y in src:
        if y == y1 and x1 < x < x2:
            ans += arc*2 - 20
            break
    print(ans)
    exit()

#assert x1 < x2

if y1 < y2:
    lis = []
    for x,y in src:
        if x < x1: continue
        if x > x2: break
        if not y1 <= y <= y2: continue
        i = bisect(lis, y)
        if i == len(lis):
            lis.append(y)
        else:
            lis[i] = y
    ans = 100 * (x2-x1 + y2-y1)
    ans += len(lis) * (arc - 20)
    print(ans)
else:
    lds = []
    for x,y in src:
        if x < x1: continue
        if x > x2: break
        if not y2 <= y <= y1: continue
        if i == 0:
            lds.append(y)
        else:
            lds[-i] = y
    ans = 100 * (x2-x1 + y1-y2)
    ans += len(lds) * (arc - 20)
    print(ans)

Submission status: Runtime Error

Solution code:
import math
from bisect import bisect
x1,y1,x2,y2 = map(int,input().split())
N = int(input())
src = [tuple(map(int,input().split())) for i in range(N)]
src.sort()
if x1 > x2:
    x1,x2 = x2,x1
    y1,y2 = y2,y1

arc = 5 * math.pi

if x1 == x2:
    if y1 > y2:
        y1,y2 = y2,y1
    ans = 100 * (y2-y1)
    for x,y in src:
        if x == x1 and y1 < y < y2:
            ans += arc*2 - 20
            break
    print(ans)
    exit()

if y1 == y2:
    if x1 > x2:
        x1,x2 = x2,x1
    ans = 100 * (x2-x1)
    for x,y in src:
        if y == y1 and x1 < x < x2:
            ans += arc*2 - 20
            break
    print(ans)
    exit()

assert x1 < x2

if y1 < y2:
    lis = []
    for x,y in src:
        if x < x1: continue
        if x > x2: break
        if not y1 <= y <= y2: continue
        i = bisect(lis, y)
        if i == len(lis):
            lis.append(y)
        else:
            lis[i] = y
    ans = 100 * (x2-x1 + y2-y1)
    ans += len(lis) * (arc - 20)
    print(ans)
else:
    lds = []
    for x,y in src:
        if x < x1: continue
        if x > x2: break
        if not y2 <= y <= y1: continue
        if i == 0:
            lds.append(y)
        else:
            lds[-i] = y
    ans = 100 * (x2-x1 + y1-y2)
    ans += len(lds) * (arc - 20)
    print(ans)

Submission status: Runtime Error

Solution code:
import math
from bisect import bisect
x1,y1,x2,y2 = map(int,input().split())
N = int(input())
src = [tuple(map(int,input().split())) for i in range(N)]
src.sort()
if x1 > x2:
    x1,x2 = x2,x1
    y1,y2 = y2,y1

arc = 5 * math.pi

if x1 == x2:
    if y1 > y2:
        y1,y2 = y2,y1
    ans = 100 * (y2-y1)
    for x,y in src:
        if x == x1 and y1 < y < y2:
            ans += arc*2 - 20
            break
    print(ans)
    exit()

if y1 == y2:
    if x1 > x2:
        x1,x2 = x2,x1
    ans = 100 * (x2-x1)
    for x,y in src:
        if y == y1 and x1 < x < x2:
            ans += arc*2 - 20
            break
    print(ans)
    exit()

assert x1 < x2

if y1 < y2:
    lis = []
    for x,y in src:
        if x < x1: continue
        if x > x2: break
        if not y1 <= y <= y2: continue
        i = bisect(lis, y)
        if i == len(lis):
            lis.append(y)
        else:
            lis[i] = y
    ans = 100 * (x2-x1 + y2-y1)
    ans += len(lis) * (arc - 20)
    print(ans)
else:
    lds = []
    for x,y in src:
        if x < x1: continue
        if x > x2: break
        if not y2 <= y <= y1: continue
        print(x,y,i)
        if i == 0:
            lds.append(y)
        else:
            lds[-i] = y
    ans = 100 * (x2-x1 + y1-y2)
    ans += len(lds) * (arc - 20)
    print(ans)

Submission status: Runtime Error

Solution code:
import math
from bisect import bisect
x1,y1,x2,y2 = map(int,input().split())
N = int(input())
src = [tuple(map(int,input().split())) for i in range(N)]
src.sort()
if x1 > x2:
    x1,x2 = x2,x1
    y1,y2 = y2,y1

arc = 5 * math.pi

if x1 == x2:
    if y1 > y2:
        y1,y2 = y2,y1
    ans = 100 * (y2-y1)
    for x,y in src:
        if x == x1 and y1 < y < y2:
            ans += arc*2 - 20
            break
    print(ans)
    exit()

if y1 == y2:
    if x1 > x2:
        x1,x2 = x2,x1
    ans = 100 * (x2-x1)
    for x,y in src:
        if y == y1 and x1 < x < x2:
            ans += arc*2 - 20
            break
    print(ans)
    exit()

assert x1 < x2

if y1 < y2:
    lis = []
    for x,y in src:
        if x < x1: continue
        if x > x2: break
        if not y1 <= y <= y2: continue
        i = bisect(lis, y)
        if i == len(lis):
            lis.append(y)
        else:
            lis[i] = y
    ans = 100 * (x2-x1 + y2-y1)
    ans += len(lis) * (arc - 20)
    print(ans)
else:
    print(src)
    lds = []
    for x,y in src:
        if x < x1: continue
        if x > x2: break
        if not y2 <= y <= y1: continue
        print(x,y,i)
        if i == 0:
            lds.append(y)
        else:
            lds[-i] = y
    ans = 100 * (x2-x1 + y1-y2)
    ans += len(lds) * (arc - 20)
    print(ans)

Submission status: Runtime Error

Solution code:
import math

M=[int(x) for x in input().split()]
N=int(input())
xd=abs(M[0]-M[2])
yd=abs(M[1]-M[3])
F=[]
for i in range(N):
  a,b=map(int,input().split())
  if min(M[0],M[2])<= a <= max(M[0],M[2]) and min(M[1],M[3])<= b <= max(M[1],M[3]):
    F.append([a,b])
F.sort(key=lambda x:x[0])
dp=[200001 for x in range(len(F)+1)]

def lower_bound(a,n):
  l,r=0,len(a)-1
  while r>=l:
    mid=int(l+(r-l)/2)
    if a[mid]>=n:  
      r=mid-1
    else:
      l=mid+1
  return l

for i in range(len(F)):
  cnt=lower_bound(dp,F[i][1])
  dp[cnt]=min(dp[cnt],F[i][1])

res=0
for j in range(len(F)-1,-1,-1):
  if dp[j]<200001:
    res=j+1
    break

if res==min(xd,yd)+1:
  ans=(xd)*100+(yd)*100+(math.pi*10-20)-(20-math.pi*5)*(res-1)  
else:
  ans=(xd)*100+(yd)*100-(20-math.pi*5)*res

print(ans)

Submission status: Runtime Error

Solution code:
import math

M=[int(x) for x in input().split()]
N=int(input())
xd=abs(M[0]-M[2])
yd=abs(M[1]-M[3])
F=[]
for i in range(N):
  a,b=map(int,input().split())
  if min(M[0],M[2])<= a <= max(M[0],M[2]) and min(M[1],M[3])<= b <= max(M[1],M[3]):
    F.append([a,b])
F.sort(key=lambda x:x[0])
dp=[200000 for x in range(len(F))]

def lower_bound(a,n):
  l,r=0,len(a)-1
  while r>=l:
    mid=int(l+(r-l)/2)
    if a[mid]>=n:  
      r=mid-1
    else:
      l=mid+1
  return l

for i in range(len(F)):
  cnt=lower_bound(dp,F[i][1])
  dp[cnt]=min(dp[cnt],F[i][1])

res=0
for j in range(len(F)-1,-1,-1):
  if dp[j]<200000:
    res=j+1
    break

if res==min(xd,yd)+1:
  ans=(xd)*100+(yd)*100+(math.pi*10-20)-(20-math.pi*5)*(res-1)  
else:
  ans=(xd)*100+(yd)*100-(20-math.pi*5)*res

print(ans)

Submission status: Runtime Error

Solution code:
import math

M=[int(x) for x in input().split()]
N=int(input())
xd=abs(M[0]-M[2])+1
yd=abs(M[1]-M[3])+1
dp=[[0 for i in range(yd)] for j in range(xd)]
res=0
for i in range(N):
  a,b=map(int,input().split())
  if min(M[0],M[2])<= a <= max(M[0],M[2]) and min(M[1],M[3])<= b <= max(M[1],M[3]):
    dp[a-min(M[0],M[2])][b-min(M[1],M[3])]='F'
    res+=1

for i in range(1,xd):
  for j in range(1,yd):
    if dp[i-1][j]=='F' or dp[i][j-1]=='F':
      dp[i][j]=dp[i-1][j-1]+1
    else:
      if dp[i][j]!='F':
        dp[i][j]=max(dp[i-1][j],dp[i][j-1])
      
cnt=dp[xd-1][yd-1]
if xd==1 or yd==1:
  ans=(xd-1)*100+(yd-1)*100+(math.pi*10-20)*res
else:
  ans=(xd-1)*100+(yd-1)*100-(20-math.pi*5)*cnt

print(ans)

Submission status: Runtime Error

Solution code:
import math

M=[int(x) for x in input().split()]
N=int(input())
xd=abs(M[0]-M[2])+1
yd=abs(M[1]-M[3])+1
dp=[[0 for i in range(yd)] for j in range(xd)]
res=0
for i in range(N):
  a,b=map(int,input().split())
  if min(M[0],M[2])<= a <= max(M[0],M[2]) and min(M[1],M[3])<= b <= max(M[1],M[3]):
    dp[a-min(M[0],M[2])][b-min(M[1],M[3])]='F'
    res+=1

for i in range(1,xd):
  for j in range(1,yd):
    if dp[i-1][j]=='F' or dp[i][j-1]=='F':
      dp[i][j]=dp[i-1][j-1]+1
    else:
      if dp[i][j]!='F':
        dp[i][j]=max(dp[i-1][j],dp[i][j-1])
      
cnt=dp[xd-1][yd-1]
if xd==1 or yd==1:
  ans=(xd-1)*100+(yd-1)*100+(math.pi*10-20)
else:
  ans=(xd-1)*100+(yd-1)*100-(20-math.pi*5)*cnt

print(ans)

Submission status: Runtime Error

Solution code:
import math

M=[int(x) for x in input().split()]
N=int(input())
xd=abs(M[0]-M[2])+1
yd=abs(M[1]-M[3])+1
dp=[[0 for i in range(yd)] for j in range(xd)]
res=0
for i in range(N):
  a,b=map(int,input().split())
  if min(M[0],M[2])<= a <= max(M[0],M[2]) and min(M[1],M[3])<= b <= max(M[1],M[3]):
    dp[a-min(M[0],M[2])][b-min(M[1],M[3])]='F'
    res+=1

for i in range(1,xd):
  for j in range(1,yd):
    if dp[i-1][j]=='F' or dp[i][j-1]=='F':
      dp[i][j]=d[i-1][j-1]+1
    else:
      if dp[i][j]!='F':
        dp[i][j]=max(dp[i-1][j],dp[i][j-1])
      
cnt=dp[xd-1][yd-1]
if xd==1 or yd==1:
  ans=(xd-1)*100+(yd-1)*100+(math.pi*10-20)
else:
  ans=(xd-1)*100+(yd-1)*100-(20-math.pi*5)*cnt

print(ans)

Submission status: Runtime Error

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) > 1:
					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: Runtime Error

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]})

# print(spring)
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 1:
					j = int((start+length)/2)
					if spring[i]['y'] < L[j] :
						last = j
					else:
						start = j
					if (last-start) == 1:
						L[last] = x
						break
						
		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: Runtime Error

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]})

# print(spring)
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[0] = spring[0]['y']
		length = 1
		for i in range(1,N):
			if L[lenth] < spring[i]['y']
				L.append(spring[i]['y'])
				length++
			else:
				for j in range(0,length):
					if spring[i]['y'] >= L[j]:
						temp = i
						break
				L[temp] = spring[i]['y']
		
		path += (r*pi/2 - 2*r) * length

print(path)

Submission status: Runtime Error

Solution code:
import operator
from bisect import bisect_left
from math import pi
import decimal


def lis(A):
    L = [A[0]]
    for a in A[1:]:
        if a > L[-1]:
            L.append(a)
        else:
            L[bisect_left(L, a)] = a
    return len(L)


def solve():
    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((x, y))
    fountains.sort()

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

    fountains_y = list(map(operator.itemgetter(1), fountains))
    mc = lis(fountains_y)
    dpi = decimal.Decimal(pi)
    base = decimal.Decimal((x2 - x1 + y2 - y1) * 100 - mc * 20)

    return str(base + mc * 5 * dpi)


print(solve())

Submission status: Runtime Error

Solution code:
def LIS(L):
  from bisect import bisect
  seq=[]
  for i in L:
    pos=bisect(seq,i)
    if len(seq)<=pos:
	  seq.append(i)
    else:
      seq[pos]=i
  return len(seq)
 
import math 
x1,y1,x2,y2=map(int,input().split())
N=int(input())
W=abs(x2-x1)
H=abs(y2-y1)
xsgn=2*(x2>x1)-1
ysgn=2*(y2>y1)-1
XY=[]
for i in range(N):
  x,y=map(int,input().split())
  if (x-x1)*(x-x2)<=0 and (y-y1)*(y-y2)<=0:
    XY+=[[xsgn*(x-x1),ysgn*(y-y1)]]
short=20-5*math.pi
long=10*math.pi-20
straight=100*(W+H)
 
XY.sort()
Y = [y for x,y in XY]
 
fountain=LIS(Y)
 
if fountain<min(W,H)+1:
  print(straight-short*fountain)
else:
  print(straight-short*(fountain-1)+long)

Submission status: Runtime Error

Solution code:
def LIS(L):
  from bisect import bisect
  seq=[]
  for i in L:
    pos=bisect(seq,i)
    if len(seq)<=pos:
	  seq.append(i)
    else:
      best[seq]=i
  return len(seq)
 
import math 
x1,y1,x2,y2=map(int,input().split())
N=int(input())
W=abs(x2-x1)
H=abs(y2-y1)
xsgn=2*(x2>x1)-1
ysgn=2*(y2>y1)-1
XY=[]
for i in range(N):
  x,y=map(int,input().split())
  if (x-x1)*(x-x2)<=0 and (y-y1)*(y-y2)<=0:
    XY+=[[xsgn*(x-x1),ysgn*(y-y1)]]
short=20-5*math.pi
long=10*math.pi-20
straight=100*(W+H)
 
XY.sort()
Y = [y for x,y in XY]
 
fountain=LIS(Y)
 
if fountain<min(W,H)+1:
  print(straight-short*fountain)
else:
  print(straight-short*(fountain-1)+long)

Submission status: Runtime Error

Solution code:
from sys import stdin
from math import pi

# 그냥 가면 100(|x1 - x2| + |y1 - y2|)
# junction 통과할 때마다 답이 좋아짐. 이 개수를 늘려야?
C = 100
J90 = -20 + pi * 2*10/4
J180 = -20 + pi * 2*10/2

x1, y1, x2, y2 = map(int, stdin.readline().split())
N = int(stdin.readline())
x = [0] * N
y = [0] * N
for i in range(N):
    x[i], y[i] = map(int, stdin.readline().split())

for i in range(N):
    if x1 > x2: x[i] = int(1e8) - x[i]
    if y1 > y2: y[i] = int(1e8) - y[i]

if x1 > x2: x1, x2 = int(1e8) - x1, int(1e8) - x2
if y1 > y2: y1, y2 = int(1e8) - y1, int(1e8) - y2

ans = 0
P = [(x[i], y[i]) for i in range(N) if x1 <= x[i] <= x2 and y1 <= y[i] <= y2]
if x1 == x2 or y1 == y2:
    ans = (-20 + 10*pi) * len(P)
elif len(P) == 0:
    ans = 0
elif len(P) > 0:
    P.sort()

    def lis(a):
        import bisect
        cur = [a[0]]
        for y in a[1:]:
            if cur[-1] < y:
                cur.append(y)
            else:
                cur[bisect.bisect_left(cur, y)] = y
        return len(cur)

    ans = J90 * lis([y for x, y in P])
    if P[0][0] == x1 and y1 < P[0][1] < y2:
        ans = J180 + J90 * lis([y for x, y in P if x > x1 and P[0][1] < y])
    if x1 < P[0][0] < x2 and P[0][1] == y1:
        ans = J180 + J90 * lis([y for x, y in P if y > y1 and P[0][0] < x])
    if P[-1][0] == x2 and y1 < P[-1][1] < y2:
        ans = J180 + J90 * lis([y for x, y in P if x < x2 and P[-1][1] > y])
    if x1 < P[-1][0] < x2 and P[-1][1] == y2:
        ans = J180 + J90 * lis([y for x, y in P if y < y2 and P[-1][0] > x])

print("%.20f" % (C * (x2 - x1 + y2 - y1) + ans))

Submission status: Runtime Error

Solution code:
from sys import stdin
from math import pi

# 그냥 가면 100(|x1 - x2| + |y1 - y2|)
# junction 통과할 때마다 답이 좋아짐. 이 개수를 늘려야?
C = 100
J90 = -20 + pi * 2*10/4
J180 = -20 + pi * 2*10/2

x1, y1, x2, y2 = map(int, stdin.readline().split())
N = int(stdin.readline())
x = [0] * N
y = [0] * N
for i in range(N):
    x[i], y[i] = map(int, stdin.readline().split())

for i in range(N):
    if x1 > x2: x[i] = int(1e8) - x[i]
    if y1 > y2: y[i] = int(1e8) - y[i]

if x1 > x2: x1, x2 = int(1e8) - x1, int(1e8) - x2
if y1 > y2: y1, y2 = int(1e8) - y1, int(1e8) - y2

ans = 0
P = [(x[i], y[i]) for i in range(N) if x1 <= x[i] <= x2 and y1 <= y[i] <= y2]
if x1 == x2 or y1 == y2:
    ans = (-20 + 10*pi) * len(P)
elif len(P) == 0:
    ans = 0
elif len(P) > 0:
    P.sort()

    def lis(a):
        import bisect
        cur = [a[0]]
        for y in a[1:]:
            if cur[-1] < y:
                cur.append(y)
            else:
                cur[bisect.bisect_left(cur, y)] = y
        return len(cur)

    ans = J90 * lis([y for x, y in P])
    if P[0][0] == x1 and y1 < P[0][1] < y2:
        ans = J180 + J90 * lis([y for x, y in P if x > x1 and P[0][1] < y])
    if x1 < P[0][0] < x2 and P[0][1] == y1:
        ans = J180 + J90 * lis([y for x, y in P if y > y1 and P[0][0] < x])
    if P[-1][0] == x2 and y1 < P[-1][1] < y2:
        ans = J180 + J90 * lis([y for x, y in P if x < x2 and P[0][1] < y])
    if x1 < P[-1][0] < x2 and P[-1][1] == y2:
        ans = J180 + J90 * lis([y for x, y in P if y < y2 and P[0][0] < x])

print("%.20f" % (C * (x2 - x1 + y2 - y1) + ans))