CSES - Aalto Competitive Programming 2024 - wk10 - Mon - Results
Submission details
Task:Point in Polygon
Sender:esya_rae
Submission time:2024-11-11 16:31:45 +0200
Language:Python3 (PyPy3)
Status:READY
Result:
Test results
testverdicttime
#1ACCEPTED0.41 sdetails
#2--details
#3ACCEPTED0.04 sdetails
#4ACCEPTED0.04 sdetails
#5ACCEPTED0.05 sdetails
#6ACCEPTED0.04 sdetails
#7ACCEPTED0.04 sdetails
#8ACCEPTED0.04 sdetails
#9ACCEPTED0.04 sdetails
#10ACCEPTED0.04 sdetails
#11ACCEPTED0.04 sdetails
#12ACCEPTED0.04 sdetails
#13ACCEPTED0.04 sdetails

Code

import sys
import math
input = sys.stdin.readline

def gcd(a, b):
    while b != 0:
        a, b = b, a%b
    return a

class Point:
    def __init__(self, x, y):
        self.x = x
        self.y = y

    def __sub__(self, other):
        return Point(self.x - other.x, self.y - other.y)

    def __eq__(self, other):
        return self.x == other.x and self.y == other.y


class Line:
    def __init__(self, a, b):
        self.a = a
        self.b = b


def cross_product(p, q):
    #x1y2 − x2y1
    return p.x * q.y - p.y * q.x


def from_the_line(p, s):
    #(p−s1)×(p−s2)
    return cross_product(p - s.a, p - s.b)


def on_the_line(p, s):
    return (cross_product(p - s.a, p - s.b) == 0 and (min(s.a.y, s.b.y) <= p.y <= max(s.a.y, s.b.y)) and
            (min(s.a.x, s.b.x) <= p.x <= max(s.a.x, s.b.x)))


def same_line(p, q):
    return on_the_line(p.a, q) or on_the_line(p.b, q) or on_the_line(q.a, p) or on_the_line(q.b, p)


def end_intersection(p, q):
    return p.a == q.a or p.a == q.b or p.b == q.b or p.b == q.a


def middle_intersection(p, q):
    return ((from_the_line(p.a, q) > 0) != (from_the_line(p.b, q) > 0) and
            (from_the_line(q.a, p) > 0) != (from_the_line(q.b, p) > 0))


n, m = map(int, input().split())
polygon_points = []
polygon_lines = []
maxx = -math.inf
maxy = -math.inf

for i in range(n):
    x, y = map(int, input().split())
    p = Point(x, y)
    maxx = max(maxx, p.x)
    maxy = max(maxy, p.y)
    polygon_points.append(p)
    # if same_line(l1, l2) or end_intersection(l1, l2) or middle_intersection(l1, l2):
    #     print('YES')
    # else:
    #     print('NO')
# print(polygon_points)


for i in range(1, n):
    l = Line(polygon_points[i], polygon_points[i - 1])
    polygon_lines.append(l)
polygon_lines.append(Line(polygon_points[n - 1], polygon_points[0]))


for i in range(m):
    x, y = map(int, input().split())
    p = Point(x, y)

    on_the_border = False
    for l in polygon_lines:
        if on_the_line(p, l):
            on_the_border = True
            break

    if on_the_border:
        print('BOUNDARY')
    else:
        line = Line(p, Point(maxx * 10, maxy * 10 + 1))
        inter = 0
        for l in polygon_lines:
            if end_intersection(line, l) or middle_intersection(line, l):
                inter += 1
        if inter % 2 == 0:
            print('OUTSIDE')
        else:
            print('INSIDE')


Test details

Test 1

Verdict: ACCEPTED

input
100 1000
-7 -19
91 77
100 100
64 60
...

correct output
INSIDE
OUTSIDE
INSIDE
INSIDE
INSIDE
...

user output
INSIDE
OUTSIDE
INSIDE
INSIDE
INSIDE
...

Test 2

Verdict:

input
1000 1000
365625896 -113418831
278762563 38777445
250367343 -96991975
175866909 -129766978
...

correct output
OUTSIDE
OUTSIDE
INSIDE
OUTSIDE
OUTSIDE
...

user output
(empty)

Test 3

Verdict: ACCEPTED

input
4 1
1 5
5 5
5 1
1 1
...

correct output
INSIDE

user output
INSIDE

Test 4

Verdict: ACCEPTED

input
4 1
1 5
5 5
5 1
1 1
...

correct output
OUTSIDE

user output
OUTSIDE

Test 5

Verdict: ACCEPTED

input
4 1
1 100
2 50
1 20
0 50
...

correct output
INSIDE

user output
INSIDE

Test 6

Verdict: ACCEPTED

input
8 1
0 0
0 2
1 1
2 2
...

correct output
INSIDE

user output
INSIDE

Test 7

Verdict: ACCEPTED

input
4 4
0 0
3 0
3 4
0 4
...

correct output
INSIDE
BOUNDARY
OUTSIDE
BOUNDARY

user output
INSIDE
BOUNDARY
OUTSIDE
BOUNDARY

Test 8

Verdict: ACCEPTED

input
6 1
0 0
0 2
3 1
2 2
...

correct output
INSIDE

user output
INSIDE

Test 9

Verdict: ACCEPTED

input
3 1
0 0
1 1000000000
-3 0
1 1

correct output
OUTSIDE

user output
OUTSIDE

Test 10

Verdict: ACCEPTED

input
3 1
-100000 0
-1000000000 -999999999
1000000000 1000000000
0 0

correct output
OUTSIDE

user output
OUTSIDE

Test 11

Verdict: ACCEPTED

input
3 1
-100000 0
-999999999 -1000000000
1000 1000
0 0

correct output
INSIDE

user output
INSIDE

Test 12

Verdict: ACCEPTED

input
4 1
-4 1
-6 1
-6 -1
-4 -1
...

correct output
INSIDE

user output
INSIDE

Test 13

Verdict: ACCEPTED

input
3 1
0 10
0 -10
10 0
1 0

correct output
INSIDE

user output
INSIDE