Hdu1377_counting squares (scanned line/line segment tree)

Source: Internet
Author: User

Solution report

Question:

Rectangular area and.

Ideas:

Scanned line + line segment tree

# Include <algorithm> # include <iostream> # include <cstring> # include <cstdio> using namespace STD; struct seg {int lx, RX, H, V; friend bool operator <(seg a, seg B) {return. h <B. h ;}} seg [500000]; int LZ [201000], sum [201000]; void push_up (int rt, int L, int R) {If (LZ [RT]) sum [RT] = R + 1-L; else if (L = r) sum [RT] = 0; else sum [RT] = sum [RT <1] + sum [RT <1 | 1];} void Update (int rt, int L, int R, int Ql, int QR, int v) {If (QL> r | | QR <L) return; If (QL <= L & R <= QR) {LZ [RT] + = V; push_up (RT, L, R ); return;} int mid = (L + r)> 1; Update (RT <1, L, mid, Ql, Qr, V ); update (RT <1 | 1, Mid + 1, R, Ql, Qr, V); push_up (RT, L, R);} int main () {int N, i, j, X1, Y1, X2, Y2, X3, Y3, X4, Y4; while (~ Scanf ("% d", & X1, & Y1, & X2, & Y2) {int m = 0; if (x1 = Y1 & X2 = Y2 & X1 = X2 & X1 =-2) break; memset (LZ, 0, sizeof (LZ )); memset (sum, 0, sizeof (SUM); seg [M]. lx = min (x1, x2); seg [M]. RX = max (x1, x2); seg [M]. V = 1; seg [M ++]. H = min (Y1, Y2); seg [M]. lx = min (x1, x2); seg [M]. RX = max (x1, x2); seg [M]. V =-1; seg [M ++]. H = max (Y1, Y2); While (~ Scanf ("% d", & X1, & Y1, & X2, & Y2 )) {If (x1 = Y1 & X2 = Y2 & X1 = X2 & X1 =-2) break; if (x1 = Y1 & X2 = Y2 & X1 = X2 & X1 =-1) break; seg [M]. lx = min (x1, x2); seg [M]. RX = max (x1, x2); seg [M]. V = 1; seg [M ++]. H = min (Y1, Y2); seg [M]. lx = min (x1, x2); seg [M]. RX = max (x1, x2); seg [M]. V =-1; seg [M ++]. H = max (Y1, Y2);} Sort (SEG, SEG + M); int ans = 0; for (I = 0; I <m-1; I ++) {Update (100,-1, SEG [I]. lx, SEG [I]. rx-1, SEG [I]. v); ans + = sum [1] * (SEG [I + 1]. h-seg [I]. h);} printf ("% d \ n", ANS);} return 0 ;}

Counting squares Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 1318 accepted submission (s): 671


Problem descriptionyour input is a series of rectangles, one per line. each rectangle is specified as two points (x, y) that specify the opposite corners of a rectangle. all coordinates will be integers in the range 0 to 100. for example, the line
5 8 7 10
Specifies the rectangle who's corners are (5, 8), (7, 8), (7, 10), (5, 10 ).
If drawn on graph paper, that rectangle wocould cover four squares. your job is to count the number of unit (I. E ., 1*1) squares that are covered by any one of the rectangles given as input. any square covered by more than one rectangle shoshould only be counted once.

Inputthe input format is a series of lines, each containing 4 integers. four-1's are used to separate problems, and four-2's are used to end the last problem. otherwise, the numbers are the X-ycoordinates of two points that are opposite corners of a rectangle.

Outputyour output shocould be the number of squares covered by each set of rectangles. Each number shocould be printed on a separate line.

Sample Input
 
5 8 7 106 9 7 86 8 8 11-1-1-10 0 100 10050 75 12 9039 42 57 73-2-2-2-2

Sample output
 
810000

Source Zhejiang University of Technology's fourth college Program Design Competition

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.