POJ2318判断点在直线哪一侧+二分查找区间
Posted tennant
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ2318判断点在直线哪一侧+二分查找区间相关的知识,希望对你有一定的参考价值。
题目大意:给定一个矩形和一些线段,线段将矩形分割为从左至右的若干部分,之后给出一些玩具的坐标,求每个部分中玩具的数量
#include<cstdio>
#include<cstdlib>
#include<cstring>
using namespace std;
struct point {
int x, y;
};
struct Node {
point a, b;
}A[5010];
int pos[5010];
bool is_right(int xx, int yy, int mid) {
int ans = (A[mid].a.x - xx)*(A[mid].b.y - yy) - (A[mid].a.y - yy)*(A[mid].b.x - xx);
if (ans < 0)return false;
return true;
}
void search(int xx, int yy, int n) {
int left = 0, right = n - 1;
while (left <= right) {
int mid = (left + right) >> 1;
if (is_right(xx, yy, mid)) {
left = mid + 1;
}
else {
right = mid - 1;
}
}
pos[left]++;
}
int main() {
int n, m, i, j, x1, x2, y1, y2;
while (scanf("%d", &n), n) {
scanf("%d%d%d%d%d", &m, &x1, &y1, &x2, &y2);
for (i = 0; i < n; ++i) {
int xd, xu;
scanf("%d%d", &xu, &xd);
A[i].a.x = xu;
A[i].a.y = y1;
A[i].b.x = xd;
A[i].b.y = y2;
}
memset(pos, 0, sizeof(pos));
for (i = 0; i < m; ++i) {
int xx, yy;
scanf("%d%d", &xx, &yy);
search(xx, yy, n);
}
for (i = 0; i <= n; ++i) {
printf("%d: %d\n", i, pos[i]);
}
printf("\n");
}
return 0;
}
以上是关于POJ2318判断点在直线哪一侧+二分查找区间的主要内容,如果未能解决你的问题,请参考以下文章