题目链接:https://cn.vjudge.net/problem/POJ-2318

题意

在一个矩形内,给出n-1条线段,把矩形分成n快四边形

问某些点在那个四边形内

思路

二分+判断点与位置关系

提交过程

WA*n x1和x2,y1和y2在复制的时候没分清(哭
WA 可能存在二分问题?
AC

代码

  1. #define PI 3.1415926
  2. #include <cmath>
  3. #include <cstdio>
  4. #include <vector>
  5. #include <algorithm>
  6. using namespace std;
  7. const double eps=1e-10;
  8. const int maxn=5e3+20;
  9. struct Point{
  10. double x, y;
  11. Point(int x=0, int y=0):x(x), y(y) {}
  12. // no known conversion for argument 1 from 'Point' to 'Point&'
  13. Point operator + (Point p){return Point(x+p.x, y+p.y);}
  14. Point operator - (Point p){return Point(x-p.x, y-p.y);}
  15. Point operator * (double k){return Point(k*x, k*y);}
  16. Point operator / (double k){return Point(x/k, y/k);}
  17. bool operator < (Point p) const{return (x==p.x)?(y<p.y):(x<p.x);} // need eps?
  18. bool operator == (const Point p) const{return fabs(x-p.x)<eps&&fabs(y-p.y)<eps;}
  19. double norm(void){return x*x+y*y;}
  20. double abs(void){return sqrt(norm());}
  21. double dot(Point p){return x*p.x+y*p.y;} // cos
  22. double cross(Point p){return x*p.y-y*p.x;} // sin
  23. };
  24. struct Segment{Point p1, p2;};
  25. struct Circle{Point o; double rad;};
  26. typedef Point Vector;
  27. typedef vector<Point> Polygon;
  28. typedef Segment Line;
  29. int ccw(Point p0, Point p1, Point p2){
  30. Vector v1=p1-p0, v2=p2-p0;
  31. if (v1.cross(v2)>eps) return 1; // anti-clockwise
  32. if (v1.cross(v2)<-eps) return -1; // clockwise
  33. if (v1.dot(v2)<0) return 2;
  34. if (v1.norm()<v2.norm()) return -2;
  35. return 0;
  36. }
  37. Point project(Segment s, Point p){
  38. Vector base=s.p2-s.p1;
  39. double k=(p-s.p1).cross(base)/base.norm();
  40. return s.p1+base*k;
  41. }
  42. Point reflect(Segment s, Point &p){
  43. return p+(project(s, p)-p)*2;
  44. }
  45. double lineDist(Line l, Point p){
  46. return abs((l.p2-l.p1).cross(p-l.p1)/(l.p2-l.p1).abs());
  47. }
  48. double SegDist(Segment s, Point p){
  49. if ((s.p2-s.p1).dot(p-s.p1)<0) return Point(p-s.p1).abs();
  50. if ((s.p1-s.p2).dot(p-s.p2)<0) return Point(p-s.p2).abs();
  51. return abs((s.p2-s.p1).cross(p-s.p1)/(s.p2-s.p1).abs());
  52. }
  53. bool intersect(Point p1, Point p2, Point p3, Point p4){
  54. return ccw(p1, p2, p3)*ccw(p1, p2, p4)<=0 &&
  55. ccw(p3, p4, p1)*ccw(p3, p4, p2)<=0;
  56. }
  57. Point getCrossPoint(Segment s1, Segment s2){
  58. Vector base=s2.p2-s2.p1;
  59. double d1=abs(base.cross(s1.p1-s2.p1));
  60. double d2=abs(base.cross(s1.p2-s2.p1));
  61. double t=d1/(d1+d2);
  62. return s1.p1+(s1.p2-s1.p1)*t;
  63. }
  64. double area(Polygon poly){
  65. double res=0; long long size=poly.size();
  66. for (int i=0; i<poly.size(); i++)
  67. res+=poly[i].cross(poly[(i+1)%size]);
  68. return abs(res/2);
  69. }
  70. int contain(Polygon poly, Point p){
  71. int n=poly.size();
  72. bool flg=false;
  73. for (int i=0; i<n; i++){
  74. Point a=poly[i]-p, b=poly[(i+1)%n]-p;
  75. if (ccw(poly[i], poly[(i+1)%n], p)==0) return 1; // 1 means on the polygon.
  76. if (a.y>b.y) swap(a, b);
  77. if (a.y<0 && b.y>0 && a.cross(b)>0) flg=!flg;
  78. }return flg?2:0; // 2 fo inner, 0 for outer.
  79. }
  80. Polygon convexHull(Polygon poly){
  81. if (poly.size()<3) return poly;
  82. Polygon upper, lower;
  83. sort(poly.begin(), poly.end());
  84. upper.push_back(poly[0]); upper.push_back(poly[1]);
  85. lower.push_back(poly[poly.size()-1]); lower.push_back(poly[poly.size()-2]);
  86. for (int i=2; i<poly.size(); i++){
  87. for (int n=upper.size()-1; n>=1 && ccw(upper[n-1], upper[n], poly[i])!=-1; n--)
  88. upper.pop_back();
  89. upper.push_back(poly[i]);
  90. }
  91. for (int i=poly.size()-3; i>=0; i--){
  92. for (int n=lower.size()-1; n>=1 && ccw(lower[n-1], lower[n], poly[i])!=-1; n--)
  93. lower.pop_back();
  94. lower.push_back(poly[i]);
  95. }
  96. for (int i=1; i<lower.size(); i++)
  97. upper.push_back(lower[i]);
  98. return upper;
  99. }
  100. Segment seg[maxn];
  101. int n, m;
  102. int solve(Point p){
  103. int l=0, r=n;
  104. while (l<r){
  105. int mid=l+(r-l)/2;
  106. if (ccw(seg[mid].p1, seg[mid].p2, p)==-1) r=mid;
  107. else l=mid+1;
  108. }
  109. for (int i=max(l-3, 0); i<=min(l+3, n); i++)
  110. if (ccw(seg[i].p1, seg[i].p2, p)==-1)
  111. return l;
  112. }
  113. int main(void){
  114. long long x, y, x1, y1, x2, y2, xt1, xt2;
  115. while (scanf("%d", &n)==1 && n){
  116. int bin[maxn]={0};
  117. scanf("%d%lld%lld%lld%lld", &m, &x1, &y1, &x2, &y2);
  118. for (int i=0; i<n; i++){
  119. scanf("%lld%lld", &xt1, &xt2);
  120. seg[i].p1=Point(xt1-x1, y1-y2);
  121. seg[i].p2=Point(xt2-x1, y2-y2);
  122. }
  123. seg[n].p1=Point(x2-x1, y1-y2);
  124. seg[n].p2=Point(x2-x1, y2-y2);
  125. while (m--){
  126. scanf("%lld%lld", &x, &y);
  127. bin[solve(Point(x-x1, y-y2))]++;
  128. }
  129. for (int i=0; i<=n; i++)
  130. printf("%d: %d\n", i, bin[i]);
  131. printf("\n");
  132. }
  133. return 0;
  134. }
Time Memory Length Lang Submitted
204ms 716kB 4134 G++ 2018-08-01 12:19:23

POJ-2318 TOYS 计算几何 判断点在线段的位置的更多相关文章

  1. poj 2318 TOYS(计算几何 点与线段的关系)

    TOYS Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 12015   Accepted: 5792 Description ...

  2. POJ 2318 TOYS | 二分+判断点在多边形内

    题意: 给一个矩形的区域(左上角为(x1,y1) 右下角为(x2,y2)),给出n对(u,v)表示(u,y1) 和 (v,y2)构成线段将矩形切割 这样构成了n+1个多边形,再给出m个点,问每个多边形 ...

  3. POJ 2318 TOYS(计算几何)

    跨产品的利用率推断点线段向左或向右,然后你可以2分钟 代码: #include <cstdio> #include <cstring> #include <algorit ...

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

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

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

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

  6. POJ 2318 TOYS (计算几何,叉积判断)

    TOYS Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8661   Accepted: 4114 Description ...

  7. TOYS - POJ 2318(计算几何,叉积判断)

    题目大意:给你一个矩形的左上角和右下角的坐标,然后这个矩形有 N 个隔板分割成 N+1 个区域,下面有 M 组坐标,求出来每个区域包含的坐标数.   分析:做的第一道计算几何题目....使用叉积判断方 ...

  8. 【POJ】2318 TOYS ——计算几何+二分

    TOYS Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10281   Accepted: 4924 Description ...

  9. POJ 2318 TOYS (叉乘判断)

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

随机推荐

  1. Java使用反射通过对象属性获取属性的值

    代码: // 通过属性获取传入对象的指定属性的值 public String getValueByPropName(Student student, String propName) { String ...

  2. JWT加密

    JWT是一种加密算法,为了防止请求的信息在传输途中被拦截修改 JWT的引用: install-package jwt JWF由三部分组成:Header,Payload,Signature Payloa ...

  3. sklearn学习4----预处理(1)标准化

    一.[标准化]scale: 1.导入模块  from sklearn.preprocessing import scaler 2.作用:直接将给定数据进行标准化 3.使用代码 X_scaled=sca ...

  4. kernel zram feature

    what is zram? Zram wiki zram zram(也称为 zRAM,先前称为 compcache)是 Linux 内核的一项功能,可提供虚拟内存压缩.zram 通过在 RAM 内的压 ...

  5. UVA401-Palindromes(紫书例题3.3)

    A regular palindrome is a string of numbers or letters that is the same forward as backward. For exa ...

  6. 流媒体应用程序Mobdro或存在安全隐患

    Mobdro是一款流媒体应用程序,可以安装在任何Android设备上,包括手机,平板电脑,亚马逊的Fire TV Stick和Google的Chromecast.它现在已经流行了一段时间,特别是在围绕 ...

  7. 炫酷 CSS 背景效果的 10 个代码片段

    在现代网页设计中,大背景图设计非常流行.随着高清(现在是4K)显示器的出现,越来越多的网页设计师使用大背景图来填充屏幕. 因为这样可以造成很大的视觉冲击力,并有助于更好的传递所要表现的内容. 但是,如 ...

  8. Vue生命周期函数的应用

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. css表格表单和统筹

    css:表格表单和统筹 学习目标 1.表单标签及属性高级 2.表格标签及属性高级 3.CSS统筹 4.BFC概念和应用场景 一.表单标签及属性高级 回顾: 表单的作用:用来收集用户的信息的; 表单的组 ...

  10. 监控myserver计数器