BZOJ5008: 方师傅的房子

BZOJ5008: 方师傅的房子

https://lydsy.com/JudgeOnline/problem.php?id=5008

分析:

  • 二分这个点在哪个三角形所在顶角内,然后再判断是否在三角形内部。
  • 用叉积来判断。

代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <vector>
#include <iostream>
#include <cmath>
using namespace std;
#define N 100050
typedef long long ll;
struct Point {
	ll x,y;
	Point() {}
	Point(ll x_,ll y_) {x=x_,y=y_;}
	Point operator - (const Point &p) const {return Point(x-p.x,y-p.y);}
}a[N],b[N];
ll cross(const Point &p1,const Point &p2) {return p1.x*p2.y-p1.y*p2.x;}
int n;
int main() {
	scanf("%d",&n);
	int i;
	for(i=1;i<=n;i++) scanf("%lld%lld",&a[i].x,&a[i].y);
	for(i=1;i<=n;i++) b[i]=a[i]-a[1];
	int m,ans=0;
	scanf("%d",&m);
	int lstans=1;
	Point p=Point(0,0);
	while(m--) {
		ll dx,dy;
		scanf("%lld%lld",&dx,&dy);
		p.x+=lstans*dx,p.y+=lstans*dy;
		if(cross(p-a[1],a[2]-a[1])>0||cross(p-a[1],a[n]-a[1])<0) {
			lstans=-1;
		}else {
			int l=2,r=n+1;
			while(l<r) {
				int mid=(l+r)>>1;
				if(cross(p-a[1],a[mid]-a[1])<0) l=mid+1;
				else r=mid;
			}
			if(cross(p-a[l-1],a[l]-a[l-1])<=0) {
				ans++; lstans=1;
			}else {
				lstans=-1;
			}
		}
	}
	printf("%d\n",ans);
}
posted @ 2019-01-01 18:47  fcwww  阅读(135)  评论(0编辑  收藏  举报