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
思路:这题很恶心,卡线段树。只能考虑状压来做,但说实话这个状压也是暴力的一批,复杂度不忍直视。
将每个查询要计算的矩形状压下来。dfs的时候考虑容斥,其实等价于求出任意个矩形直接的相交面积。根据容斥的奇数-,偶数+原则即可。
代码:
//poj 3695
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
#include <map>
#include <vector>
#include <set>
#include <string>
#include <math.h>
#include <stack>
const int maxn=1<<21;
#define mod 998244353
#define Lson l,m,rt<<1
#define Rson m,r,rt<<1|1
#define inf 0x3f3f3f3f
typedef long long ll;
using namespace std;
int n,m;
struct Point
{
int x1,y1,x2,y2;
}p[25];
int query[maxn];
int ans[maxn];
void dfs(int x1,int y1,int x2,int y2,int pos,int sgn,int sta)
{
if(x1>=x2||y1>=y2) return;
if(pos==n+1)
{
if(sta){
for(int i=1;i<=m;i++)
{
if((query[i]&sta)==sta)
{
ans[query[i]]+=sgn*(x2-x1)*(y2-y1);
}
}
}
return;
}
int xx1=max(x1,p[pos+1].x1),yy1=max(y1,p[pos+1].y1);
int xx2=min(x2,p[pos+1].x2),yy2=min(y2,p[pos+1].y2);
dfs(x1,y1,x2,y2,pos+1,sgn,sta);
dfs(xx1,yy1,xx2,yy2,pos+1,-sgn,sta|(1<<pos));
return;
}
int main(int argc, char const *argv[])
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
int Case=0;
while(scanf("%d%d",&n,&m)!=EOF&&(n+m))
{
memset(query,0,sizeof(query));
memset(ans,0,sizeof(ans));
for(int i=1;i<=n;i++)
{
scanf("%d%d%d%d",&p[i].x1,&p[i].y1,&p[i].x2,&p[i].y2);
}
for(int i=1;i<=m;i++)
{
int num;
scanf("%d",&num);
while(num--)
{
int x;
scanf("%d",&x);
query[i]|=(1<<(x-1));
}
}
dfs(0,0,inf,inf,0,-1,0);
printf("Case %d:\n",++Case);
for(int i=1;i<=m;i++)
{
printf("Query %d: %d\n",i,ans[query[i]]);
}
printf("\n");
}
return 0;
}