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 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
	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: Wrong Answer

Solution code:
import sys
pi = 3.1415926535897932384626

def input():
	return sys.stdin.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
	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)
	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: Wrong Answer

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
	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)
	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: Wrong Answer

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
	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)
	ans = (abs(x1-x2) + abs(y1-y2)) * 100 + num * 5 * pi - num * 20
	if num == min(abs(x1-x2), abs(y1-y2)):
		ans += 5 * pi
	print("{0:.15f}".format(ans))

Submission status: Wrong Answer

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
	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)
	ans = (abs(x1-x2) + abs(y1-y2)) * 100 + num * 5 * pi - num * 20
	if num == min((abs(x1-x2), abs(y1-y2))):
		ans += 5 * pi
	print("{0:.15f}".format(ans))

Submission status: Wrong Answer

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
	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)
	ans = (abs(x1-x2) + abs(y1-y2)) * 100 + num * 5 * pi - num * 20
	print("{0:.15f}".format(ans))

Submission status: Wrong Answer

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:
			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)
	ans = (abs(x1-x2) + abs(y1-y2)) * 100 + num * 5 * pi - num * 20
	print("{0:.15f}".format(ans))

Submission status: Wrong Answer

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:
			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: Wrong Answer

Solution code:
import sys
from math import pi

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

INF = 10**30

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: Wrong Answer

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

    assert(x1<=x2 and y1<=y2)
    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+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: Wrong Answer

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):
        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: Wrong Answer

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+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: Wrong Answer

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
    ans -= k*(20-5*pi)

print(ans)

Submission status: Wrong Answer

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]
f.sort(key = lambda x: (x[0], -x[1]))
lis = [inf] * N
lislen = 0
for i in range(N):
  if not (min(x1, x2) <= f[i][0] <= max(x1, x2) and min(y1, y2) <= f[i][1] <= max(y1, y2)):
    continue
  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: Wrong Answer

Solution code:
from bisect import bisect_left as bl
from math import pi
x1, y1, x2, y2 = map(int, input().split())
N = int(input())
inf = 10 ** 9
f = [list(map(int, input().split())) for _ in range(N)]
f.sort(key = lambda x: (x[0], -x[1]))
lis = [inf] * N
lislen = 0
for i in range(N):
  if not (min(x1, x2) <= f[i][0] <= max(x1, x2) and min(y1, y2) <= f[i][1] <= max(y1, y2)):
    continue
  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: Wrong Answer

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

Submission status: Wrong Answer

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 len(XY) <= 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: Wrong Answer

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 len(XY) < 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: Wrong Answer

Solution code:
# https://ikatakos.com/pot/programming_algorithm/dynamic_programming/longest_common_subsequence
from bisect import bisect_left
def lis(A: list):
    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
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: Wrong Answer

Solution code:
# https://ikatakos.com/pot/programming_algorithm/dynamic_programming/longest_common_subsequence
from bisect import bisect_left
def lis(A: list):
    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
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]
    print(100*(xmax-xmin + ymax-ymin) + (5*pi-20)*lis(Y))