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. 《JavaScript》页面跳转

    window.location.href: <i onclick="window.location.href = '/Form/Form_Write/Index?viewname=Fo ...

  2. 阅读笔记《我是一只IT小小鸟》

    我是一只IT小小鸟 我们在尝试新的事物的时候,总是会遇到各种各样的困难,不同的人会在碰壁不同的次数之后退出.用程序员喜欢的话来说就是,我们都在for循环,区别在于你是什么情况下break;的.有的人退 ...

  3. IIS 7.0 的 ASP.NET 应用程序生命周期概述

    文章:IIS 7.0 的 ASP.NET 应用程序生命周期概述 地址:https://msdn.microsoft.com/zh-cn/library/bb470252(v=vs.100).aspx ...

  4. lintcode-496-玩具工厂

    496-玩具工厂 工厂模式是一种常见的设计模式.请实现一个玩具工厂 ToyFactory 用来产生不同的玩具类.可以假设只有猫和狗两种玩具. 您在真实的面试中是否遇到过这个题? Yes 样例 ToyF ...

  5. lintcode-65-两个排序数组的中位数

    65-两个排序数组的中位数 两个排序的数组A和B分别含有m和n个数,找到两个排序数组的中位数,要求时间复杂度应为O(log (m+n)). 样例 给出数组A = [1,2,3,4,5,6] B = [ ...

  6. zuoyeQAQ

    public class StringAPIDemo { /** * @param args */ public static void main(String[] args) { // TODO A ...

  7. FreeMarker(XML模板)导出word

    在项目中使用它完成的功能是按照固定的模板将数据导出到Word.比如台账.在完成后将处理过程按照台账的要求导出,有时程序中需要实现生成标准Word文档,要求能够打印,并且保持页面样式不变. 这个功能就是 ...

  8. vue-cli3使用cdn方式引入moment.js

    1. index.html引入: <script src="https://cdn.bootcss.com/moment.js/2.20.1/moment.min.js"&g ...

  9. vue-cli配置axios,并基于axios进行后台请求函数封装

    文章https://www.cnblogs.com/XHappyness/p/7677153.html已经对axios配置进行了说明,后台请求时可直接this.$axios直接进行.这里的缺点是后端请 ...

  10. Matlab里面.M文件不能运行,预期的图像也显示不出来的一个原因

    matlab中function函数的函数名与保存的文件名需要一样: 函数名是GAconstrain,文件名保存成GAconstrain.m,不要使用复制时候产生副本GAconstrain(1).m.