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

# 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:
				for j in range(length):
					if spring[i]['y'] <= L[j]:
						L[j] = spring[i]['y']
						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: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
from bisect import bisect_left, insort
from math import pi

def inpl(): return tuple(map(int, input().split()))
sx, sy, gx, gy = inpl()

yrev = False
if sx > gx:
    sx, sy, gx, gy = gx, gy, sx, sy

if sy > gy:
    sy, gy = 10**8 - 1 - sy, 10**8 - 1 - gy 
    yrev = True

N = int(input())
fountains = []

for _ in range(N):
    x, y = inpl()
    if yrev:
        y = 10**8 - 1 - y
    if (sx <= x <= gx) & (sy <= y <= gy):
        insort(fountains, [x, y])

L = []
if len(fountains):
    L.append(fountains[0][1])

for f in fountains: # 最長増加部分列
    x, y = f
    if L[-1] < y:
        L.append(y)
    else:
        L[bisect_left(L, y)] = y

if min(gx - sx, gy - sy) + 1 == len(L):
    print(((gx - sx)+(gy - sy))*100 - (20 - 5*pi)*(len(L)-1) + (10*pi - 20))
else:
    print(((gx - sx)+(gy - sy))*100 - (20 - 5*pi)*len(L))

Submission status: Time Limit Exceeded

Solution code:
def LIS(L):
  """数列L→最大増加部分列の長さ
  計算量はN^2"""
  res=0
  dp=[1]*N#最後がa[i] となるような最長の増加部分列の長さ
  for i in range(N):
    for j in range(i):
      if L[j]<L[i]:
        dp[i]=max(dp[i],dp[j]+1)
    res=max(res,dp[i])
  return res
 
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)]]
N=len(XY)
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: Time Limit Exceeded

Solution code:
# C
import numpy as np
from bisect import bisect

x1, y1, x2, y2 = map(int, input().split())
N = int(input())

xL = min(x1, x2)
xR = max(x1, x2)
yL = min(y1, y2)
yR = max(y1, y2)

xv = np.zeros(N, dtype='int')
yv = np.zeros(N, dtype='int')

ind = 0
for _ in range(N):
    x, y = map(int, input().split())
    if xL <= x and x <= xR and yL <= y and y <= yR:
        xv[ind] = x
        yv[ind] = y
        ind += 1

xv = xv[:ind]
yv = yv[:ind]

N = ind



if x1 < x2 and y1 > y2:
    direction = 'rightdown'
elif x1 > x2 and y1 < y2:
    direction = 'rightdown'
else:
    direction = 'rightup'

    
# sort
if direction == 'rightup':
    xv_s = np.argsort(xv)
else:
    xv_s = np.argsort(-xv)

# calculate
dp = [yR+1]*(ind)
for i in range(ind):
    y = yv[xv_s[i]]
    dp[bisect(dp,y)] = y
count_ = bisect(dp,yR)

diff = 5*np.pi - 20
diff2 = 10*np.pi - 20
if count_ == min((xR-xL), (yR-yL)) + 1:
    print(100*((xR-xL) + (yR-yL)) + (count_-1)*diff + diff2)
else:
    print(100*((xR-xL) + (yR-yL)) + count_*diff)

Submission status: Time Limit Exceeded

Solution code:
# C
import numpy as np
from bisect import bisect

x1, y1, x2, y2 = map(int, input().split())
N = int(input())

xL = min(x1, x2)
xR = max(x1, x2)
yL = min(y1, y2)
yR = max(y1, y2)

xv = np.zeros(N, dtype='int')
yv = np.zeros(N, dtype='int')

ind = 0
for _ in range(N):
    x, y = map(int, input().split())
    if xL <= x and x <= xR and yL <= y and y <= yR:
        xv[ind] = x
        yv[ind] = y
        ind += 1

xv = xv[:ind]
yv = yv[:ind]

N = ind



if x1 < x2 and y1 > y2:
    direction = 'upper'
elif x1 > x2 and y1 < y2:
    direction = 'upper'
else:
    direction = 'down'

    
# sort
xv_s = np.argsort(xv)
yv_ = yv[xv_s]

if direction == 'upper':
    yv_s_ = np.argsort(-yv_)
else:
    yv_s_ = np.argsort(yv_)

yv_s = np.zeros(ind, dtype='int')
for i in range(ind):
    yv_s[yv_s_[i]] = i


# calculate
dp = [ind+1]*(ind)
for i in range(ind):
    y = yv_s[i]
    dp[bisect(dp,y)] = y
count_ = bisect(dp,ind)

diff = 5*np.pi - 20
diff2 = 10*np.pi - 20
if count_ == min((xR-xL), (yR-yL)) + 1:
    print(100*((xR-xL) + (yR-yL)) + (count_-1)*diff + diff2)
else:
    print(100*((xR-xL) + (yR-yL)) + count_*diff)

Submission status: Accepted

Solution code:
import sys
import numpy as np

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

def longest_increasing_subsequence(seq):
    side = 'left'
    N = len(seq)
    INF = 10**18
    dp = np.full_like(seq, INF)
    for x in seq:
        i = np.searchsorted(dp, x, side)
        dp[i] = x
    return np.searchsorted(dp, INF - 1, side)

if sys.argv[-1] == 'ONLINE_JUDGE':
    import numba
    from numba.pycc import CC
    i8 = numba.from_dtype(np.int64)
    signature = (i8[:], )

    cc = CC('my_module')
    cc.export('longest_increasing_subsequence',
              signature)(longest_increasing_subsequence)
    cc.compile()

from my_module import longest_increasing_subsequence

x1, y1, x2, y2 = map(int, readline().split())
N = int(readline())
XY = np.array(read().split(), np.int64)

X = XY[::2]
Y = XY[1::2]

if x1 > x2:
    x1, x2 = x2, x1
    y1, y2 = y2, y1
if y1 > y2:
    y1, y2 = -y1, -y2
    Y = -Y

X -= x1
Y -= y1
x = x2 - x1
y = y2 - y1

cond = (X >= 0) & (X<=x) & (Y>=0) & (Y<=y)
X = X[cond]
Y = Y[cond]
ind = np.argsort(X)
X = X[ind]
Y = Y[ind]

n = longest_increasing_subsequence(Y)

answer = x+y
answer -= (0.2 - 0.1 * np.pi/2) * n
if n == min(x,y) + 1:
    answer += 0.1 * np.pi / 2
answer *= 100
print(answer)

Submission status: Accepted

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=-y1,-y2 # y1<=y2
        trans=True

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

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

    # 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)
    # LISの数が l より大きい時 -> 1回は無駄にしてしまう
    if(k>l):
        d-=(k-1)*(20-5*pi)
        d+=10*pi-20
    else:
        d-=k*(20-5*pi)
    print(d)
resolve()

Submission status: Accepted

Solution code:
import sys
import bisect
input = sys.stdin.readline

pi = 3.1415926535897938
x1, y1, x2, y2 = [int(v) for v in input().split()]
if x1 > x2:
    x1, x2 = x2, x1
    y1, y2 = y2, y1
ans = 100*(abs(x2-x1) + abs(y2-y1))
d = -1 if y1 > y2 else 1

xmax, xmin = max(x1,x2), min(x1,x2)
ymax, ymin = max(y1,y2), min(y1,y2)
n = int(input())
fountains = []
for i in range(n):
    x, y = [int(v) for v in input().split()]
    if xmin <= x <= xmax and ymin <= y <= ymax:
        fountains.append([x, y])

if x1 == x2 or y1 == y2:
    if len(fountains) == 1:
        ans += 10*pi - 20
else:
    fountains.sort()
    fountains = [-10**9] + [ i[1]*d for i in fountains ]
    m = len(fountains)
    lis_list = [ 10**9 for i in range(m) ]
    for i in fountains:
        a = bisect.bisect_left(lis_list, i)
        lis_list[a] = i

    k = -1
    for i in lis_list:
        if i != 10**9:
            k += 1

    if xmax - xmin + 1 == k or ymax - ymin + 1 == k:
        ans -= (k-1)*(20-5*pi)
        ans += 10*pi - 20
    else:       
        ans -= k*(20-5*pi)

print(ans)

Submission status: Accepted

Solution code:
from bisect import bisect_left as bl
from math import pi
fx = 1
fy = 1
x1, y1, x2, y2 = map(int, input().split())
if x1 > x2: fx = -1
if y1 > y2: fy = -1
N = int(input())
inf = 10 ** 9
f = [list(map(int, input().split())) for _ in range(N)]
f = [[p[0] * fx, p[1] * fy] for p in f if (min(x1, x2) <= p[0] <= max(x1, x2) and min(y1, y2) <= p[1] <= max(y1, y2))]
f.sort(key = lambda x: (x[0], -x[1]))
lis = [inf] * N
lislen = 0
for i in range(len(f)):
  t = f[i][1]
  ind = bl(lis, t)
  lis[ind] = t
  lislen = max(lislen, ind + 1)
if lislen == min(abs(x2 - x1), abs(y2 - y1)) + 1:
  print((abs(x2 - x1) + abs(y2 - y1)) * 100 - 20 * lislen + 10 * pi + 5 * pi * max(lislen - 1, 0))
else:
  print((abs(x2 - x1) + abs(y2 - y1)) * 100 - 20 * lislen + 5 * pi * lislen)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

from bisect import bisect_left
from math import pi

sx,sy,gx,gy = map(int,input().split())
N = int(input())
XY = [tuple(int(x) for x in input().split()) for _ in range(N)]

if sx > gx:
    sx,gx = gx,sx
    sy,gy = gy,sy
gx -= sx
gy -= sy

XY = [(x-sx,y-sy) for x,y in XY if 0<=x-sx<=gx]

if gy >= 0:
    XY = [(x,y) for x,y in XY if 0<=y<=gy]
else:
    gy = -gy
    XY = [(x,-y) for x,y in XY if 0<=-y<=gy]

# xは相異なるので...
XY.sort()
Y = [y for x,y in XY]

# longest increasing subseq
INF = 10**18
dp = [INF] * (len(Y)+1)
for y in Y:
    idx = bisect_left(dp,y)
    dp[idx] = y
L = bisect_left(dp,INF)

if L <= min(gx,gy):
    answer = 100*(gx+gy) + (10*pi/2-20) * L
else:
    answer = 100*(gx+gy) + (10*pi/2-20) * (L-1) + (10*pi-20)

print(answer)

Submission status: Accepted

Solution code:
def lis(A: list):
    # https://ikatakos.com/pot/programming_algorithm/dynamic_programming/longest_common_subsequence
    from bisect import bisect_left
    L = [A[0]]
    for a in A[1:]:
        if a > L[-1]:
            # Lの末尾よりaが大きければ増加部分列を延長できる
            L.append(a)
        else:
            # そうでなければ、「aより小さい最大要素の次」をaにする
            # 該当位置は、二分探索で特定できる
            L[bisect_left(L, a)] = a
    return len(L)

from math import pi
from operator import itemgetter
x1, y1, x2, y2 = map(int, input().split())
if x1 > x2:
    x1, x2 = x2, x1
    y1, y2 = y2, y1
xmin, xmax = sorted([x1, x2])
ymin, ymax = sorted([y1, y2])
N = int(input())
XY = []
for _ in range(N):
    X, Y = map(int, input().split())
    if xmin <= X <= xmax and ymin <= Y <= ymax:
        XY.append([X, Y])
if len(XY)==0:
    print(100*(xmax-xmin + ymax-ymin))
#elif x1==x2 or y1==y2:
#    print(100*(xmax-xmin + ymax-ymin) + (10*pi-20))
else:
    XY.sort()
    Y = list(map(itemgetter(1), XY))
    if y1 > y2:
        Y = Y[::-1]
    lis_ = lis(Y)
    if lis_==ymax-ymin+1 or lis_==xmax-xmin+1:
        lis_ -= 1
        print(100*(xmax-xmin + ymax-ymin) + (5*pi-20)*lis_ + (10*pi-20))
    else:
        print(100*(xmax-xmin + ymax-ymin) + (5*pi-20)*lis_)

Submission status: Accepted

Solution code:
import bisect
import sys
from math import atan
def lis(L):
    if not L:
        return 0
    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:
    x1, x2 = x2, x1
    y1, y2 = y2, y1

geta = 10**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]
    lb = lis(B)
    if lb == min(abs(x1-x2), abs(y1-y2)) + 1:
        print(100*mhd(x1-x2, y1-y2) + (lb-1)*(20*atan(1)-20) + 40*atan(1) - 20)
    else:
        print(100*mhd(x1-x2, y1-y2) + lb*(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]
    lb = lis(B)
    if lb == min(abs(x1-x2), abs(y1-y2)) + 1:
        print(100*mhd(x1-x2, y1-y2) + (lb-1)*(20*atan(1)-20) + 40*atan(1) - 20)
    else:
        print(100*mhd(x1-x2, y1-y2) + lb*(20*atan(1)-20))

Submission status: Accepted

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()

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)
    if len(lis) in (x2-x1+1, y2-y1+1):
        ans += (len(lis)-1) * (arc - 20)
        ans += arc*2 - 20
    else:
        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
        i = bisect(lds, -y)
        if i == len(lds):
            lds.append(-y)
        else:
            lds[i] = -y
    ans = 100 * (x2-x1 + y1-y2)
    if len(lds) in (x2-x1+1, y1-y2+1):
        ans += (len(lds)-1) * (arc - 20)
        ans += arc*2 - 20
    else:
        ans += len(lds) * (arc - 20)
    print(ans)

Submission status: Accepted

Solution code:
x1,y1,x2,y2=map(int,input().split())
fx=1
fy=1
if x1>x2:
	x1,x2=x2,x1
	fx=-1
if y1>y2:
	y1,y2=y2,y1
	fy=-1

n=int(input())
p=[]
for i in range(n):
	x,y=map(int,input().split())
	if x1<=x<=x2 and y1<=y<=y2:
		p.append((fx*x,fy*y))
p=[y for x,y in sorted(p)]

from bisect import bisect_left as bl

b=[]

for y in p:
	pos=bl(b,y)
	if len(b)<=pos:
		b.append(y)
	else:
		b[pos]=y
	
from math import pi

l=len(b)

if l==min(x2-x1,y2-y1)+1:
	print(100*(x2-x1+y2-y1)+(5*pi-20)*(l-1)+(10*pi-20))
else:
	print(100*(x2-x1+y2-y1)+(5*pi-20)*l);

Submission status: Accepted

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

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**15
mod = 10**9+7

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()
def pf(s): return print(s, flush=True)

class Seg():
    def __init__(self, na, default, func):
        if isinstance(na, list):
            n = len(na)
        else:
            n = na
        i = 1
        while 2**i <= n:
            i += 1
        self.D = default
        self.H = i
        self.N = 2**i
        if isinstance(na, list):
            self.A = [default] * (self.N) + na + [default] * (self.N-n)
            for i in range(self.N-1,0,-1):
                self.A[i] = func(self.A[i*2], self.A[i*2+1])
        else:
            self.A = [default] * (self.N*2)
        self.F = func

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

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

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

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

    def query(self, a, b):
        A = self.A
        l = a + self.N
        r = b + self.N
        res = self.D
        while l < r:
            if l % 2 == 1:
                res = self.merge(res, A[l])
                l += 1
            if r % 2 == 1:
                r -= 1
                res = self.merge(res, A[r])
            l >>= 1
            r >>= 1

        return res

def main():
    txy = LI()
    x1,y1,x2,y2 = txy
    n = I()
    a = [LI() for _ in range(n)]
    im = 10**8
    xs = set([x1,x2])
    ys = set([y1,y2])
    for x,y in a:
        xs.add(x)
        ys.add(y)
    xd = {}
    yd = {}
    i = 0
    for x in sorted(list(xs)):
        xd[x] = i
        i += 1
    i = 0
    for y in sorted(list(ys)):
        yd[y] = i
        i += 1
    x1 = xd[x1]
    x2 = xd[x2]
    y1 = yd[y1]
    y2 = yd[y2]
    a = [[xd[x],yd[y]] for x,y in a]

    if x1 > x2:
        x2,y2,x1,y1 = x1,y1,x2,y2
    if y1 > y2:
        y1 = im - y1
        y2 = im - y2 - y1
        for i in range(n):
            a[i][1] = im - a[i][1] - y1
    else:
        y2 -= y1
        for i in range(n):
            a[i][1] -= y1
    y1 = 0

    x2 -= x1
    for i in range(n):
        a[i][0] -= x1
    x1 = 0

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

    seg = Seg(y2,0,f)

    for x,y in sorted(a):
        if x < 0 or y < 0 or y > y2 or x > x2:
            continue
        t = seg.query(0,y)
        if seg.find(y) > t:
            continue
        seg.update(y, t+1)

    t = seg.total()
    kx = abs(txy[0]-txy[2])
    ky = abs(txy[1]-txy[3])
    r = (kx+ky) * 100
    if t == min(kx, ky) + 1:
        return r - t * 20 + math.pi * 5 * (t-1)  + math.pi * 10

    return r - t * 20 + math.pi * 5 * t


print(main())

Submission status: Accepted

Solution code:
from math import pi
from bisect import bisect_left as bl

def LIS(p) :
    route = []
    for y in p :
        pos = bl(route, y)
        if len(route) <= pos : route.append(y)
        else : route[pos] = y
    return len(route)


def main() :
    x1, y1, x2, y2 = map(int, input().split())
    xfilp = 1
    yflip = 1
    if x1 > x2 :
        xfilp = -1
        x1, x2 = x2, x1
    if y1 > y2 :
        yflip = -1
        y1, y2 = y2, y1
    n = int(input())
    p = []
    for i in range(n) :
        x, y = map(int, input().split())
        if x1 <= x <= x2 and y1 <= y <= y2 :
            p.append((x * xfilp, y * yflip))
    p = [y for x, y in sorted(p, key=lambda t:t[0])]
    foun = LIS(p)
    ans = 100*(x2-x1 + y2-y1) - (20 - pi*5)*foun
    if foun == min(x2-x1+1, y2-y1+1) :
        ans += pi*5
    print(ans)

main()

Submission status: Accepted

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
    base = decimal.Decimal(100) * (x2 - x1 + y2 - y1)

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

    if not fountains:
        return str(base)

    dpi = decimal.Decimal(pi)

    fountains.sort()
    fountains_y = list(map(operator.itemgetter(1), fountains))
    mc = lis(fountains_y)
    if mc == min(x2 - x1, y2 - y1) + 1:
        dist = base + (dpi * 5 - 20) * (mc - 1) + dpi * 10 - 20
    else:
        dist = base + (dpi * 5 - 20) * mc
    return str(dist)


print(solve())

Submission status: Accepted

Solution code:
from decimal import Decimal as D

from bisect import bisect_left as bl


# 最長増加部分列の長さを求める。
def LIS(L):
    best = []
    for i in L:
        # ソートされた順序を保ったまま、bestにiを挿入できる位置がpos
        pos = bl(best, i)
        if len(best) <= pos:
            best.append(i)
        else:
            best[pos] = i
    return len(best)


if __name__ == '__main__':
    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))
    # xの順序でソートし、yを入れたリスト
    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: Accepted

Solution code:
# -*- coding: utf-8 -*-
from bisect import bisect_left, insort
from math import pi
 
def inpl(): return tuple(map(int, input().split()))
sx, sy, gx, gy = inpl()
 
yrev = False
if sx > gx:
    sx, sy, gx, gy = gx, gy, sx, sy
 
if sy > gy:
    sy, gy = 10**8 - 1 - sy, 10**8 - 1 - gy 
    yrev = True
 
N = int(input())
fountains = []
 
for _ in range(N):
    x, y = inpl()
    if yrev:
        y = 10**8 - 1 - y
    if (sx <= x <= gx) & (sy <= y <= gy):
        fountains.append([x, y])

fountains.sort()
 
L = []
if len(fountains):
    L.append(fountains[0][1])
 
for f in fountains: # 最長増加部分列
    x, y = f
    if L[-1] < y:
        L.append(y)
    else:
        L[bisect_left(L, y)] = y
 
if min(gx - sx, gy - sy) + 1 == len(L):
    print(((gx - sx)+(gy - sy))*100 - (20 - 5*pi)*(len(L)-1) + (10*pi - 20))
else:
    print(((gx - sx)+(gy - sy))*100 - (20 - 5*pi)*len(L))

Submission status: Accepted

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

Solution code:
# C
from bisect import bisect
import math
x1, y1, x2, y2 = map(int, input().split())
N = int(input())

xL = min(x1, x2)
xR = max(x1, x2)
yL = min(y1, y2)
yR = max(y1, y2)

xyl = []

if x1 < x2 and y1 > y2:
    direction = 'rightdown'
elif x1 > x2 and y1 < y2:
    direction = 'rightdown'
else:
    direction = 'rightup'
    
    
# sort
if direction == 'rightup':
    for _ in range(N):
        x, y = map(int, input().split())
        if xL <= x and x <= xR and yL <= y and y <= yR:
            xyl.append((x, y-yL))
else:
    for _ in range(N):
        x, y = map(int, input().split())
        if xL <= x and x <= xR and yL <= y and y <= yR:
            xyl.append((x, yR-y))

ind = len(xyl)
xyl.sort()

# calculate
dp = [yR-yL+1]*(ind)
for i in range(ind):
    _, y = xyl[i]
    dp[bisect(dp,y)] = y
count_ = bisect(dp,yR-yL)

diff = 5*math.pi - 20
diff2 = 10*math.pi - 20
if count_ == min((xR-xL), (yR-yL)) + 1:
    print(100*((xR-xL) + (yR-yL)) + (count_-1)*diff + diff2)
else:
    print(100*((xR-xL) + (yR-yL)) + count_*diff)

Submission status: Accepted

Solution code:
import math
import bisect

def get_lis(S):
    dp = [float('inf')] * len(S)
    for i, (x, y) in enumerate(S):
        a = S[i][1]
        j = bisect.bisect_right(dp, a)
        dp[j] = a
    return bisect.bisect_left(dp, float('inf'))

x_1, y_1, x_2, y_2 = map(int, input().split())
N = int(input())

if x_1 <= x_2:
    x_s = x_1
    y_s = y_1
    x_t = x_2
    y_t = y_2
else:
    x_s = x_2
    y_s = y_2
    x_t = x_1
    y_t = y_1

x_min = min(x_1, x_2)
x_max = max(x_1, x_2)
y_min = min(y_1, y_2)
y_max = max(y_1, y_2)

S = []
for i in range(N):
    X, Y = map(int, input().split())
    if X < x_min or X > x_max or Y < y_min or Y > y_max:
        continue
    S.append((X, Y))

dist = 0
s_num = len(S)

S.sort()
if y_s > y_t:
    S.reverse()
    start_y = y_t
    end_y = y_s
else:
    start_y = y_s
    end_y = y_t

round_count = get_lis(S)
dist = (abs(x_t - x_s) + abs(y_t - y_s)) * 100 + (math.pi * 20 / 4 - 20) * round_count

if min(abs(x_t - x_s), abs(y_t - y_s)) + 1 == round_count:
    dist += math.pi * 5

print(dist)

Submission status: Accepted

Solution code:
#最大増加部分列（Longest Increasing Subsequence）
def LIS(L):
  """数列L→最大増加部分列の長さ
  計算量はNlogN"""
  from bisect import bisect
  best=[]
  for i in L:
    pos=bisect(best,i)
    if len(best)<=pos:
      best.append(i)
    else:
      best[pos]=i
  return len(best)

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)]]
N=len(XY)
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: Accepted

Solution code:
#最大増加部分列（Longest Increasing Subsequence）
def LIS(N,a,H):
  """数列の長さN, 数列a→最大増加部分列の長さ
  計算量はNlogN"""
  dp = [H+1]*len(a)#??
  for i,f in enumerate(a):
    dp[bisect(dp,f)] = f
  return bisect(dp,H)

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)]]
N=len(XY)
short=20-5*math.pi
long=10*math.pi-20
straight=100*(W+H)
from bisect import bisect
 
# find LIS
XY.sort()
Y = [y for x,y in XY]

fountain=LIS(N,Y,H)

if fountain<min(W,H)+1:
  print(straight-short*fountain)
else:
  print(straight-short*(fountain-1)+long)

Submission status: Accepted

Solution code:
#最大増加部分列（Longest Increasing Subsequence）
def LIS(N,a):
  """数列の長さN, 数列a→最大増加部分列の長さ
  計算量はN^2"""
  res=0
  dp=[1]*N#最後がa[i] となるような最長の増加部分列の長さ
  for i in range(N):
    for j in range(i):
      if XY[j][0]<XY[i][0] and XY[j][1]<XY[i][1]:
        dp[i]=max(dp[i],dp[j]+1)
    res=max(res,dp[i])
  return res

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)]]
N=len(XY)
short=20-5*math.pi
long=10*math.pi-20
straight=100*(W+H)
from bisect import bisect
 
# find LIS
XY.sort()
XY = [y for x,y in XY]

dp = [H+1]*len(XY)
 
for i,f in enumerate(XY):
  dp[bisect(dp,f)] = f
fountain = bisect(dp,H)
if fountain<min(W,H)+1:
  print(straight-short*fountain)
else:
  print(straight-short*(fountain-1)+long)

Submission status: Accepted

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 + 1, y2 - y1 + 1):
        c = d
    print "%.12f" % ((x2 - x1 + y2 - y1) * 100 + (m - 2) * d + c)
main()