POJ3695-Rectangles

Rectangles
Time Limit: 2000MS   Memory Limit: 65536K
Total 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  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
//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;
}

發佈了243 篇原創文章 · 獲贊 12 · 訪問量 20萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章