POJ3695-Rectangles

来源:互联网 发布:淘宝春季女装新款 编辑:程序博客网 时间:2024/06/05 09:22
Rectangles
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 3497 Accepted: 1003

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  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
//AC代码
/*题意:求多个矩形的面积并由于此题询问很多不适合线段树做(大牛除外,反正本人做不出来一直TLE到死)所以我看了别人许多都是直接离散化+暴力,虽然1900ms+过了但是好歹是过了下面就是离散化+暴力 1964MS飘过*/#include<iostream>#include<queue>#include<cstdio>#include<algorithm>#include<cstring>#include<iomanip>#include<map>#include<cstdlib>#include<cmath>#include<vector>#define LL long long#define IT __int64#define zero(x) fabs(x)<eps#define mm(a,b) memset(a,b,sizeof(a))const int INF=0x7fffffff;const double inf=1e8;const double eps=1e-10;const double PI=acos(-1.0);const int Max=51;const int maxn=100001;using namespace std;typedef struct Node{    int x;    int y;    int mark;}point;point pnt[Max];int mark_x[Max];//int mark_y[Max];int xx[Max];int yy[Max];int area[Max][Max];bool ok[Max][Max];bool cmp_x(point u,point v){    return u.x<v.x;}bool cmp_y(point u,point v){    return u.y<v.y;}int main(){    int n,m,i,j,k,num,T,P,res;    int Area;    T=1;    while(~scanf("%d%d",&n,&m)&&(n||m))    {        for(i=1,k=0;i<=n;i++)        {            scanf("%d%d%d%d",&pnt[k].x,&pnt[k].y,&pnt[k+1].x,&pnt[k+1].y);            pnt[k++].mark=i;            pnt[k++].mark=i+n;        }        sort(pnt,pnt+k,cmp_x);//把x按从小到大排序        for(i=0;i<k;i++)        {            xx[i]=pnt[i].x;            mark_x[pnt[i].mark]=i;        }        sort(pnt,pnt+k,cmp_y);//把y按从小到大排序        for(i=0;i<k;i++)        {            yy[i]=pnt[i].y;            mark_y[pnt[i].mark]=i;        }        for(i=0;i<k-1;i++)        {            for(j=0;j<k-1;j++)            {                area[i][j]=(xx[i+1]-xx[i])*(yy[j+1]-yy[j]);            }        }        printf("Case %d:\n",T++);        P=1;        while(m--)        {            mm(ok,false);            scanf("%d",&num);            while(num--)            {                scanf("%d",&res);                for(i=mark_x[res];i<mark_x[res+n];i++)//查找第k个矩形的x                {                    for(j=mark_y[res];j<mark_y[res+n];j++)//查找第k个矩形的y                    {                        ok[i][j]=true;//如果有重复覆盖的矩形块只会标记一次                    }                }            }            Area=0;            for(i=0;i<k;i++)            {                for(j=0;j<k;j++)                {                    if(ok[i][j])                        Area+=area[i][j];                }            }            printf("Query %d: %d\n",P++,Area);        }        printf("\n");    }    return 0;}

0 0