POJ 2318 TOYS | 二分+判断点在多边形内
题意:
给一个矩形的区域(左上角为(x1,y1) 右下角为(x2,y2)),给出n对(u,v)表示(u,y1) 和 (v,y2)构成线段将矩形切割
这样构成了n+1个多边形,再给出m个点,问每个多边形内有多少个点.
读入为n,m,x1,y1,x2,y2
n个数对(u,v),m个数对(x,y) (n,m<=5000)
题解:
很暴力的想法是对于每个点,枚举每个多边形进行检查.
但是多组数据就很江
考虑一下判断点在多边形内的射线法可知
枚举一个多边形的时候就可以知道点在多边形的左边还是右边
这样我们对每个点二分一下他属于那个多边形,依靠上述性质就可以在nlogn时间内做完
#include<cstdio>
#include<algorithm>
#include<cstring>
typedef long long ll;
#define N 5010
using namespace std;
int n,m,x1,x2,y1,y2,up[N],down[N],cnt[N],t;
struct point
{
int x,y;
point (){} ;
point (int _x,int _y) :
x(_x),y(_y) {};
bool operator < (const point &rhs) const
{
if (x==rhs.x) return y<rhs.y;
return x<rhs.x;
}
inline int operator * (const point &rhs) const
{
return x*rhs.y-y*rhs.x;
}
inline point operator - (const point &rhs) const
{
return point(x-rhs.x,y-rhs.y);
}
friend inline int dot (const point &lhs,const point &rhs)
{
return lhs.x*rhs.x+lhs.y*rhs.y;
}
}toy[N];
bool isinline (const point &p1,const point &p2,const point &p3)
{
int det=(p1-p3)*(p2-p3);
if (det!=0) return 0;
int Dot=dot(p1-p3,p2-p3);
return Dot<=0;
}
struct polygon
{
point p[10];
int inner (const point &pp)
{
int cnt=0;
for (int i=1;i<=4;i++)
{
if (isinline(p[i],p[i+1],pp)) return 1;
int d1=p[i].y-pp.y,d2=p[i+1].y-pp.y;
int del=(p[i]-pp)*(p[i+1]-pp);
if ( (del>=0 && d1>=0 && d2<0) ||
(del<=0 && d1<0 && d2>=0) ) cnt++;
}
return cnt;
}
}pol[N];
void solve()
{
sort(toy+1,toy+1+m);
memset(cnt,0,sizeof(cnt));
for (int i=0;i<=n;i++)
{
pol[i].p[1]=pol[i].p[5]=point(up[i],y1);
pol[i].p[4]=point(down[i],y2);
pol[i].p[3]=point(down[i+1],y2);
pol[i].p[2]=point(up[i+1],y1);
}
for (int i=1;i<=m;i++)
{
int l=0,r=n,mid;
while (l<=r)
{
mid=l+r>>1;
int res=pol[mid].inner(toy[i]);
if (res&1)
{
cnt[mid]++;
break;
}
if (res>0) l=mid+1;
else r=mid;
}
}
}
int main()
{
while (scanf("%d",&n),n)
{
if (++t!=1) puts("");
scanf("%d%d%d%d%d",&m,&x1,&y1,&x2,&y2);
for (int i=1;i<=n;i++)
scanf("%d%d",&up[i],&down[i]);
for (int i=1,x,y;i<=m;i++)
{
scanf("%d%d",&x,&y);
toy[i]=point(x,y);
}
up[0]=down[0]=x1,up[n+1]=down[n+1]=x2;
solve();
for (int i=0;i<=n;i++)
printf("%d: %d\n",i,cnt[i]);
}
return 0;
}
POJ 2318 TOYS | 二分+判断点在多边形内的更多相关文章
- poj 2318 TOYS (二分+叉积)
http://poj.org/problem?id=2318 TOYS Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 101 ...
- POJ 2318 TOYS(叉积+二分)
题目传送门:POJ 2318 TOYS Description Calculate the number of toys that land in each bin of a partitioned ...
- poj 2318 叉积+二分
TOYS Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13262 Accepted: 6412 Description ...
- zoj 1081 判断点在多边形内
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=81Points Within Time Limit: 2 Second ...
- 判断点在多边形内算法的C++实现
目录 1. 算法思路 2. 具体实现 3. 改进空间 1. 算法思路 判断平面内点是否在多边形内有多种算法,其中射线法是其中比较好理解的一种,而且能够支持凹多边形的情况.该算法的思路很简单,就是从目标 ...
- hdu 1756:Cupid's Arrow(计算几何,判断点在多边形内)
Cupid's Arrow Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- php之判断点在多边形内的api
1.判断点在多边形内的数学思想:以那个点为顶点,作任意单向射线,如果它与多边形交点个数为奇数个,那么那个点在多边形内,相关公式: <?php class AreaApi{ //$area是一个多 ...
- ZOJ 1081 Points Within | 判断点在多边形内
题目: 给个n个点的多边形,n个点按顺序给出,给个点m,判断m在不在多边形内部 题解: 网上有两种方法,这里写一种:射线法 大体的思想是:以这个点为端点,做一条平行与x轴的射线(代码中射线指向x轴正方 ...
- R树判断点在多边形内-Java版本
1.什么是RTree 待补充 2.RTree java依赖 rtree的java开源版本在GitHub上:https://github.com/davidmoten/rtree 上面有详细的使用说明 ...
随机推荐
- 第34-3题:LeetCode437. Path Sum III
题目 二叉树不超过1000个节点,且节点数值范围是 [-1000000,1000000] 的整数. 示例: root = [10,5,-3,3,2,null,11,3,-2,null,1], sum ...
- Python线程创建与使用
Python多为线程编程提供了两个简单明了的模块:thread和threading,Python3中已经不存thread模块,已经被改名为_thread,实际优先使用 threading模块. 1.P ...
- 【ACM之行】◇第一站◇ 2018HDU多校赛总结
◇第一站◇ 2018HDU多校赛 十场多校赛下来,也算是给一个初中生开了眼界……看着清华一次次AK(默默立下flag),看着自己被同校的高中生完虐,一个蒟蒻只能给dalao们垫脚
- CodeForces_864_bus
C. Bus time limit per test 2 seconds memory limit per test 256 megabytes input standard input output ...
- 没有CTO的Netflix有哪些值得我们学习的工程文化?
作者介绍: 杨波,拍拍贷基础框架研发总监.具有超过 10 年的互联网分布式系统研发和架构经验,曾先后就职于:eBay 中国研发中心(eBay CDC),任资深研发工程师,参与亿贝开放 API 平台研发 ...
- gdb-pada调试实例
先编写个简单的hello的程序 hello.c (ps:有没有头文件行不行,试试不就知道了) int main(){ printf("hello!\n"); int m,n; in ...
- Docker自学纪实(六)搭建docker私有仓库
docker的镜像仓库分两种:一种是从官方公有仓库拉取:还有就是自己搭建私有仓库.官方的镜像仓库是面对整个应用市场的:私有仓库一般用于公司内部,就是公司项目自身所需的镜像.搭建私有仓库有什么好处?私有 ...
- java.lang.UnsupportedOperationException 原因以及解决方案
如下代码: Map[] cardProds = JsonUtils.getObject(oldCartValue, new TypeReference<Map[]>(){}); List& ...
- SSO 单点登录总结(PHP)
本篇文章根据个人理解的知识整理汇总,如有不足之处,请大家多多指正. 单点登录(SSO--Single Sign On)的应用是很普遍的,尤其在大型网站系统中,比如百度,登录百度账号和,再转到百度经验. ...
- 英文缩写SFR
英文缩写为SFR,是Special Function Register(特殊功能寄存器)的缩写.