Test instructions
A straight line with a rectangle on a plane and a rectangle of n longitudinal segments are given.
The lines intersect each other, dividing the rectangle into n-1 blocks;
Now there are m points on the rectangle, and there are several points in each block.
n,m<=5000, the data guarantee point is strictly inside the block (not in the straight line);
Exercises
First the straight line, and then the most simple idea is to take a point one by one and block to compare;
This complexity is O (nm), and actually makes a lot of useless calculations;
For example: When you compare a block with a point, you can actually know whether the point is on the left or the right side of the block;
In other words, each comparison can be removed half of the block, that is, two-point solution;
Because of the left and right symmetry, we just need to determine whether the point is on the left side of the block;
This satisfies the dichotomy property, after using the cross product to judge, May O (Logn) solves;
Overall complexity O (MLOGN);
Code:
#include <math.h> #include <stdio.h> #include <string.h> #include <algorithm> #define N 5100using namespace Std;const double Eps=1e-6;const double inf=1e100;struct point{double x,y;friend point operator-(poin T A,point b) {point Temp;temp.x=a.x-b.x,temp.y=a.y-b.y;return temp;} Friend point operator + (point A,point b) {point Temp;temp.x=a.x+b.x,temp.y=a.y+b.y;return temp;} Friend Point operator * (double A,point b) {point Temp;temp.x=a*b.x,temp.y=a*b.y;return temp;} Friend double operator ^ (Point A,point B) {return a.x*b.y-a.y*b.x;}} St,en;struct line{point p,v;int cnt;void Build (double a,double b) {p=st; Point U=en;p.x=a;u.x=b;v=u-p;} BOOL Check (Point t) {if ((v^ (t-p)) >0) return 1;elsereturn 0;}} Li[n];int Main () {int n,m,i,j,k,l,r,mid;double A, b; Point T;while (scanf ("%d", &n) &&n) {memset (li,0,sizeof (LI)); scanf ("%d", &m); scanf ("%lf%lf%lf%lf", &ST.X,&EN.Y,&EN.X,&ST.Y); Li[0]. Build (st.x,st.x); li[n+1]. Build (en.x,en.x), for (i=1;i<=n;i++) {scanf ("%lf%lf", &a,&B); Li[i]. Build (b,a);} for (i=1;i<=m;i++) {scanf ("%lf%lf", &t.x,&t.y), L=0,r=n+1;while (l<=r) {mid=l+r>>1;if (Li[mid]. Check (t)) r=mid-1;elsel=mid+1;} li[l].cnt++;} for (i=1;i<=n+1;i++) {printf ("%d:%d\n", i-1,li[i].cnt);} Putchar (' \ n ');} return 0;}
poj-2318 TOYS