Zoj 3664 split the rectangle 37th ACM/ICPC Changchun Division Field Competition J question (simulate achievements, brute force find LCA)

Source: Internet
Author: User
Split the rectangle
Time limit:2 seconds Memory limit:32768 KB

Alice comes up with a new game and wants to play with Bob.

There is one rectangle on the paper initially, we define its lower-left corner's coordinate is (XL, yl) and upper-right corner's coordinate is (XR, yr ).

Bob has executed the step as description below n times:

Bob shoshould select one empty rectangle. if the rectangle is the initial one or is split by one vertical segment, he shocould split it into two parts by drawing one horizontal segment; otherwise he shoshould split the rectangle into two parts by drawing one vertical segment. an empty rectangle means there is no segment in this rectangle variable t its only four boundary segments.

You shoshould pay attention that there are only two kinds segments: vertical segment and horizontal segment.

Now Bob has several queries. in each query, Bob selects two target points in the paper. (you can assume that all given target points are always located inside the initial rectangle and not in any drawing segments .) he wants Alice to answer the question as soon as possible: Alice can erase several existing segments, and make two target points in one empty rectangle, and she shoshould answer how should empty rectangles at most wocould be left at last.

But there are some restrictions: Alice cannot erase segments of the initial rectangle (the (XL, yl) to (XR, yr) One), she can only erase segments drew by Bob; if Alice want to erase one segment, both sides of the segment must be empty rectangles, and after erase it, the two empty rectangles must combine to one bigger empty rectangle; if erasing an existing segment will lead to a disconnected graph, the operation is forbidden.

Input

There are multiple test cases.

The first line contains four integers XL, YL, xr, yr indicating the coordinates of the lower-left corner and the upper-right corner of the initial huge rectangle respectively. (-100,000 ≤ XL, YL, xr, yr ≤ 100,000, XL <XR, yl <yr)

The next line contains two integers n and Q. (1 ≤ n, Q ≤1000)

The next n lines each line contains four integers X1, Y1, X2, Y2 indicating the coordinates of two endpoints of one drawing segments. (-100,000 ≤ X1, Y1, X2, Y2 ≤ 100,000, X1 = X2 | Y1 = Y2)

The next Q lines each line contains four integers XA, ya, XB, Yb indicating the coordinates of two target points in this query. (-100,000 ≤ XA, ya, XB, Yb ≤ 100,000 ).

Output

For each test case, output Q lines, output the answer of each query in each line.

Sample Input
 
-10-10 10 105 1-10 0 10 05-10 5 0-5 0-5 10-5 5 5 10 55-5 10-50-3 7-30 0 4 43 20 2 4 22 0 2 2 2 41 1 1 31 1 3 1-10-10 10 103 4-10 0 10 00-10 0 00 0 0 0 10-9-9- 8-8-9-9-9 9-9-9 9-9-9 9
Sample output
 
4134131

Contest:The 2012 ACM-ICPC Asia Changchun Regional Contest

 

During the Changchun competition, I did not even read any questions about the cup.

In fact, it is not difficult to understand this question.

Create a tree based on the added edge.

Then, each query is to find the leaf node number of the vertex. Then find the LCA of the two leaf nodes, and try to find the LCA brute force.

Then the answer is n + 1-(number of leaf nodes under the node where the LCA is located) + 1

 

A good question.

Note that the given points are not ordered.

# Include <stdio. h> # Include < String . H> # Include <Algorithm> # Include <Iostream> # Include <Math. h> Using  Namespace  STD;  Const   Int Maxn = 2020  ;  Struct  Node {  Int  Lson, rson, father;  Int Dep; //  Depth      Int  XL, YL, xr, yr;  Void Init (Int De, Int  F) {lson = Rson = 0  ; Father = F; Dep = De ;}  Void Update ( Int A, Int B, Int C, Int  D) {XL = A; yl = B; xr = C; yr =D ;}} node [maxn];  //  Node  Int  Tol;  Int Num [maxn]; //  Number of leaf nodes under each node  Int Root; //  Root Node  Int Find ( Int Root, Int X, Int  Y ){ Int TMP = Root, TT;  While ( 1  ){  If (Node [TMP]. lson = 0 ) Return  TMP; TT = Node [TMP]. lson;  If (X <= node [TT]. XR & x> = node [TT]. XL & Y> = node [TT]. YL & Y <= node [TT]. yr) TMP = TT;  Else TMP =Node [TMP]. rson ;}}  Int Get_num ( Int  Now) {num [now] = 0  ;  If (Node [now]. lson = 0  ){  Return Num [now] = 1  ;}  Else  {Num [now] + =Get_num (node [now]. lson); num [now] + = Get_num (node [now]. rson );}  Return  Num [now];}  Int  Main (){  //  Freopen ("in.txt", "r", stdin );  //  Freopen ("out.txt", "W", stdout );      Int  XL, YL, xr, yr;  Int  N, Q;  While (Scanf ( "  % D  " , & XL, & YL, & XR, & yr )! = EOF) {Root = 0  ; Node [root]. INIT (  0 ,- 1  ); Node [root]. Update (XL, YL, xr, yr); Tol = 1  ; Scanf (  "  % D  " , & N ,&Q );  For ( Int I = 1 ; I <= N; I ++ ) {Scanf (  "  % D  " , & XL, & YL, & XR ,& Yr );  If (XL> Xr) Swap (XL, xr );  If (Yl> Yr) Swap (YL, yr );  Int Pos = find (root, (XL + XR )/ 2 , (Yl + yr )/ 2  );  Int Dep = Node [POS]. Dep; node [POS]. lson = Tol; node [tol]. INIT (Dep + 1  , POS); node [tol]. Update (node [POS]. XL, node [POS]. YL, xr, yr); Tol ++ ; Node [POS]. rson = Tol; node [tol]. INIT (Dep + 1 , POS); node [tol]. Update (XL, YL, node [POS]. XR, node [POS]. yr); Tol ++ ;} Get_num (Root );  While (Q -- ) {Scanf (  "  % D  " , & XL, & YL, & XR ,& Yr );  Int Tmp1 = Find (root, XL, yl );  Int Tmp2 = Find (root, xr, yr ); While (Tmp1! = Tmp2 ){  If (Node [tmp1]. Dep <node [tmp2]. Dep) tmp2 = Node [tmp2]. Father;  Else   If (Node [tmp1]. Dep> node [tmp2]. Dep) tmp1 = Node [tmp1]. Father;  Else  {Tmp1 = Node [tmp1]. Father; tmp2 = Node [tmp2]. Father;} printf ( "  % D \ n  " , N + 1 -Num [tmp1] + 1  );}}  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.