题目传送门

题意:POJ 2318 有一个长方形,用线段划分若干区域,给若干个点,问每个区域点的分布情况

分析:点和线段的位置判断可以用叉积判断。给的线段是排好序的,但是点是无序的,所以可以用二分优化。用到了叉积

  1. /************************************************
  2. * Author :Running_Time
  3. * Created Time :2015/10/23 星期五 11:38:18
  4. * File Name :POJ_2318.cpp
  5. ************************************************/
  6.  
  7. #include <cstdio>
  8. #include <algorithm>
  9. #include <iostream>
  10. #include <sstream>
  11. #include <cstring>
  12. #include <cmath>
  13. #include <string>
  14. #include <vector>
  15. #include <queue>
  16. #include <deque>
  17. #include <stack>
  18. #include <list>
  19. #include <map>
  20. #include <set>
  21. #include <bitset>
  22. #include <cstdlib>
  23. #include <ctime>
  24. using namespace std;
  25.  
  26. #define lson l, mid, rt << 1
  27. #define rson mid + 1, r, rt << 1 | 1
  28. typedef long long ll;
  29. const int N = 5e3 + 10;
  30. const int INF = 0x3f3f3f3f;
  31. const int MOD = 1e9 + 7;
  32. const double EPS = 1e-10;
  33. struct Point { //点的定义
  34. double x, y;
  35. Point (double x=0, double y=0) : x (x), y (y) {}
  36. };
  37. typedef Point Vector; //向量的定义
  38. Point read_point(void) { //点的读入
  39. double x, y;
  40. scanf ("%lf%lf", &x, &y);
  41. return Point (x, y);
  42. }
  43. double polar_angle(Vector A) { //向量极角
  44. return atan2 (A.y, A.x);
  45. }
  46. double dot(Vector A, Vector B) { //向量点积
  47. return A.x * B.x + A.y * B.y;
  48. }
  49. double cross(Vector A, Vector B) { //向量叉积
  50. return A.x * B.y - A.y * B.x;
  51. }
  52. int dcmp(double x) { //三态函数,减少精度问题
  53. if (fabs (x) < EPS) return 0;
  54. else return x < 0 ? -1 : 1;
  55. }
  56. Vector operator + (Vector A, Vector B) { //向量加法
  57. return Vector (A.x + B.x, A.y + B.y);
  58. }
  59. Vector operator - (Vector A, Vector B) { //向量减法
  60. return Vector (A.x - B.x, A.y - B.y);
  61. }
  62. Vector operator * (Vector A, double p) { //向量乘以标量
  63. return Vector (A.x * p, A.y * p);
  64. }
  65. Vector operator / (Vector A, double p) { //向量除以标量
  66. return Vector (A.x / p, A.y / p);
  67. }
  68. bool operator < (const Point &a, const Point &b) { //点的坐标排序
  69. return a.x < b.x || (a.x == b.x && a.y < b.y);
  70. }
  71. bool operator == (const Point &a, const Point &b) { //判断同一个点
  72. return dcmp (a.x - b.x) == 0 && dcmp (a.y - b.y) == 0;
  73. }
  74. double length(Vector A) { //向量长度,点积
  75. return sqrt (dot (A, A));
  76. }
  77. double angle(Vector A, Vector B) { //向量转角,逆时针,点积
  78. return acos (dot (A, B) / length (A) / length (B));
  79. }
  80. double area_triangle(Point a, Point b, Point c) { //三角形面积,叉积
  81. return fabs (cross (b - a, c - a)) / 2.0;
  82. }
  83. Vector rotate(Vector A, double rad) { //向量旋转,逆时针
  84. return Vector (A.x * cos (rad) - A.y * sin (rad), A.x * sin (rad) + A.y * cos (rad));
  85. }
  86. Vector nomal(Vector A) { //向量的单位法向量
  87. double len = length (A);
  88. return Vector (-A.y / len, A.x / len);
  89. }
  90. Point point_inter(Point p, Vector V, Point q, Vector W) { //两直线交点,参数方程
  91. Vector U = p - q;
  92. double t = cross (W, U) / cross (V, W);
  93. return p + V * t;
  94. }
  95. double dis_to_line(Point p, Point a, Point b) { //点到直线的距离,两点式
  96. Vector V1 = b - a, V2 = p - a;
  97. return fabs (cross (V1, V2)) / length (V1);
  98. }
  99. double dis_to_seg(Point p, Point a, Point b) { //点到线段的距离,两点式
  100.  
  101. if (a == b) return length (p - a);
  102. Vector V1 = b - a, V2 = p - a, V3 = p - b;
  103. if (dcmp (dot (V1, V2)) < 0) return length (V2);
  104. else if (dcmp (dot (V1, V3)) > 0) return length (V3);
  105. else return fabs (cross (V1, V2)) / length (V1);
  106. }
  107. Point point_proj(Point p, Point a, Point b) { //点在直线上的投影,两点式
  108. Vector V = b - a;
  109. return a + V * (dot (V, p - a) / dot (V, V));
  110. }
  111. bool inter(Point a1, Point a2, Point b1, Point b2) { //判断线段相交,两点式
  112. double c1 = cross (a2 - a1, b1 - a1), c2 = cross (a2 - a1, b2 - a1),
  113. c3 = cross (b2 - b1, a1 - b1), c4 = cross (b2 - b1, a2 - b1);
  114. return dcmp (c1) * dcmp (c2) < 0 && dcmp (c3) * dcmp (c4) < 0;
  115. }
  116. bool on_seg(Point p, Point a1, Point a2) { //判断点在线段上,两点式
  117. return dcmp (cross (a1 - p, a2 - p)) == 0 && dcmp (dot (a1 - p, a2 - p)) < 0;
  118. }
  119. double area_poly(Point *p, int n) { //多边形面积
  120. double ret = 0;
  121. for (int i=1; i<n-1; ++i) {
  122. ret += fabs (cross (p[i] - p[0], p[i+1] - p[0]));
  123. }
  124. return ret / 2;
  125. }
  126. struct Line {
  127. Point s, e;
  128. Line () {}
  129. Line (Point s, Point e) : s (s), e (e) {}
  130. };
  131. Line board[N];
  132. Point toy[N];
  133. Point b, e;
  134. int ans[N];
  135.  
  136. int main(void) {
  137. int n, m;
  138. bool fir = true;
  139. while(scanf ("%d", &n) == 1) {
  140. if (!n) break;
  141. if (fir) fir = false;
  142. else puts ("");
  143. scanf ("%d", &m);
  144. b = read_point ();
  145. e = read_point ();
  146. double U, L;
  147. for (int i=0; i<n; ++i) {
  148. scanf ("%lf%lf", &U, &L);
  149. board[i] = Line (Point (U, b.y), Point (L, e.y));
  150. }
  151. board[n] = Line (Point (e.x, b.y), Point (e.x, e.y));
  152. for (int i=0; i<m; ++i) {
  153. toy[i] = read_point ();
  154. }
  155. memset (ans, 0, sizeof (ans));
  156. for (int i=0; i<m; ++i) {
  157. if (toy[i].x < b.x || toy[i].x > e.x || toy[i].y > b.y || toy[i].y < e.y) continue;
  158. int l = 0, r = n, mid, tmp = 0;
  159. while (l <= r) {
  160. mid = (l + r) >> 1;
  161. if (cross (board[mid].s - toy[i], board[mid].e - toy[i]) < 0) {
  162. tmp = mid;
  163. r = mid - 1;
  164. }
  165. else l = mid + 1;
  166. }
  167. ans[tmp]++;
  168. }
  169. for (int i=0; i<=n; ++i) {
  170. printf ("%d: %d\n", i, ans[i]);
  171. }
  172. }
  173.  
  174. return 0;
  175. }

题目传送门

题意:POJ 2398 和上面那题没什么区别,回答的问题不同,线段无序先排序。

  1. /************************************************
  2. * Author :Running_Time
  3. * Created Time :2015/10/23 星期五 11:38:18
  4. * File Name :POJ_2398.cpp
  5. ************************************************/
  6.  
  7. #include <cstdio>
  8. #include <algorithm>
  9. #include <iostream>
  10. #include <sstream>
  11. #include <cstring>
  12. #include <cmath>
  13. #include <string>
  14. #include <vector>
  15. #include <queue>
  16. #include <deque>
  17. #include <stack>
  18. #include <list>
  19. #include <map>
  20. #include <set>
  21. #include <bitset>
  22. #include <cstdlib>
  23. #include <ctime>
  24. using namespace std;
  25.  
  26. #define lson l, mid, rt << 1
  27. #define rson mid + 1, r, rt << 1 | 1
  28. typedef long long ll;
  29. const int N = 1e3 + 10;
  30. const int INF = 0x3f3f3f3f;
  31. const int MOD = 1e9 + 7;
  32. const double EPS = 1e-10;
  33. struct Point { //点的定义
  34. double x, y;
  35. Point (double x=0, double y=0) : x (x), y (y) {}
  36. };
  37. typedef Point Vector; //向量的定义
  38. Point read_point(void) { //点的读入
  39. double x, y;
  40. scanf ("%lf%lf", &x, &y);
  41. return Point (x, y);
  42. }
  43. double polar_angle(Vector A) { //向量极角
  44. return atan2 (A.y, A.x);
  45. }
  46. double dot(Vector A, Vector B) { //向量点积
  47. return A.x * B.x + A.y * B.y;
  48. }
  49. double cross(Vector A, Vector B) { //向量叉积
  50. return A.x * B.y - A.y * B.x;
  51. }
  52. int dcmp(double x) { //三态函数,减少精度问题
  53. if (fabs (x) < EPS) return 0;
  54. else return x < 0 ? -1 : 1;
  55. }
  56. Vector operator + (Vector A, Vector B) { //向量加法
  57. return Vector (A.x + B.x, A.y + B.y);
  58. }
  59. Vector operator - (Vector A, Vector B) { //向量减法
  60. return Vector (A.x - B.x, A.y - B.y);
  61. }
  62. Vector operator * (Vector A, double p) { //向量乘以标量
  63. return Vector (A.x * p, A.y * p);
  64. }
  65. Vector operator / (Vector A, double p) { //向量除以标量
  66. return Vector (A.x / p, A.y / p);
  67. }
  68. bool operator < (const Point &a, const Point &b) { //点的坐标排序
  69. return a.x < b.x || (a.x == b.x && a.y < b.y);
  70. }
  71. bool operator == (const Point &a, const Point &b) { //判断同一个点
  72. return dcmp (a.x - b.x) == 0 && dcmp (a.y - b.y) == 0;
  73. }
  74. double length(Vector A) { //向量长度,点积
  75. return sqrt (dot (A, A));
  76. }
  77. double angle(Vector A, Vector B) { //向量转角,逆时针,点积
  78. return acos (dot (A, B) / length (A) / length (B));
  79. }
  80. double area_triangle(Point a, Point b, Point c) { //三角形面积,叉积
  81. return fabs (cross (b - a, c - a)) / 2.0;
  82. }
  83. Vector rotate(Vector A, double rad) { //向量旋转,逆时针
  84. return Vector (A.x * cos (rad) - A.y * sin (rad), A.x * sin (rad) + A.y * cos (rad));
  85. }
  86. Vector nomal(Vector A) { //向量的单位法向量
  87. double len = length (A);
  88. return Vector (-A.y / len, A.x / len);
  89. }
  90. Point point_inter(Point p, Vector V, Point q, Vector W) { //两直线交点,参数方程
  91. Vector U = p - q;
  92. double t = cross (W, U) / cross (V, W);
  93. return p + V * t;
  94. }
  95. double dis_to_line(Point p, Point a, Point b) { //点到直线的距离,两点式
  96. Vector V1 = b - a, V2 = p - a;
  97. return fabs (cross (V1, V2)) / length (V1);
  98. }
  99. double dis_to_seg(Point p, Point a, Point b) { //点到线段的距离,两点式
  100.  
  101. if (a == b) return length (p - a);
  102. Vector V1 = b - a, V2 = p - a, V3 = p - b;
  103. if (dcmp (dot (V1, V2)) < 0) return length (V2);
  104. else if (dcmp (dot (V1, V3)) > 0) return length (V3);
  105. else return fabs (cross (V1, V2)) / length (V1);
  106. }
  107. Point point_proj(Point p, Point a, Point b) { //点在直线上的投影,两点式
  108. Vector V = b - a;
  109. return a + V * (dot (V, p - a) / dot (V, V));
  110. }
  111. bool inter(Point a1, Point a2, Point b1, Point b2) { //判断线段相交,两点式
  112. double c1 = cross (a2 - a1, b1 - a1), c2 = cross (a2 - a1, b2 - a1),
  113. c3 = cross (b2 - b1, a1 - b1), c4 = cross (b2 - b1, a2 - b1);
  114. return dcmp (c1) * dcmp (c2) < 0 && dcmp (c3) * dcmp (c4) < 0;
  115. }
  116. bool on_seg(Point p, Point a1, Point a2) { //判断点在线段上,两点式
  117. return dcmp (cross (a1 - p, a2 - p)) == 0 && dcmp (dot (a1 - p, a2 - p)) < 0;
  118. }
  119. double area_poly(Point *p, int n) { //多边形面积
  120. double ret = 0;
  121. for (int i=1; i<n-1; ++i) {
  122. ret += fabs (cross (p[i] - p[0], p[i+1] - p[0]));
  123. }
  124. return ret / 2;
  125. }
  126. struct Line {
  127. Point s, e;
  128. Line () {}
  129. Line (Point s, Point e) : s (s), e (e) {}
  130. };
  131. bool cmp(Line A, Line B) {
  132. return A.s.x < B.s.x || (A.s.x == B.s.x && A.e.x < B.e.x);
  133. }
  134. Line board[N];
  135. Point toy[N];
  136. Point b, e;
  137. int ans[N];
  138.  
  139. int main(void) {
  140. int n, m;
  141. while(scanf ("%d", &n) == 1) {
  142. if (!n) break;
  143. scanf ("%d", &m);
  144. b = read_point ();
  145. e = read_point ();
  146. double U, L;
  147. for (int i=0; i<n; ++i) {
  148. scanf ("%lf%lf", &U, &L);
  149. board[i] = Line (Point (U, b.y), Point (L, e.y));
  150. }
  151. board[n] = Line (Point (e.x, b.y), Point (e.x, e.y));
  152. sort (board, board+1+n, cmp);
  153. for (int i=0; i<m; ++i) {
  154. toy[i] = read_point ();
  155. }
  156. memset (ans, 0, sizeof (ans));
  157. for (int i=0; i<m; ++i) {
  158. if (toy[i].x < b.x || toy[i].x > e.x || toy[i].y > b.y || toy[i].y < e.y) continue;
  159. int l = 0, r = n, mid, tmp = 0;
  160. while (l <= r) {
  161. mid = (l + r) >> 1;
  162. if (cross (board[mid].s - toy[i], board[mid].e - toy[i]) < 0) {
  163. tmp = mid;
  164. r = mid - 1;
  165. }
  166. else l = mid + 1;
  167. }
  168. ans[tmp]++;
  169. }
  170. puts ("Box");
  171. for (int i=1; i<=m; ++i) {
  172. int cnt = 0;
  173. for (int j=0; j<=n; ++j) {
  174. if (ans[j] == i) ++cnt;
  175. }
  176. if (cnt) {
  177. printf ("%d: %d\n", i, cnt);
  178. }
  179. }
  180. }
  181.  
  182. return 0;
  183. }

  

简单几何(点与线段的位置) POJ 2318 TOYS && POJ 2398 Toy Storage的更多相关文章

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

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

  2. POJ 2318 TOYS && POJ 2398 Toy Storage(几何)

    2318 TOYS 2398 Toy Storage 题意 : 给你n块板的坐标,m个玩具的具体坐标,2318中板是有序的,而2398无序需要自己排序,2318要求输出的是每个区间内的玩具数,而231 ...

  3. POJ 2318 TOYS/POJ 2398 Toy Storage

    计算几何终于开坑了... 叉积+二分. #include<iostream> #include<cstdio> #include<cstring> #include ...

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

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

  5. poj 2398 Toy Storage(计算几何)

    题目传送门:poj 2398 Toy Storage 题目大意:一个长方形的箱子,里面有一些隔板,每一个隔板都可以纵切这个箱子.隔板将这个箱子分成了一些隔间.向其中扔一些玩具,每个玩具有一个坐标,求有 ...

  6. 几何+点与线段的位置关系+二分(POJ2318)

    TOYS Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10666   Accepted: 5128 Description ...

  7. POJ 2398 - Toy Storage 点与直线位置关系

    Toy Storage Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5439   Accepted: 3234 Descr ...

  8. 2018.07.04 POJ 2398 Toy Storage(二分+简单计算几何)

    Toy Storage Time Limit: 1000MS Memory Limit: 65536K Description Mom and dad have a problem: their ch ...

  9. POJ 2318 TOYS (叉乘判断)

    <题目链接> 题目大意: 给出矩形4个点和n个挡板俩顶点的位置,这n个挡板将该矩形分成 n+1块区域,再给你m个点的坐标,然你输出每个区域内有几个点. 解题思路: 用叉乘即可简单判断点与直 ...

随机推荐

  1. cocos基础教程(3)cocos3.x版本目录结构介绍

    简介 cocos2d-x-3.x版本进行了很多优化,比如:将TTF字体用Atlas缓存,节点重排序官方声称提升了10倍速度,查找.移除节点方面也提高了10%,拆分渲染层到独立的线程运行: 另外,coc ...

  2. 什么是A记录?MX记录?CNAME记录?它们都有些什么用途?

    什么是A记录?什么是MX记录?CNAME记录又是什么?它们都有些什么用途? 好,下面就用我浅陋经验给大家介绍一下: 1. A记录:WEB服务器的IP指向 A (Address) 记录是用来指定主机名( ...

  3. hibernate中几个接口作用

    1.Configuration 类 Configuration 类负责管理 Hibernate 的配置信息,包括数据库的URL.用户名.密码.JDBC驱动类,数据库Dialect,数据库连接池等,其加 ...

  4. 【OpenStack】OpenStack系列11之namaspace&openvswitch原理实践

    Namespace实现网络隔离与互通 新建ns: ip netns add foo 查看ns: ip netns 查看ns详细配置: ip netns exec foo ip addr 设置ns内部l ...

  5. PYTHON实现HTTP摘要认证(DIGEST AUTHENTICATION)

    参考: http://blog.csdn.net/kiwi_coder/article/details/28677651 http://blog.csdn.net/gl1987807/article/ ...

  6. windows2003批量添加和导出所有ip

    批量添加IP 在cmd命令行下运行: FOR /L %i IN (130,1,190) DO netsh interface ip add address "本地连接" 192.1 ...

  7. Java for LeetCode 073 Set Matrix Zeroes

    Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. 解题思路: ...

  8. BestCoder13 1001.Beautiful Palindrome Number(hdu 5062) 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5062 题目意思:给出 N,找出 1 - 10^N 中满足 Beautiful Palindrome N ...

  9. codeforces B. Levko and Permutation 解题报告

    题目链接:http://codeforces.com/problemset/problem/361/B 题目意思:有n个数,这些数的范围是[1,n],并且每个数都是不相同的.你需要构造一个排列,使得这 ...

  10. HDU 5795 A Simple Nim (博弈) ---2016杭电多校联合第六场

    A Simple Nim Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tota ...