Cod sursa(job #1098386)

Utilizator mihai995mihai995 mihai995 Data 4 februarie 2014 19:42:27
Problema Poligon Scor 0
Compilator cpp Status done
Runda Lista lui wefgef Marime 3.23 kb
#include <fstream>
#include <cstdlib>
#include <vector>
#include <algorithm>
using namespace std;

template<const int N>
class Poligon{
    struct Point{
        unsigned short int x, y;
    };

    struct Segm{
        Point A, B;
        Segm() : A(), B() {};
        Segm(Point A, Point B) : A(A), B(B) {};

        inline void ordX(){
            if (A.x > B.x)
                swap(A, B);
        }

        inline void ordY(){
            if (A.y > B.y)
                swap(A, B);
        }

        inline bool operator<(const Segm& S) const {
            return A.y < S.A.y || (A.y == S.A.y && B.y < S.B.y);
        }
    };

    Point P[N + 1];
    Segm S[N];
    int X[N], nrP, nrFile, startStep;
    vector<int> file[N];

    int binSrcX(int x){
        int ans = 0;
        for (int step = startStep, aux = startStep ; step ; step >>= 1, aux = ans ^ step)
            if (aux < nrFile && X[aux] <= x)
                ans = aux;
        return ans;
    }

    long long getSide(Segm S, long long x, long long y){
        Point A = S.A, B = S.B;
        return (y - A.y) * ((int)B.x - A.x) - (x - A.x) * ((int)B.y - A.y);
    }

    bool isInside(vector<int>& v, int x, int y){
        if (v.empty() || getSide(S[ v[0] ], x, y) < 0)
            return false;
        int ans = 0;
        for (int step = startStep, aux = startStep ; step ; step >>= 1, aux = ans ^ step)
            if ( aux < v.size() && getSide( S[ v[aux] ], x, y) >= 0 )
                ans = aux;
        return (ans & 1) == 0 || getSide( S[ v[ans] ], x, y ) == 0;
    }

public:
    Poligon(){
        nrP = 0;
    }

    void add(int x, int y){
        P[nrP].x = x;
        P[nrP].y = y;
        nrP++;
    }

    void preprocesare(){
        ///DETERMINE STEP SIZE
        startStep = 1;
        while ( (startStep << 1) <= nrP )
            startStep <<= 1;
        ///INDEX BY X
        for (int i = 0 ; i < nrP ; i++)
            X[i] = P[i].x;
        sort(X, X + nrP);
        nrFile = 1;
        for (int i = 1 ; i < nrP ; i++)
            if (X[i] != X[i - 1])
                X[nrFile++] = X[i];
        ///CREATE SEGMENTS
        int st, dr;
        P[nrP] = P[0];
        for (int i = 0 ; i < nrP ; i++){
            S[i] = Segm(P[i], P[i + 1]);
            S[i].ordY();
        }
        sort(S, S + nrP);
        ///CREATE FILES
        for (int i = 0 ; i < nrP ; i++){
            S[i].ordX();
            st = binSrcX(S[i].A.x);
            dr = binSrcX(S[i].B.x);

            for (int j = st ; j < dr ; j++)
                file[j].push_back(i);
        }
    }

    bool query(int x, int y){
        int P = binSrcX(x);
        if (isInside( file[P], x, y ))
            return true;
        if (P && X[P] == x)
            return isInside( file[P - 1], x, y );
        return false;
    }
};

Poligon<800> P;

ifstream in("poligon.in");
ofstream out("poligon.out");

int main(){
    int n, nrQ, x, y;

    in >> n >> nrQ;
    for (int i = 1 ; i <= n ; i++){
        in >> x >> y;
        P.add(x, y);
    }

    P.preprocesare();

    int ans = 0;
    while (nrQ--){
        in >> x >> y;
        ans += P.query(x, y);
    }
    out << ans << "\n";

    return 0;
}