rectangles
Time Limit: 2000MS |
|
Memory Limit: 65536K |
Total Submissions: 3730 |
|
Accepted: 1082 |
Description
You is developing a software for painting rectangles in the screen. The software supports drawing several rectangles and filling some of them with a color different from the color of the BAC Kground. You is to implement an important function. The function answer such queries as what's the colored area if a subset of rectangles in the screen is filled.
Input
The input consists of multiple test cases. Each test case starts with a line containing the integersN(1≤N≤20) andM(1≤M≤100000), indicating the number of rectangles on the screen and the number of queries, respectively.
TheI-th Line of the followingNLines contains four integersX1,Y1,X2,Y2 (0≤X1 <X2≤1000, 0≤y1 < y2≤1000), which indicate that the lower-left and upper-right coordinates of theI-th Rectangle is (X1,Y1) and (X2,Y2). Rectangles is numbered from 1 toN.
The lastMLines of each test case describeMQueries. Each query starts with a integerR(1<=R≤N), which is the number of rectangles the query was supposed to fill. The following list ofRIntegers in the same line gives the rectangles the query was supposed to fill with each integer of which would be between 1 andN, inclusive.
The last test was followed by a line containing the zeros.
Output
For each test case, print a line containing the test case number (beginning with 1).
For each query of the input, print a line containing the query number (beginning with 1) followed by the corresponding ANS Wer for the query. Print a blank line after the output for each test case.
Sample Input
2 20 0 2 21 1 3 31 12 1 22 10 1 1 22 1 3 22 1 20 0
Sample Output
Case 1:query 1:4query 2:7case 2:query 1:2
Source
Asia Hefei Regional Contest Online by USTC
Title Link: http://poj.org/problem?id=3695
The main idea: to give n rectangles, with their pairs of vertices, m query, each query gives the number of R, to find the R rectangle together with the plot area
Topic Analysis: There is a special feature is that n is very small, but M is very large, so we can put the state of inquiry with binary compression, and then directly calculate the status of these inquiries, O (1) query can, calculate each state, in the DFS, such as three rectangular 22 intersect, set an area of a,b,c a∪b∪c = A + B + C-a∩b-a∩c-b∩c + a∩b∩c, when the intersection can be calculated by the coordinates of the corner to calculate the area of the Intersect, as for the tolerance of the symbol, in the DFS with a parameter Sgin said, each time after the change number can be, For each rectangle (including the small rectangles that come out), search for and select two different states respectively
#include <cstdio> #include <cstring> #include <algorithm>using namespace std;int const INF = 0x3fffffff ; int const MAX = 1 << 21;int Q[max], ans[max];int N, m;struct r{int x1, y1; int x2, y2;} r[25];void DFS (int x1, int y1, int x2, int y2, int pos, int sgin, int sta) {if (x1 >= x2 | | y1 >= y2) retur N if (pos = = n + 1) {if (STA) for (int i = 1; I <= m; i++) if ((Q[i] & STA) = = STA) Ans[q[i]] + = Sgin * (x2-x1) * (Y2-Y1); Return } int xx1 = max (x1, R[pos + 1].x1); int yy1 = max (y1, R[pos + 1].y1); int xx2 = min (x2, R[pos + 1].x2); int yy2 = min (y2, r[pos + 1].y2); DFS (x1, y1, x2, y2, pos + 1, sgin, STA); DFS (xx1, Yy1, xx2, Yy2, pos + 1,-sgin, STA | (1 << POS)); return;} int main () {int CA = 1; while (scanf ("%d%d", &n, &m)! = EOF && (n + m)) {memset (q, 0, sizeof (q)); memset (ans, 0, sizeof (ans)); printf ("Case%d:\n", ca++); for (int i = 1; I <= n; i++) scanf ("%d%d%d%d", &r[i].x1, &r[i].y1, &r[i].x2, &r[i].y2); for (int i = 1; I <= m; i++) {int num; scanf ("%d", &num); while (Num--) {int tmp; scanf ("%d", &tmp); Q[i] |= (1 << (tmp-1)); }} DFS (0, 0, INF, INF, 0,-1, 0); for (int i = 1; I <= m; i++) printf ("Query%d:%d\n", I, Ans[q[i]]); printf ("\ n"); }}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
POJ 3695 rectangles (Rectangle-shaped pressure + capacity repulsion theorem good question)