Want to see more problem-solving reports: http://blog.csdn.net/wangjian8006/article/details/7870410
Reprinted please indicate the source: http://blog.csdn.net/wangjian8006
Similar to poj2318, the given line segments are no longer ordered, so they need to be sorted,
The output is also a little different. How many intervals are needed for the output of 1-m toys?
Also, after reading discuss, it indicates that it will blow up the int, modify the type, and write an inheritance class on the class 2318 to add features.
Although the Code is long, I am still familiar with the engineering method to write the code.
/* The meaning is the same as that of 2318, except that the given line segments are no longer ordered, so they need to be sorted, and the output is also a little different. Also, after reading discuss, it is said that the int will be cracked, write an inheritance class on 2318 of classes to add new features. Although the Code is long, I am still familiar with the engineering method to write the code. */# include <iostream> # include <algorithm> using namespace STD; # DEFINE _ maxl _ 5010 typedef long boxtype; typedef struct {boxtype X, Y;} Point; typedef struct {point first, second;} line; Class cbox {protected: boxtype ntop, nleft, nright, nbuttom; boxtype ncountline; // record the number of lines in the box * pline; // The boxtype * Prange of the record line information; // The number of boxtypes (point P1, point P2, point P0) of the toys in the record range; // calculate the public cross product: cbox (); Virtual ~ Cbox (); void setborder (boxtype top, boxtype left, boxtype right, boxtype buttom); // set the box rectangular coordinate void addline (boxtype topx, boxtype buttomx ); // Add a line boxtype getrange (boxtype Val) to the box; // obtain the number of toys in a certain range void settoy (point toy); // put a toy in the box }; class cboxtow: Public cbox {protected: boxtype * prangecount; public: cboxtow (); Virtual ~ Cboxtow (); void lineper (); // sort the line segments by void countrange (); // calculate the number of boxtype getrangecount (boxtype Val) intervals of each number of toys ); // obtain the number of intervals for a certain number of toys}; cbox: cbox () {ncountline = 0; pline = new line [_ maxl _]; prange = new boxtype [_ maxl _]; memset (Prange, 0, sizeof (boxtype) * _ maxl _);} cbox ::~ Cbox () {Delete [] pline; Delete [] Prange;} void cbox: setborder (boxtype top, boxtype left, boxtype right, boxtype buttom) {ntop = top; nleft = left; nright = right; nbuttom = buttom;} void cbox: addline (boxtype topx, boxtype buttomx) {pline [ncountline]. first. X = topx; pline [ncountline]. first. y = ntop; pline [ncountline]. second. X = buttomx; pline [ncountline]. second. y = nbuttom; ncountline ++;} boxtype cbox: getrange (Box Type Val) {return Prange [Val];} boxtype cbox: Multi (point P1, point P2, point P0) {return (p1.x-release X) * (p2.y-release y) -(p2.x-Snapshot X) * (p1.y-Snapshot y);} void cbox: settoy (point toy) {// use a binary search to find the int L in which the vertex is located, r, mid; L = 0, r = nCountLine-1; while (L <r) {mid = (L + r)> 1; if (multi (toy, pline [Mid]. first, pline [Mid]. second)> 0) L = Mid + 1; else r = mid;} If (multi (toy, pline [l]. first, pline [l]. second) <0) prang E [l] ++; else Prange [L + 1] ++;} cboxtow: cboxtow () {prangecount = new boxtype [_ maxl _]; memset (prangecount, 0, sizeof (boxtype) * _ maxl _);} cboxtow ::~ Cboxtow () {Delete [] prangecount;} bool CMP (const line & A, const line & B) {return. first. x <= B. first. x;} void cboxtow: lineper () {sort (pline, pline + ncountline, CMP);} void cboxtow: countrange () {int I; for (I = 0; I <= ncountline; I ++) {prangecount [Prange [I] ++ ;}} boxtype cboxtow: getrangecount (boxtype Val) {return prangecount [Val];} int main () {boxtype I, FX, lx; point a, B; boxtype n, m; while (CIN> N & N) {CIN> m>. x>. Y> B. x> B. y; cboxtow box; box. setborder (. y,. x, B. x, B. y); for (I = 0; I <n; I ++) {CIN> FX> lx; box. addline (FX, lx);} box. lineper (); for (I = 0; I <m; I ++) {CIN>. x>. y; box. settoy (a);} box. countrange (); cout <"box" <Endl; for (I = 1; I <= m; I ++) {If (box. getrangecount (I )! = 0) cout <I <":" <box. getrangecount (I) <Endl ;}} return 0 ;}