Rectangles hdu2461容斥定理

Rectangles

Time Limit: 5000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1259    Accepted Submission(s): 661


Problem Description
You are developing a software for painting rectangles on the screen. The software supports drawing several rectangles and filling some of them with a color different from the color of the background. You are to implement an important function. The function answer such queries as what is the colored area if a subset of rectangles on the screen are filled.
 
Input
The input consists of multiple test cases. Each test case starts with a line containing two integers N(1 ≤ N ≤ 20) and M(1 ≤ M ≤ 100000), indicating the number of rectangles on the screen and the number of queries, respectively.
The i-th line of the following N lines contains four integers X1,Y1,X2,Y2 (0 ≤ X1 < X2 ≤ 1000, 0 ≤ Y1 < Y2 ≤ 1000), which indicate that the lower-left and upper-right coordinates of the i-th rectangle are (X1, Y1) and (X2, Y2). Rectangles are numbered from 1 to N.
The last M lines of each test case describe M queries. Each query starts with a integer R(1<=R ≤ N), which is the number of rectangles the query is supposed to fill. The following list of R integers in the same line gives the rectangles the query is supposed to fill, each integer of which will be between 1 and N, inclusive.

The last test case is followed by a line containing two zeros.
 
Output
For each test case, print a line containing the test case number( beginning with 1).
For each query in the input, print a line containing the query number (beginning with 1) followed by the corresponding answer for the query. Print a blank line after the output for each test case.
 
Sample Input
2 2
0 0 2 2
1 1 3 3
1 1
2 1 2
2 1
0 1 1 2
2 1 3 2
2 1 2
0 0
 
Sample Output
Case 1:
Query 1: 4
Query 2: 7
 
Case 2:
Query 1: 2
 
 
没优化版:
 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <string.h>
 4 #include <math.h>
 5 #include <algorithm>
 6 using namespace std;
 7 #define INF 100000000
 8 typedef struct point
 9 {
10     int x1,y1,x2,y2;
11 }point;
12 point p[100];
13 int ans[1100000]={0};
14 int n;
15 void dfs(int x1,int y1,int x2,int y2,int deep,int sign,int sta)
16 {
17     if( x1 >= x2 || y1 >= y2 ) return;
18     if(deep==n)
19     {
20         if(sta)
21         for(int i=1;i<(1<<n);i++)
22         {
23             if((i|sta)<=i)
24             ans[i]+=sign*(x2-x1)*(y2-y1);
25         }
26         return ;
27     }
28     dfs(x1,y1,x2,y2,deep+1,sign,sta);
29     dfs(max(x1,p[deep].x1),max(y1,p[deep].y1),min(x2,p[deep].x2),min(y2,p[deep].y2),deep+1,-sign,sta|(1<<deep));
30 }
31 int main()
32 {
33     int m,i,ss,cas=1,mm,x,cass;
34     while(scanf("%d%d",&n,&m),(n||m))
35     {
36         memset(ans,0,sizeof(ans));
37         for(i=0;i<n;i++)
38             scanf("%d%d%d%d",&p[i].x1,&p[i].y1,&p[i].x2,&p[i].y2);
39         dfs(0,0,INF,INF,0,-1,0);
40         printf("Case %d:
",cas++);
41         cass=1;
42         while(m--)
43         {
44             scanf("%d",&mm);
45             ss=0;
46             for(i=0;i<mm;i++)
47             {
48                 scanf("%d",&x);
49                 ss|=(1<<(x-1));
50             }
51             printf("Query %d: %d
",cass++,ans[ss]);
52         }
53         printf("
");
54     }
55 }
View Code

 优化版:

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <string.h>
 4 #include <math.h>
 5 #include <algorithm>
 6 using namespace std;
 7 #define INF 100000000
 8 typedef struct point
 9 {
10     int x1,y1,x2,y2;
11 } point;
12 point p[100];
13 int ans[1100000],staa[100005];
14 int n,m;
15 void dfs(int x1,int y1,int x2,int y2,int deep,int sign,int sta)
16 {
17     if( x1 >= x2 || y1 >= y2 ) return;
18     if(deep==n)
19     {
20         if(sta)
21             for(int i=0; i<m; i++)
22             {
23                 if((staa[i]|sta)<=staa[i])
24                     ans[staa[i]]+=sign*(x2-x1)*(y2-y1);
25             }
26         return ;
27     }
28     dfs(x1,y1,x2,y2,deep+1,sign,sta);
29     dfs(max(x1,p[deep].x1),max(y1,p[deep].y1),min(x2,p[deep].x2),min(y2,p[deep].y2),deep+1,-sign,sta|(1<<deep));
30 }
31 int main()
32 {
33     int i,cas=1,mm,x,cass;
34     while(scanf("%d%d",&n,&m),(n||m))
35     {
36         memset(ans,0,sizeof(ans));
37         memset(staa,0,sizeof(staa));
38         for(i=0; i<n; i++)
39             scanf("%d%d%d%d",&p[i].x1,&p[i].y1,&p[i].x2,&p[i].y2);
40         printf("Case %d:
",cas++);
41         cass=0;
42         while(m--)
43         {
44             scanf("%d",&mm);
45             for(i=0; i<mm; i++)
46             {
47                 scanf("%d",&x);
48                 staa[cass]|=(1<<(x-1));
49             }
50             cass++;
51         }
52         m=cass;
53         dfs(0,0,INF,INF,0,-1,0);
54         for(i=1; i<=cass; i++)
55             printf("Query %d: %d
",i,ans[staa[i-1]]);
56         printf("
");
57     }
58 }
View Code
原文地址:https://www.cnblogs.com/ERKE/p/3681383.html