CSES - Aalto Competitive Programming 2024 - wk10 - Mon - Results
Submission details
Task:Point in Polygon
Sender:odanobunaga8199
Submission time:2024-11-11 16:46:51 +0200
Language:C++ (C++20)
Status:READY
Result:ACCEPTED
Test results
testverdicttime
#1ACCEPTED0.01 sdetails
#2ACCEPTED0.01 sdetails
#3ACCEPTED0.00 sdetails
#4ACCEPTED0.00 sdetails
#5ACCEPTED0.00 sdetails
#6ACCEPTED0.00 sdetails
#7ACCEPTED0.00 sdetails
#8ACCEPTED0.00 sdetails
#9ACCEPTED0.00 sdetails
#10ACCEPTED0.00 sdetails
#11ACCEPTED0.01 sdetails
#12ACCEPTED0.00 sdetails
#13ACCEPTED0.00 sdetails

Code

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

struct Point {
    long long x;
    long long y;
};

bool pointOnSegment(const Point& p, const Point& q, const Point& r) {
    long long cross = (r.x - p.x) * (q.y - p.y) - (r.y - p.y) * (q.x - p.x);
    if (cross != 0)
        return false;
    
    if (min(p.x, r.x) <= q.x && q.x <= max(p.x, r.x) &&
        min(p.y, r.y) <= q.y && q.y <= max(p.y, r.y))
        return true;
    else
        return false;
}

string pointInPolygon(const Point& pt, const vector<Point>& polygon) {
    int n = polygon.size();
    bool onBoundary = false;
    int crossings = 0;
    
    for(int i = 0; i < n; ++i){
        int j = (i + 1) % n;
        Point p1 = polygon[i];
        Point p2 = polygon[j];
        
        if(pointOnSegment(p1, pt, p2)){
            onBoundary = true;
            break;
        }
        
        if( (p1.y <= pt.y && p2.y > pt.y) || (p2.y <= pt.y && p1.y > pt.y) ){
            double intersect_x;
            if(p2.y != p1.y){
                intersect_x = p1.x + (double)(p2.x - p1.x) * (pt.y - p1.y) / (p2.y - p1.y);
            } else {
                intersect_x = p1.x;
            }
            
            if(intersect_x > pt.x){
                crossings++;
            }
        }
    }
    
    if(onBoundary){
        return "BOUNDARY";
    }
    else{
        return (crossings % 2) ? "INSIDE" : "OUTSIDE";
    }
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    
    int n, m;
    cin >> n >> m;
    
    vector<Point> polygon(n);
    for(int i =0; i <n; ++i){
        cin >> polygon[i].x >> polygon[i].y;
    }
    
    vector<Point> queries(m);
    for(int i =0; i <m; ++i){
        cin >> queries[i].x >> queries[i].y;
    }
    
    for(int i =0; i <m; ++i){
        string res = pointInPolygon(queries[i], polygon);
        cout << res << "\n";
    }
}

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
...
Truncated

Test 2

Verdict: ACCEPTED

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

correct output
OUTSIDE
OUTSIDE
INSIDE
OUTSIDE
OUTSIDE
...

user output
OUTSIDE
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: 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