and check the SET + compute geometry.
1 /*1558*/2#include <cstdio>3#include <cstring>4#include <cstdlib>5 6 #defineMAXN 10057 8typedefstruct {9 Doublex, y;Ten } point_t; One Atypedefstruct { - point_t B, E; - } seg_t; the - seg_t SEGS[MAXN]; - intPRE[MAXN]; - intANS[MAXN]; + - intMaxintAintb) { + returnA>b?a:b; A } at - intMinintAintb) { - returnA<b?a:b; - } - - Doubledirection (point_t p0, point_t p1, point_t p2) { in return(p1.x-p0.x) * (P2.Y-P0.Y)-(P1.Y-P0.Y) * (p2.x-p0.x); - } to + BOOLonsegment (point_t p0, point_t p1, point_t p2) { - if(Min (p0.x, p1.x) <=p2.x && P2.x<=max (p0.x, p1.x) && the(Min (p0.y, p1.y) <=p2.y && p2.y<=Max (p0.y, P1.y ))) * return true; $ return false;Panax Notoginseng } - the BOOLintersect (seg_t x, seg_t y) { +point_t P1 = x.b, p2 = x.e, p3 = y.b, P4 =y.e; A DoubleD1 =direction (P3, P4, p1); the DoubleD2 =direction (P3, P4, p2); + DoubleD3 =direction (P1, P2, p3); - DoubleD4 =direction (P1, P2, p4); $ if(((d1>0&& d2<0) || (d1<0&& d2>0)) && $((d3>0&& d4<0) || (d3<0&& d4>0)) ) - return true; - Else if(d1==0&&onsegment (P3, P4, p1)) the return true; - Else if(d2==0&&onsegment (P3, P4, p2))Wuyi return true; the Else if(d3==0&&onsegment (P1, p2, p3)) - return true; Wu Else if(d4==0&&onsegment (P1, P2, p4)) - return true; About Else $ return false; - } - - intFindintx) { A if(x = =Pre[x]) + returnx; thePRE[X] =find (Pre[x]); - returnPre[x]; $ } the the voidMergeintXinty) { the intFX =find (x); the intFY =find (y); - if(FX! =FY) { inPRE[FY] =FX; theANS[FX] + =Ans[fy]; theANS[FY] =0; About } the } the the intMain () { + intt, N, M; - intI, J, K; the Charcmd[3];Bayi the #ifndef Online_judge theFreopen ("data.in","R", stdin); - #endif - thescanf"%d", &t); the while(t--) { thescanf"%d", &n); the for(i=1; i<=n; ++i) { -Pre[i] =i; theAns[i] =1; the } them =1;94 while(n--) { thescanf"%s", cmd); the if(cmd[0] =='P') { thescanf"%LF%LF%LF%LF", &segs[m].b.x, &segs[m].b.y, &segs[m].e.x, &segs[m].e.y);98 for(i=1; i<m; ++i) { About if(Intersect (Segs[i], segs[m])) - merge (I, m);101 }102++m;103}Else {104scanf"%d", &j); theK =find (j);106printf"%d\n", Ans[k]);107 }108 }109 if(t) theprintf"\ n");111 } the 113 return 0; the}
"Hdoj" 1558 Segment set