CSES - Aalto Competitive Programming 2024 - wk10 - Mon - Results
Submission details
Task:Point in Polygon
Sender:HFalke
Submission time:2024-11-11 16:41:36 +0200
Language:C++ (C++17)
Status:READY
Result:ACCEPTED
Test results
testverdicttime
#1ACCEPTED0.01 sdetails
#2ACCEPTED0.02 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.00 sdetails
#12ACCEPTED0.00 sdetails
#13ACCEPTED0.00 sdetails

Code

#include <bits/stdc++.h>

using namespace std;

//Definitions for quicker writing
#define REP(i,a,b) for (int i = a; i < b; i++)
#define clz __builtin_clz
#define ctz __builtin_ctz
#define popct __builtin_popcount
#define PB push_back
#define MP make_pair
#define F first
#define S second
#define X real()
#define Y imag()

//Typedefs for quicker writing
typedef long long ll;
typedef vector<int> vi;
typedef vector<long long> vl;
typedef pair<int,int> pi;
typedef pair<long long, long long> pl;
typedef complex<long long> P;

//Max values
const long long lmx = LLONG_MAX;
const int imx = INT_MAX;
const ll INF = 1e9+7;

//cross(po v, po w){
//    return v.X * w.Y - v.Y * w.X;
//}

//ll shoelace(ll n){
//    ll sum = 0;
//    REP(i,0,n-1){
//        sum += cross(points[i],points[i+1]);
//    }
//    sum += cross(points[n-1],points[0]);
//    return abs(sum)/2;
//}

ll cross(P a, P b, P c) {
    P u = b - a;
    P v = c - a;
    ll cp = (conj(u)*v).Y;
    return (cp > 0) - (cp < 0);
}

bool comp(const P &a, const P &b) {
    return (a.X == b.X) ? (a.Y < b.Y) : (a.X < b.X);
}

bool mid(P a, P b, P c) {
    vector<P> v = {a, b, c};
    sort(v.begin(), v.end(), comp);
    return (v[1] == c);
}

// Function to check if a point is inside or outside the polygon
bool check(P a, P b, P c, P d) {
    // Calculate cross product of vectors a-b and a-c
    ll cp1 = cross(a, b, c);
    // Calculate cross product of vectors a-b and a-d
    ll cp2 = cross(a, b, d);
    // Calculate cross product of vectors c-d and c-a
    ll cp3 = cross(c, d, a);
    // Calculate cross product of vectors c-d and c-b
    ll cp4 = cross(c, d, b);
    // Check if points c and d are on different sides of line a-b
    if (cp1 * cp2 < 0 && cp3 * cp4 < 0) return 1;
    // Check if point a lies on line c-d and b lies on opposite side of c-d
    if (cp3 == 0 && mid(c, d, a) && cp4 < 0) return 1;
    // Check if point b lies on line c-d and a lies on opposite side of c-d
    if (cp4 == 0 && mid(c, d, b) && cp3 < 0) return 1;
    // Otherwise, return false
    return 0;
}

int main(){
    ll n,m;
    cin >> n >> m;

    vector<pl> vertices;
    vector<pl> points;
    ll a,b;
    REP(i,0,n){
        cin >> a >> b;
        vertices.PB({a,b});
    }

    REP(i,0,m){
        cin >> a >> b;
        points.PB({a,b});
    }

    vector<P> polygon;

    for (ll i = 0; i < n; i++) {
        polygon.push_back({vertices[i].first, vertices[i].second});
    }

    polygon.push_back(polygon[0]);

    for (ll i = 0; i < m; i++) {
        P point = {points[i].first, points[i].second};
        P infinity = {INF, INF};
        ll cnt = 0;
        ll flag = 0;
        for (ll j = 0; j < n; j++) {
            ll cp = cross(polygon[j], polygon[j+1], point);
            if (cp == 0 && mid(polygon[j], polygon[j+1], point)) {
                flag = 1;
                break;
            }
            cnt += check(polygon[j], polygon[j+1], point, infinity);
        }
        if (flag) cout << "BOUNDARY" << endl;
        else cout << (cnt & 1 ? "INSIDE" : "OUTSIDE") << endl;
    }

    return 0;
}

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