Cod sursa(job #2328894)

Utilizator alex2kamebossPuscasu Alexandru alex2kameboss Data 26 ianuarie 2019 11:17:54
Problema Poligon Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.51 kb
#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
#include <algorithm>

using namespace std;

const pair<int,int> pref = {-1,-1};

vector <pair<int,int>> pct;
int n,m,x,y,sol;

inline int det(pair<int,int> &a, pair<int,int> &b){
    long long rez = 1LL*(a.first+1)*(b.second+1) - 1LL*(b.first+1)*(a.second+1);
    if(rez<0)
        return -1;
    if(rez>0)
        return 1;
    return 0;
}

int ant(int i){
    if(i==0)
        return n-1;
    return i-1;
}

int nex(int i){
    if(i==n)
        return 1;
    return i+1;
}

bool col(pair<int,int> &p1, pair<int,int> &p2, pair<int,int> &p3){
    return 1LL*(p2.first-p1.first)*(p3.second-p1.second) - 1LL*(p3.first-p1.first)*(p2.second-p1.second) == 0;
}

bool solve(pair <int,int> p){
    int u = 0;
    int aux;
    int d1, d2;

    for(int i = 0; i < n; ++i){
        if(pct[i].first>p.first)
            continue;
        d1 = det(p,pct[i]);
        d2 = det(p,pct[i+1]);
        if(d1*d2<0)
            if(col(p,pct[i],pct[i+1]))
                ++u;
        if(d1==0 && det(p,pct[ant(i)])*det(p,pct[nex(i)])<0)
            ++u;
    }

    //cout<<u<<"\n";
    return u%2;
}

int main()
{
    freopen("poligon.in","r",stdin);
    freopen("poligon.out","w",stdout);

    scanf("%d %d", &n,&m);
    for(int i=0;i<n;++i){
        scanf("\n%d %d", &x, &y);
        pct.push_back({x,y});
    }
    pct.push_back(pct.front());

    for(int i = 0; i < m; ++i){
        scanf("\n%d %d", &x, &y);
        if(solve({x,y}))
            ++sol;
    }

    cout<<sol;

    return 0;
}