Hdu1542 line segment tree + scanning line + discretization, hdu1542 Line Segment

Source: Internet
Author: User

Hdu1542 line segment tree + scanning line + discretization, hdu1542 Line Segment

I just want to say whether the question is actually unacceptable or not. I suggest you refer to the one I wrote earlier.



# Include <stdio. h>

# Include <string. h>
# Include <algorithm>
# Include <iostream>
Using namespace std;
# Define LL (x) (x <1)
# Define RR (x) (x <1) | 1)


Int n;
Struct node
{
Double y;
Int x1, x2;
Int flash;
} A [31000];
Struct Node
{
Int ii;
Double x;
} Change [1, 35000];
Int cmp1 (Node a, Node B)
{
Return a. x <B. x;
}
Int cmp2 (node a, node B)
{
Return a. y <B. y;
}
Int num [80000];
Double map [31000];
Int update (int L, int R, int left, int right, int k, int mark)
{
Int mid = (L + R)/2;
If (L = left & right = R)
{
If (num [mark]! =-1)
{
Num [mark] + = k;
Return 0;
}
Update (L, mid, L, mid, k, LL (mark ));
Update (mid, R, mid, R, k, RR (mark ));
If (num [LL (mark)] = num [RR (mark)]) num [mark] = num [LL (mark)];
Else num [mark] =-1;
}
Else
{
If (num [mark]> = 0)
{
Num [LL (mark)] = num [RR (mark)] = num [mark];
}
If (right <= mid)
{
Update (L, mid, left, right, k, LL (mark ));
}
Else if (left> = mid)
{
Update (mid, R, left, right, k, RR (mark ));
}
Else
{
Update (L, mid, left, mid, k, LL (mark ));
Update (mid, R, mid, right, k, RR (mark ));
}
If (num [LL (mark)] = num [RR (mark)]) num [mark] = num [LL (mark)];
Else num [mark] =-1;
}
Return 0;
}
Double find (int L, int R, int mark)
{
Double Long = 0;
If (num [mark]> = 1)
{
Long + = map [R]-map [L];
Return Long;
}
If (num [mark] = 0) return 0;
Int mid = (L + R)/2;
Long + = find (L, mid, LL (mark) + find (mid, R, RR (mark ));
Return Long;
}
Int main ()
{
Int I, j, d = 1;
Double x1, y1, x2, y2;
While (~ Scanf ("% d", & n), n)
{
N * = 2;
// J = 0;
Memset (A, 0, sizeof ());
Memset (map, 0, sizeof (map ));
Memset (change, 0, sizeof (change ));
For (I = 1; I <= n; I + = 2)
{
Scanf ("% lf", & x1, & y1, & x2, & y2 );
A [I]. y = y1;
A [I]. flash = 1;
A [I + 1]. y = y2;
A [I + 1]. flash =-1;
Change [I]. x = x1;
Change [I + 1]. ii = change [I]. ii = I;
Change [I + 1]. x = x2;
}
Int leap [10000];
Memset (leap, 0, sizeof (leap ));
Sort (change + 1, change + 1 + n, cmp1 );
Int j = 0;
Double k =-1;
For (I = 1; I <= n; I ++)
{
If (change [I]. x! = K)
{
K = change [I]. x;
++ J;
Map [j] = change [I]. x;
}
If (leap [change [I]. ii] = 0)
{
A [change [I]. ii]. x1 = A [change [I]. ii + 1]. x1 = j;
Leap [change [I]. ii] = 1;
}
Else A [change [I]. ii]. x2 = A [change [I]. ii + 1]. x2 = j;
}
Sort (A + 1, A + 1 + n, cmp2 );
Memset (num, 0, sizeof (num ));
Update (1, j, A [1]. x1, A [1]. x2, 1, 1 );
Double area = 0;
For (I = 2; I <= n; I ++)
{
Area + = find (1, j, 1) * (A [I]. y-A [I-1]. y );
If (A [I]. flash = 1)
Update (1, j, A [I]. x1, A [I]. x2, 1, 1 );
Else update (1, j, A [I]. x1, A [I]. x2,-1, 1 );
}
Printf ("Test case # % d \ n", d ++ );
Printf ("Total received area: %. 2lf \ n", area );
Printf ("\ n ");
}
Return 0;
}
How to Use the inverse Number of Line Segment Tree Knowledge

Very simple ......
Set series to a, and discretization the series. In the previous and later enumeration, calculate the answer ......
Discretization: for example, 2 5 8 3 10 is equivalent to 1 3 4 2 5, which can be solved by sorting and adding small processing.
To enumerate the number of I, we need to find from 1 to the number of I-1 is greater than a [I], update the answer.
How can this problem be solved?
After each enumeration, insert a number into the line segment tree and insert it to the Shenma place of the Line Segment tree? Of course, this is where the number is inserted.
For example, 3 2 4 1. The changes of the Line Segment tree should be: tree [3] + = 1; tree [2] + = 1; tree [4] + = 1; tree [1] + = 1;
Set x = a [I]. In this way, when inserting a number X, first calculate the tree [x + 1] ~ Sum of tree [n], which is 1 ~ The number of I-1 numbers is larger than a [I. We can use the line segment tree summation to achieve O (n log n). I will not go into details about the specific implementation.
 
Pascal questions and procedures of discretization line segment tree

Refer to this file

 

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.