Rectangles of HDU-2461, State Compression

Source: Internet
Author: User

This is simply to find the area of the rectangle and the line segment tree? There are only 20 rectangles, so we can use them for rejection. However, this is troublesome because it requires an area in the case of a specific combination, and the write method that has been tried several times for each solution times out one by one. In this case, when DFS is selected, the query status is enumerated directly. If the current status is its subset, the query status is directly added to the List. The last M inquiry can be completed in O (1) time. Water passed in ms.

 

The Code is as follows:

 

# Include <cstring> # include <cstdlib> # include <cstdio> # include <algorithm> # define INF 10000 using namespace STD; int n, m, status [1250000], SEQ [100005]; struct rec {int x1, x2, Y1, Y2;} e [50]; inline void getint (Int & T) {char C; while (C = getchar (), C <'0' | C> '9'); t = C-'0'; while (C = getchar (), c >='0' & C <= '9') {T = T * 10 + C-'0';} void DFS (int p, int X1, int Y1, int X2, int Y2, int Sign, int Sta) {If (x1> = X2 | Y1> = Y2) return; // If the merging area is zero if (P = N) {If (STA! = 0) {for (INT I = 1; I <= m; ++ I) {If (SEQ [I] | Sta) <= seq [I]) {// indicates that the current status is a subset of I. status [seq [I] + = sign * (x2-X1) * (Y2-Y1) ;}} return ;} DFS (p + 1, X1, Y1, X2, Y2, sign, Sta); DFS (p + 1, max (x1, E [p + 1]. x1), max (Y1, E [p + 1]. y1), min (X2, E [p + 1]. x2), min (Y2, E [p + 1]. y2),-sign, Sta | (1 <p) ;}int main () {int R, C, STA, CA = 0; while (scanf ("% d", & N, & M), N | M) {memset (status, 0, sizeof (Status )); for (INT I = 1; I <= N; ++ I) {// scanf ("% d", & E [I]. x1, & E [I]. y1, & E [I]. x2, & E [I]. y2); getint (E [I]. x1), getint (E [I]. y1), getint (E [I]. x2), getint (E [I]. y2);} printf ("case % d: \ n", ++ CA); For (INT I = 1; I <= m; ++ I) {sta = 0; getint (r); For (Int J = 1; j <= r; ++ J) {getint (C ); sta | = 1 <(c-1);} seq [I] = sta; // keep all statuses} DFS (0, 0, 0, INF, INF, -1, 0); // once DFS finds all selected area for (INT I = 1; I <= m; ++ I) {printf ("query % d: % d \ n ", I, status [seq [I]);} puts (" ");} return 0 ;}

 

The following uses scanning lines to solve this problem. First, we reserve all the X coordinates of all the rectangles to be queried, sort them in ascending order, deduplicate them, and enumerate each small area, perform the height of the area on the rectangle to be asked, and calculate a part of the value immediately when the height is divided into two areas, to prevent the merge of the two regions, you need to sort the output rectangles by Y axis to ensure that the lower Y axis (the Y axis of the bottom edge) is in ascending order.

# Include <cstdlib> # include <cstdio> # include <cstring> # include <algorithm> # define INF 0x3fffffffusing namespace STD; int n, m, REC [25], line [50], Q; struct rectangle {int X1, Y1, X2, Y2;} e [25]; bool cmpy (int A, int B) {return E [A]. y1 <E [B]. y1;} int Merge () {int CNT = 0, L, R, U, D, sum = 0; For (INT I = 1; I <= Q; ++ I) {line [++ CNT] = E [Rec [I]. x1; line [++ CNT] = E [Rec [I]. x2;} Sort (REC + 1, REC + q + 1, cmpy); // sorts the rectangles stored in rec by Y1, so that sort (LINE + 1, line + 1 + CNT); CNT = unique (LINE + 1, line + 1 + CNT)-(line + 1); For (INT I = 2; I <= CNT; + + I) {L = line [I-1], r = line [I], u = 0, D = inf; For (Int J = 1; j <= Q; ++ J) {// traverse all the rectangles int c = rec [J]; If (E [C]. x1 <= L & E [C]. x2> = r) {If (E [C]. y1> U & U> d) {sum + = (R-l) * (u-d); U = E [C]. y2, D = E [C]. y1;} else {u = max (u, E [C]. y2); D = min (D, E [C]. y1) ;}}if (u> d) {sum + = (R-l) * (u-d) ;}} return sum ;} int main () {int CA = 0; while (scanf ("% d", & N, & M), N | M) {for (INT I = 1; I <= N; ++ I) {scanf ("% d", & E [I]. x1, & E [I]. y1, & E [I]. x2, & E [I]. y2);} printf ("case % d: \ n", ++ CA); For (INT I = 1; I <= m; ++ I) {scanf ("% d", & Q); For (Int J = 1; j <= Q; ++ J) {scanf ("% d ", & rec [J]);} printf ("query % d: % d \ n", I, merge () ;}puts ("") ;}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.