poj 2318 TOYS 点与矩形的关系
题目链接
题意
有一个矩形盒子,\(n(n\leq 5e4)\)条线段将其分成了\(n+1\)个区域(每条线段的两个端点分别在矩形的上边和下边,且线段互不相交)。现向盒子中扔\(m(m\leq 5e4)\)个玩具,问最终盒子的\(n+1\)个区域中各有多少玩具。数据保证玩具不会扔在线段上。

思路
假设玩具\(P\)在第\(i\)个区域,其左边为第\(i\)条线段\(A_1B_1\),右边为第\(i+1\)条线段\(A_2B_2\)(\(A\)在上边,\(B\)在下边),则有$$\overrightarrow{PA_1}\times\overrightarrow{PB_1}\gt 0 且 \overrightarrow{PA_2}\times\overrightarrow{PB_2}\lt 0$$
并且左边的叉积都大于\(0\),右边都小于\(0\).
因此可以二分查找\(P\)的位置。
也可以先对玩具的横坐标用\(lower\_bound\)确定下来一个小范围,再在这个小范围内二分(直接查找也可以,因为这个范围应该很小)。
Code
#include <cstdio>
#include <cstring>
#include <algorithm>
#define maxn 5010
int up[maxn], down[maxn], ans[maxn], n, m;
using namespace std;
typedef long long LL;
LL vec(LL x1, LL y1, LL x2, LL y2, LL x0, LL y0) {
return (x1-x0) * (y2-y0) - (x2-x0) * (y1-y0);
}
void work() {
memset(ans, 0, sizeof ans);
int x1, y1, x2, y2;
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]);
up[0] = down[0] = x1, up[n+1] = down[n+1] = x2;
while (m--) {
int x, y;
scanf("%d%d", &x, &y);
int p2 = lower_bound(down, down+n+1, x) - down,
p1 = lower_bound(up, up+n+1, x) - up;
if (p1 == p2) { ++ans[p1-1]; continue; }
if (p1 > p2) swap(p1, p2);
for (int i = p1; i <= p2; ++i) {
if (vec(up[i], y1, down[i], y2, x, y) < 0) { ++ans[i-1]; break; }
}
}
for (int i = 0; i <= n; ++i) printf("%d: %d\n", i, ans[i]);
printf("\n");
}
int main() {
while (scanf("%d", &n) && n) work();
return 0;
}
poj 2318 TOYS 点与矩形的关系的更多相关文章
- POJ 2318 TOYS(点与直线的关系 叉积&&二分)
题目链接 题意: 给定一个矩形,n个线段将矩形分成n+1个区间,m个点,问这些点的分布. 题解: 思路就是叉积加二分,利用叉积判断点与直线的距离,二分搜索区间. 代码: 最近整理了STL的一些模板,发 ...
- POJ 2318 TOYS(叉积+二分)
题目传送门:POJ 2318 TOYS Description Calculate the number of toys that land in each bin of a partitioned ...
- poj 2318 TOYS (二分+叉积)
http://poj.org/problem?id=2318 TOYS Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 101 ...
- 向量的叉积 POJ 2318 TOYS & POJ 2398 Toy Storage
POJ 2318: 题目大意:给定一个盒子的左上角和右下角坐标,然后给n条线,可以将盒子分成n+1个部分,再给m个点,问每个区域内有多少各点 这个题用到关键的一步就是向量的叉积,假设一个点m在 由ab ...
- poj 2318 TOYS & poj 2398 Toy Storage (叉积)
链接:poj 2318 题意:有一个矩形盒子,盒子里有一些木块线段.而且这些线段坐标是依照顺序给出的. 有n条线段,把盒子分层了n+1个区域,然后有m个玩具.这m个玩具的坐标是已知的,问最后每一个区域 ...
- 简单几何(点与线段的位置) POJ 2318 TOYS && POJ 2398 Toy Storage
题目传送门 题意:POJ 2318 有一个长方形,用线段划分若干区域,给若干个点,问每个区域点的分布情况 分析:点和线段的位置判断可以用叉积判断.给的线段是排好序的,但是点是无序的,所以可以用二分优化 ...
- POJ 2318 TOYS && POJ 2398 Toy Storage(几何)
2318 TOYS 2398 Toy Storage 题意 : 给你n块板的坐标,m个玩具的具体坐标,2318中板是有序的,而2398无序需要自己排序,2318要求输出的是每个区间内的玩具数,而231 ...
- poj 2318 TOYS(计算几何 点与线段的关系)
TOYS Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 12015 Accepted: 5792 Description ...
- POJ 2318 - TOYS - [计算几何基础题]
题目链接:http://poj.org/problem?id=2318 Time Limit: 2000MS Memory Limit: 65536K Description Calculate th ...
随机推荐
- IDEA设置每次打开重新选择项目
通过这里,选择settings,或者进入之后的FILE->settings.搜索System 即可出现
- 原型与原型继承demo
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Docker 镜像&仓库 获取及推送镜像
docker查看.删除镜像 docker镜像存储位置: /var/lib/docker 查看docker信息也可以查看保存位置 docker info 1.列出镜像 docker images -aa ...
- 【php】 phpword下载文件问题
这个问题是在 科锐国际 工作过程中发现的 word文档的名字(有汉字和空格)在windows系统上遍历是查不到文件的,但是在linux系统上市可以的压缩包里面的中文名word文档,如果出现汉字和空格, ...
- SQL前后端分页
/class Page<T> package com.neusoft.bean; import java.util.List; public class Page<T> { p ...
- LeetCode(134) Gas Station
题目 There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. ...
- LeetCode(123) Best Time to Buy and Sell Stock III
题目 Say you have an array for which the ith element is the price of a given stock on day i. Design an ...
- The 2018 ACM-ICPC Chinese Collegiate Programming Contest Maximum Element In A Stack
//利用二维数组模拟 #include <iostream> #include <cstdio> #include <cstring> #include <s ...
- Latex:插入伪代码
*本文属于转载. *转载链接:https://blog.csdn.net/lwb102063/article/details/53046265 目录 clrscode algorithm algori ...
- MVC如何在解决方案下创建文件夹
背景:为什么要在解决方案下创建文件夹? 比如,在开发过程中,会抽象出大量的公共方法,如数据库访问的方法.配置文件读取方法等等,将这些方法生成自己的DLL库文件,方便在其他的项目中进行复用.那么,这些方 ...