POJ 2318 (点和线的关系)

题目:传送门

题意: 给你一个矩阵的左下角坐标和右上角坐标,然后给你 n 条线段,将 矩阵切成了 n + 1个区域。 然后给你 m 个玩具的坐标, 问你每个区域各有多少玩具。

题意: 判断一下点和线的关系就行辽, 然后二分优化一下。

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
#include <map>
#include <vector>
#include <set>
#include <string>
#include <math.h>
#define LL long long
#define mem(i, j) memset(i, j, sizeof(i))
#define rep(i, j, k) for(int i = j; i <= k; i++)
#define dep(i, j, k) for(int i = k; i >= j; i--)
#define pb push_back
#define make make_pair
#define INF INT_MAX
#define inf LLONG_MAX
#define PI acos(-1)
using namespace std;

const int N = 5050;

struct Point {
    int x, y;
    Point(int x = 0, int y = 0) : x(x), y(y) { } /// 构造函数
};

typedef Point Vector;
/// 向量+向量=向量, 点+向量=向量
Vector operator + (Vector A, Vector B) { return Vector(A.x + B.x, A.y + B.y); }
///点-点=向量
Vector operator - (Point A, Point B) { return Vector(A.x - B.x, A.y - B.y); }
///向量*数=向量
Vector operator * (Vector A, int p) { return Vector(A.x * p, A.y * p); }
///向量/数=向量
Vector operator / (Vector A, int p) { return Vector(A.x / p, A.y / p); }

const int eps = 1e-10;
int dcmp(int x) {
    if(fabs(x) < eps) return 0; else return x < 0 ? -1 : 1;
}

bool operator < (const Point& a, const Point& b) {
    return a.x == b.x ? a.y < b.y : a.x < b.x;
}

bool operator == (const Point& a, const Point &b) {
    return dcmp(a.x - b.x) == 0 && dcmp(a.y - b.y) == 0;
}

int Cross(Vector A, Vector B) { return A.x*B.y - A.y*B.x; } /// 叉积


Point P[N], Q[N], toy;
int n, m, x1, y1, x2, y2, x, y;
int ans[N];

int main() {
    while(scanf("%d", &n) == 1 && n) {
        scanf("%d %d %d %d %d", &m, &x1, &y1, &x2, &y2);
        rep(i, 0, n - 1) scanf("%d %d", &P[i].x, &Q[i].x), P[i].y = y1, Q[i].y = y2, ans[i] = 0;
        P[n].x = x2; P[n].y = y1; Q[n].x = x2; Q[n].y = y2; ans[n] = 0;
        while(m--) {
            scanf("%d %d", &toy.x, &toy.y);
            int l = 0, r = n, tmp;
            while(l <= r) {
                int mid = (l + r) >> 1;
                if(Cross(P[mid] - toy, Q[mid] - toy) < 0) {
                    tmp = mid; r = mid - 1;
                }
                else l = mid + 1;
            }
            ans[tmp]++;
        }
        rep(i, 0, n) printf("%d: %d\n", i, ans[i]);
        puts("");
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/Willems/p/12339821.html