(Hdu step 5.1.3) Segment set (calculate the number of segments in the set of line segments that intersect a line Segment), hdu5.1.3

Source: Internet
Author: User
Tags x2 y2

(Hdu step 5.1.3) Segment set (calculate the number of segments in the set of line segments that intersect a line Segment), hdu5.1.3



Question:

Segment set
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission (s): 177 Accepted Submission (s): 82
 
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.
Output
For each Q-command, output the answer. There is a blank line between test cases.
Sample Input
110P 1.00 1.00 4.00 2.00P 1.00 -2.00 8.00 4.00Q 1P 2.00 3.00 3.00 1.00Q 1Q 3P 1.00 4.00 8.00 2.00Q 2P 3.00 3.00 6.00 -2.00Q 5
 
Sample Output
12225
 
AuthorLL
SourceHDU 2006-12 Programming Contest
RecommendLL


Question:

Here we will explain the concept of "Line Segment Set": There are three line segments A, B, and C. If A and B intersect, B and C intersect (line A and line C can not be directly intersection). At this time, A, B, C still belong to the same line segment set. In the input example, P indicates adding a new edge, followed by an edge.

The abscissa coordinate of the start point and the abscissa coordinate of the end point


Question Analysis:

Calculate the ry + and query the set. In fact, we only use the concept of "intersection" in the calculation set to determine whether two line segments are intersecting. The general idea is that when adding a line segment, if the two line segments intersect, the two lines will be merged into the same line segment set. Then query a certain item during Q.

The number of line segments in the collection of line segments.


The Code is as follows:

/** C. cpp ** Created on: February 28, 2015 * Author: Administrator */# include <iostream> # include <cstdio> # include <algorithm> # include <cmath> using namespace std; const int maxn = 1001; int father [maxn]; // used to save the parent-child relationship int r [maxn]; // used to save the weight of a node as the root node. (It can be understood as the number of vertex segments in a line segment set ). for example, r [a] indicates the int counter with the weight of the set where a is the root node; // number of current edges/*** search for the root node of the set where a node is located */int find (int a) {if (a = father [a]) {return a;} return father [a] = find (father [a]);} /*** merge the set of node a with the set of Node B */void join (int a, int B) {int fa = find (); // find the root node int fb = find (B) of the set where node a is located; // find the root node of the set where Node B is located if (fa! = Fb) {// if the root node of the set where a is located is different from the root node of the set where B is located, father [fa] = fb; // The two sets are merged. in the operation room, the root node fa of the set where a is located points to the root node fbr [fb] + = r [fa] of the set where Node B is located; // Add the fa weight of the node to the node fb/*** reset the fa weight of the node to 0. why? * It is mainly used to avoid repeated addition of weights. This is determined by the execution operation of the addEdge () function. * Each time a new edge exists, addEdge () traverses all the edges in the edge set to determine whether it is intersecting with the new edge. * If yes, the join () operation is executed (). at this time, the weights of one whip will be moved to the other side. * For example, there are three edges with numbers 1, 2, and 3. They add the edge set in sequence. their weights are 1, 1, and 1. if all three edges belong to the same set, * the weight of this set should be 1 + 1 + 1 = 3. ** when edge 2 is added, if edge 1 and edge 2 are mutually exceeded, the weight of edge 2 is 2 * If the weight of Node 1 is not reset to 0, in the case that the edges 2 and 1 on January 1, * after merging, the weight of the query set obtained is 1 + 2 + 1 = 4, this is obviously incorrect */r [fa] = 0 ;}} struct Point {// Point double x; // abscissa double y; // ordinate }; struct Edge {// Edge Point start; // start Point end; // end Point} edges [maxn]; // side set double multiply (Point p1, Point p2, Point p0) {return (p1.x-converted X) * (p2.y-converted y)-(p2.x-p0. X) * (p1.y-Policy);} // a (x1, y1), B (x2, y2) // x1 * y2-x2 * y1/*** determine whether two line segments are intersecting */int intersect (Edge u, Edge v) {return (max (u. start. x, u. end. x)> = min (v. start. x, v. end. x) & // whether the rightmost vertex in u is on the right of the leftmost vertex in v (max (v. start. x, v. end. x)> = min (u. start. x, u. end. x) & // whether the rightmost vertex in v is on the right of the leftmost vertex in u. start. y, u. end. y)> = min (v. start. y, v. end. y) & // whether the top vertex in the u is above the bottom vertex in the v (max (v. start. y, v. end. y)> = min (u. start. y, U. end. y) & // determines whether the top vertex of the most vertices in the v is located at the top of the vertices in the u. // determines whether the two line segments may overlap at the vertical layer (multiply (v. start, u. end, u. start) * multiply (u. end, v. end, u. start)> = 0) & // Judge v. start, v. whether end is distributed in u. end (or online) (multiply (u. start, v. end, v. start) * multiply (v. end, u. end, v. start)> = 0); // judge u. start, u. whether end is distributed in v. start side (or online)}/*** add edge operation */void addEdge () {int I; for (I = 1; I <counter; ++ I) {// compare the newly added edge with all edges in the current edge set if (intersect (edges [I], edges [counte R]) = true) {// determines whether they are joined (I, counter ); // merge operations if intersection exists.}/*** initialize */void init () {int I; for (I = 1; I <maxn; ++ I) {// traverse all nodes. the index starts from 1. Do not start from 0. otherwise there will be some problems. seriously think about why father [I] = I; // father's Day of all nodes is its own r [I] = 1 by default; // The default weight of all nodes is 1} int main () {int t; scanf ("% d", & t); int cas = 0; // It is mainly used to print an empty row while (t --) {if (cas! = 0) {// note the same statement. in some cases, all blank rows can be AC, and some must be the last one cannot be empty rows printf ("\ n");} cas ++; counter = 1; // The number of edges starts from 1 init (); int m; scanf ("% d", & m); string str; int I; for (I = 1; I <= m; ++ I) {// please note the input sample in this form: cin> str; if (str [0] = 'P ') {scanf ("% lf", & edges [counter]. start. x, & edges [counter]. start. y, & edges [counter]. end. x, & edges [counter]. end. y); addEdge (); counter ++;} else {int index; scanf ("% d", & index); printf ("% d \ n ", r [find (index)]) ;}// printf ("\ n") ;}return 0 ;}











Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.