题目链接

题意

有一个矩形盒子,\(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;
}

最新文章

  1. psql
  2. Nopcommerce 二次开发1 基础
  3. struts学习
  4. equals()方法
  5. iscsi: 环境搭建
  6. POJ 3255 Roadblocks --次短路径
  7. Sqli-labs less 51
  8. centos系统常用软件环境搭建
  9. bzoj1858
  10. 调试Linq的时候获得相对应的SQL
  11. IT忍者神龟之Struts2.xml配置全然正确流程能走通可是有红叉解决
  12. Eclipse用法和技巧二十三:查看JDK源码
  13. transition与animation
  14. Arduino 3g shield using GSM bought from ITead
  15. poj2886线段树(单点修改,区间查询)
  16. RabbitMQ教程(一) ——win7下安装RabbitMQ
  17. 创建一个vue项目的过程
  18. Vue源码之目录结构
  19. NPOI导出Excel帮助类
  20. Manjaro Linux 配置nfs服务器

热门文章

  1. debian常用指令
  2. A. Vitya in the Countryside
  3. Golang Json测试
  4. TB平台搭建之一
  5. Java-basic-2-
  6. loadView、viewDidLoad及viewDidUnload的关系(转)
  7. poj 3616 奶牛产奶问题 dp算法
  8. AD转换器的参数介绍
  9. tarjan - SPFA - Luogu 3387【模板】缩点
  10. RHEL6.X设置163yum源