Segment Set
Time Limit: 3000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 3457 accepted submission (s): 1290
Problem descriptiona segment and all segments which are connected with it compose a segment set. the size of a segment set is the number of segments in it. the problem is to find the size of some segment set.
Inputin the first line there is an integer t-the number of test case. For each test case in first line there is an integer n (n <= 1000)-the number of commands.
There are two different commands described in different format shown below:
P X1 Y1 X2 Y2-paint a segment whose coordinates of the two endpoints are (x1, Y1), (X2, Y2 ).
Q k-query the size of the segment set which contains the k-th segment.
K is between 1 and the number of segments in the moment. There is no segment in the plane at first, so the first command is always a p-command.
Outputfor each Q-command, output the answer. There is a blank line between test cases.
Sample input110p 1.00 1.00 4.00 2.00 P 1.00-2.00 8.00 4.00q 1 p 2.00 3.00 3.00 1.00q 1q 3 P 1.00 4.00 8.00 2.00q 2 P 3.00 3.00-2.00q 5
Sample output12225
Authorll
Sourcehdu 2006-12 Programming Contest
There are n instructions, P is added to a line segment, and Q queries the number of elements in the set where the ID line segment is located (the intersection of the two lines is the same set.
Idea: compress and query the relationship between each line segment in the set path. The cross (V, W) = 0 w on V according to the cross product definition.> 0: W is above V, and <0: W is below v.
A required condition for intersection of two line segments: the two endpoints of each line segment must be on both sides or straight lines of the other line segment.
1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <cmath> 5 using namespace STD; 6 7 const double EPS = 1e-8; 8 const int maxn = 1005; 9 int f [maxn]; 10 struct point11 {12 Double X, Y; 13 point () {} 14 point (Double X, Double Y ): x (x), y (y) {}15}; 16 struct line17 {18 point a, B; 19} l [maxn]; 20 typedef point vector; 21 vector operator-(vector A, vector B) {return vector (. x-B.x,. y-B.y);} 22 I Nt dcmp (Double X) 23 {24 if (FABS (x) <EPS) return 0; 25 else return x <0? -1:1; 26} 27 double cross (vector A, vector B) {return. x * B. y-A.y * B. X ;}// Cross Product 28 29 bool judge (line A, line B) // cross (V, W) = 0 when W is on V,> 0: W is above V, <0: W is below V 30 {31 if (DCMP (Cross (. a-b.a, B. b-b.a) * Cross (. b-b.a, B. b-b.a) <= 032 & DCMP (Cross (B. a-a.a,. b-a.a) * Cross (B. b-a.a,. b-a.a) <= 0) 33 return true; 34 return false; 35} 36 int findset (int x) {return f [x]! = X? F [x] = findset (F [x]): X;} 37 void Union (int A, int B) 38 {39 A = findset (); B = findset (B); 40 if (! = B) f [a] = B; 41} 42 int main () 43 {44 int t, n, I, j, ID; 45 char op [5]; 46 Double X1, Y1, X2, Y2; 47 scanf ("% d", & T); 48 While (t --) 49 {50 scanf ("% d ", & N); 51 int CNT = 0; 52 for (I = 0; I <n; I ++) 53 {54 scanf ("% s", OP ); 55 if (OP [0] = 'P') 56 {57 scanf ("% lf", & X1, & Y1, & X2, & Y2); 58 L [++ CNT]. A = point (x1, Y1); L [CNT]. B = point (X2, Y2); 59 f [CNT] = CNT; 60 for (j = 1; j <= cnt-1; j ++) 61 If (Judge (L [CNT], L [J]) 62 Union (J, CNT); 63} 64 else 65 {66 int ans = 0; scanf ("% d", & ID); 67 id = findset (ID); 68 for (j = 1; j <= CNT; j ++) 69 If (findset (j) = ID) 70 ans ++; 71 printf ("% d \ n", ANS); 72} 73} 74 if (t) printf ("\ n"); 75} 76 return 0; 77}
HDU 1558 line segments intersection + query set path compression