CSES - Aalto Competitive Programming 2024 - wk10 - Mon - Results
Submission details
Task:Point in Polygon
Sender:laluj
Submission time:2024-11-11 17:23:38 +0200
Language:C++ (C++17)
Status:READY
Result:
Test results
testverdicttime
#10.01 sdetails
#20.03 sdetails
#3ACCEPTED0.00 sdetails
#4ACCEPTED0.00 sdetails
#50.00 sdetails
#60.00 sdetails
#7ACCEPTED0.00 sdetails
#80.00 sdetails
#90.00 sdetails
#100.00 sdetails
#110.00 sdetails
#12ACCEPTED0.00 sdetails
#13ACCEPTED0.00 sdetails

Code

#include <bits/stdc++.h>
using namespace std;

struct Point {
    long long x, y;
};

bool onSegment(Point a, Point b, Point p) {
    return p.x <= max(a.x, b.x) && p.x >= min(a.x, b.x) &&
           p.y <= max(a.y, b.y) && p.y >= min(a.y, b.y);
}

// Find the orientation of the triplet (a, b, c)
// 0 -> a, b and c are collinear
// 1 -> Clockwise
// 2 -> Counterclockwise
int orientation(Point a, Point b, Point c) {
    long long val = (b.y - a.y) * (c.x - b.x) - (b.x - a.x) * (c.y - b.y);
    if (val == 0) return 0;
    return (val > 0) ? 1 : 2;
}

// Check if two line segments intersect
bool doIntersect(Point a, Point b, Point c, Point d) {
    int o1 = orientation(a, b, c);
    int o2 = orientation(a, b, d);
    int o3 = orientation(c, d, a);
    int o4 = orientation(c, d, b);

    if (o1 != o2 && o3 != o4) return true;

    if (o1 == 0 && onSegment(a, b, c)) return true;
    if (o2 == 0 && onSegment(a, b, d)) return true;
    if (o3 == 0 && onSegment(c, d, a)) return true;
    if (o4 == 0 && onSegment(c, d, b)) return true;

    return false;
}

// Check if a point is inside a polygon
string pointInPolygon(Point point, const vector<Point>& polygon) {
    int n = polygon.size();
    Point extreme = {LLONG_MAX, point.y};
    int count = 0;

    for (int i = 0; i < n; i++) {
        Point next = polygon[(i + 1) % n];

        if (doIntersect(polygon[i], next, point, extreme)) {
            if (orientation(polygon[i], next, point) == 0 && onSegment(polygon[i], next, point))
                return "BOUNDARY";
            count++;
        }
    }

    return (count % 2 == 1) ? "INSIDE" : "OUTSIDE";
}

int main() {
    int n, m;
    cin >> n >> m;
    vector<Point> polygon(n);
    
    for (int i = 0; i < n; i++) {
        cin >> polygon[i].x >> polygon[i].y;
    }
    
    for (int i = 0; i < m; i++) {
        Point p;
        cin >> p.x >> p.y;
        cout << pointInPolygon(p, polygon) << endl;
    }

    return 0;
}

Test details

Test 1

Verdict:

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

correct output
INSIDE
OUTSIDE
INSIDE
INSIDE
INSIDE
...

user output
INSIDE
OUTSIDE
INSIDE
OUTSIDE
OUTSIDE
...
Truncated

Test 2

Verdict:

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

correct output
OUTSIDE
OUTSIDE
INSIDE
OUTSIDE
OUTSIDE
...

user output
INSIDE
OUTSIDE
INSIDE
OUTSIDE
OUTSIDE
...
Truncated

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:

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

correct output
INSIDE

user output
OUTSIDE

Test 6

Verdict:

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

correct output
INSIDE

user output
OUTSIDE

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:

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

correct output
INSIDE

user output
OUTSIDE

Test 9

Verdict:

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

correct output
OUTSIDE

user output
INSIDE

Test 10

Verdict:

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

correct output
OUTSIDE

user output
INSIDE

Test 11

Verdict:

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

correct output
INSIDE

user output
OUTSIDE

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