2318

2398

题意:给出n条线将一块区域分成n+1块空间,再给出m个点,询问这些点在哪个空间里。

思路:由于只要求相对位置关系,而对具体位置不关心,那么易使用叉积性质得到相对位置关系(左侧/右侧),再因为是简单几何线段不相较,即有序分布,那么在求在哪个区间时可以先对所有线段根据x坐标排序,使用二分减少复杂度。

/** @Date    : 2017-07-11 11:05:59
* @FileName: POJ 2318 叉积性质.cpp
* @Platform: Windows
* @Author : Lweleth (SoungEarlf@gmail.com)
* @Link : https://github.com/
* @Version : $Id$
*/
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <algorithm>
#include <utility>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <stack>
#include <queue>
//#include <bits/stdc++.h>
#define LL long long
#define PII pair
#define MP(x, y) make_pair((x),(y))
#define fi first
#define se second
#define PB(x) push_back((x))
#define MMG(x) memset((x), -1,sizeof(x))
#define MMF(x) memset((x),0,sizeof(x))
#define MMI(x) memset((x), INF, sizeof(x))
using namespace std; const int INF = 0x3f3f3f3f;
const int N = 1e5+20;
const double eps = 1e-8; struct Point
{
int x, y;
Point(){}
Point(int xx, int yy){x = xx, y = yy;}
Point operator -(const Point &b) const
{
return Point(x - b.x, y - b.y);
}
int operator *(const Point &b) const
{
return x * b.x + y * b.y;
}
}; int cross(Point a, Point b)
{
return a.x * b.y - a.y * b.x;
} struct Line
{
Point s, t;
Line(){}
Line(Point ss, Point tt){s = ss, t = tt;}
}; int JudegeCross(Point p0, Point p1, Point p2)
{
return cross(p1 - p0, p2 - p0);
} Line li[N];
int ans[N];
int vis[N];
int cmp(Line a, Line b)
{
return a.s.x < b.s.x;
} int main()
{
int n, m, x1, x2, y1, y2;
while(~scanf("%d", &n) && n)
{
MMF(ans);
MMF(vis);
scanf("%d%d%d%d%d", &m, &x1, &y1, &x2, &y2);
for(int i = 0; i < n; i++)
{
int s, t;
scanf("%d%d", &s, &t);
li[i] = Line(Point(s, y1), Point(t, y2));
}
li[n] = Line(Point(x2, y1), Point(x2, y2));
sort(li, li + n + 1, cmp);
while(m--)
{
int x, y;
scanf("%d%d", &x, &y);
Point p = Point(x, y);
int l = 0, r = n;
int pos = 0;
while(l <= r)
{
int mid = (l + r) >> 1;
if(JudegeCross(p, li[mid].s, li[mid].t) < 0)
{
pos = mid;
r = mid - 1;
}
else
l = mid + 1;
}
ans[pos]++;
}
printf("Box\n");
for(int i = 0; i <= n; i++)
if(ans[i])
vis[ans[i]]++;
for(int i = 1; i <= n; i++)
if(vis[i])
printf("%d: %d\n", i, vis[i]);
}
return 0;
}
/** @Date    : 2017-07-11 11:05:59
* @FileName: POJ 2318 叉积性质.cpp
* @Platform: Windows
* @Author : Lweleth (SoungEarlf@gmail.com)
* @Link : https://github.com/
* @Version : $Id$
*/
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <algorithm>
#include <utility>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <stack>
#include <queue>
//#include <bits/stdc++.h>
#define LL long long
#define PII pair
#define MP(x, y) make_pair((x),(y))
#define fi first
#define se second
#define PB(x) push_back((x))
#define MMG(x) memset((x), -1,sizeof(x))
#define MMF(x) memset((x),0,sizeof(x))
#define MMI(x) memset((x), INF, sizeof(x))
using namespace std; const int INF = 0x3f3f3f3f;
const int N = 1e5+20;
const double eps = 1e-8; struct Point
{
int x, y;
Point(){}
Point(int xx, int yy){x = xx, y = yy;}
Point operator -(const Point &b) const
{
return Point(x - b.x, y - b.y);
}
int operator *(const Point &b) const
{
return x * b.x + y * b.y;
}
}; int cross(Point a, Point b)
{
return a.x * b.y - a.y * b.x;
} struct Line
{
Point s, t;
Line(){}
Line(Point ss, Point tt){s = ss, t = tt;}
}; int JudegeCross(Point p0, Point p1, Point p2)
{
return cross(p1 - p0, p2 - p0);
} Line li[N];
int ans[N]; int main()
{
int n, m, x1, x2, y1, y2;
while(~scanf("%d", &n) && n)
{
MMF(ans);
scanf("%d%d%d%d%d", &m, &x1, &y1, &x2, &y2);
for(int i = 0; i < n; i++)
{
int s, t;
scanf("%d%d", &s, &t);
li[i] = Line(Point(s, y1), Point(t, y2));
}
li[n] = Line(Point(x2, y1), Point(x2, y2));
while(m--)
{
int x, y;
scanf("%d%d", &x, &y);
Point p = Point(x, y);
int l = 0, r = n;
int pos = 0;
while(l <= r)
{
int mid = (l + r) >> 1;
if(JudegeCross(p, li[mid].s, li[mid].t) < 0)
{
pos = mid;
r = mid - 1;
}
else
l = mid + 1;
}
ans[pos]++;
}
for(int i = 0; i <= n; i++)
{
printf("%d: %d\n", i, ans[i]);
}
printf("\n");
}
return 0;
}

POJ 2318/2398 叉积性质的更多相关文章

  1. 二分+叉积判断方向 poj 2318 2398

    // 题意:问你每个区域有多少个点 // 思路:数据小可以直接暴力 // 也可以二分区间 #include <cstdio> #include <cstring> #inclu ...

  2. POJ 2318 TOYS 叉积

    题目大意:给出一个长方形盒子的左上点,右下点坐标.给出n个隔板的坐标,和m个玩具的坐标,求每个区间内有多少个玩具. 题目思路:利用叉积判断玩具在隔板的左方或右方,并用二分优化查找过程. #includ ...

  3. POJ 2318 TOYS (叉积+二分)

    题目: Description Calculate the number of toys that land in each bin of a partitioned toy box. Mom and ...

  4. 向量的叉积 POJ 2318 TOYS & POJ 2398 Toy Storage

    POJ 2318: 题目大意:给定一个盒子的左上角和右下角坐标,然后给n条线,可以将盒子分成n+1个部分,再给m个点,问每个区域内有多少各点 这个题用到关键的一步就是向量的叉积,假设一个点m在 由ab ...

  5. poj 2318 TOYS &amp; poj 2398 Toy Storage (叉积)

    链接:poj 2318 题意:有一个矩形盒子,盒子里有一些木块线段.而且这些线段坐标是依照顺序给出的. 有n条线段,把盒子分层了n+1个区域,然后有m个玩具.这m个玩具的坐标是已知的,问最后每一个区域 ...

  6. 简单几何(点与线段的位置) POJ 2318 TOYS && POJ 2398 Toy Storage

    题目传送门 题意:POJ 2318 有一个长方形,用线段划分若干区域,给若干个点,问每个区域点的分布情况 分析:点和线段的位置判断可以用叉积判断.给的线段是排好序的,但是点是无序的,所以可以用二分优化 ...

  7. poj 2318 叉积+二分

    TOYS Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 13262   Accepted: 6412 Description ...

  8. POJ 2318 TOYS(叉积+二分)

    题目传送门:POJ 2318 TOYS Description Calculate the number of toys that land in each bin of a partitioned ...

  9. poj 2398(叉积判断点在线段的哪一侧)

    Toy Storage Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5016   Accepted: 2978 Descr ...

随机推荐

  1. 把字符串"3,1,2,4"以","分割拆分为数组,数组元素并按从小到大的顺序排列

    package com.wangcf; /** * 把字符串"3,1,2,4"以","分割拆分为数组,数组元素并按从小到大的顺序排列 * @author fan ...

  2. Right-BICEP 测试四则运算程序

    测试方法:      Right-BICEP 测试计划: 1.边界测试是否正确 2.负数表示是否实现 3.是否有乘除法 4.是否可以选择题目数量 5.是否有输出方式 6.是否有括号 7.是否有重复查询 ...

  3. WPF和Expression Blend开发实例:充分利用Blend实现一个探照灯的效果

    本篇文章阅读的基础是在读者对于WPF有一定的了解并且有WPF相关的编码经验,对于Blend的界面布局有基础的知识.文章中对于相应的在Blend中的操作进行演示,并不会进行细致到每个属性的介绍.同时,本 ...

  4. IOC与DI(xml 配置)

    Spring可以帮助我们管理软件开发过程中的对象,以及如何创建和维护对象之间的关系. Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架,可以将组建的耦合度降至最低,即实现解耦 ...

  5. jdbc 2.0

    1.Statement接口不能接受参数 2.PreparedStatement接口在运行时接受输入参数 3.CallableStatement接口也可以接受运行时输入参数,当想要访问数据库存储过程时使 ...

  6. QMdiArea及QMdiSubWindow实现父子窗口及布局方法

    版权声明:若无来源注明,Techie亮博客文章均为原创. 转载请以链接形式标明本文标题和地址: 本文标题:QMdiArea及QMdiSubWindow实现父子窗口及布局方法     本文地址:http ...

  7. [C/C++] 虚函数机制

    转自:c++ 虚函数的实现机制:笔记 1.c++实现多态的方法 其实很多人都知道,虚函数在c++中的实现机制就是用虚表和虚指针,但是具体是怎样的呢?从more effecive c++其中一篇文章里面 ...

  8. 下载文件 通过a 标签 请求某个servlet进行下载的

    下载文件 通过a 标签 请求某个servlet进行下载的

  9. APIO/CTSC2017游记

    5.10开坑,别问我为啥今天才开始写,前几天玩得太开心了233 5.7 坐火车坐火车,坐地铁坐地铁.其实是第一次坐地铁233.解锁了在地铁上双手玩手机不扶东西站立的姿势? 全程烧流量上QQ,拜大佬约面 ...

  10. Selenium遇到问题unknown error:cannot create default profile directory......

    1.selenium遇到问题unknown error:cannot create default profile directory...... 2.解决方案 问题1:把驱动放入C:\Windows ...