码迷,mamicode.com
首页 > 其他好文 > 详细

[容斥原理] hdu 2461 Rectangles

时间:2014-05-09 15:02:18      阅读:394      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   class   code   java   

题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=2461

Rectangles

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


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
 

Source
 

Recommend
teddy   |   We have carefully selected several similar problems for you:  2462 2463 2457 2458 2456 
 

Statistic | Submit | Discuss | Note

题目意思:

给n(n<=20)个矩形,有m(m<=100000)个询问,每个询问求给定矩形的并的面积。

解题思路:

简单容斥原理

对于指定的矩形并面积,先求出所有单个矩形的和,再减去两个矩形的交,再加上三个矩形的交,以此类推。

问题就转化为求多个矩形的交,这个转化为求两个矩形的交。

代码:

//#include<CSpreadSheet.h>

#include<iostream>
#include<cmath>
#include<cstdio>
#include<sstream>
#include<cstdlib>
#include<string>
#include<string.h>
#include<cstring>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<ctime>
#include<bitset>
#include<cmath>
#define eps 1e-6
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
#define ll __int64
#define LL long long
#define lson l,m,(rt<<1)
#define rson m+1,r,(rt<<1)|1
#define M 1000000007
//#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;

#define Maxn 33

struct Rec
{
    int x1,y1,x2,y2;
    bool vv;
}rec[Maxn];

int n,m,num,pp[Maxn],ans;

Rec cal(Rec a,Rec b) //求两个矩形的交的矩形
{
    Rec res;
    if(a.x1>b.x1)
        swap(a,b);

    if(b.x1>=a.x2||b.y1>=a.y2||b.y2<=a.y1)
    {
        res.vv=false;  //说明交的面积为0
        return res;
    }
    res.vv=true; //注意要置为true
    res.x1=b.x1;
    res.y1=max(a.y1,b.y1);

    res.x2=min(a.x2,b.x2);
    res.y2=min(a.y2,b.y2);

    return res;

}

int area(Rec cur)
{
    return (cur.x2-cur.x1)*(cur.y2-cur.y1);
}

void dfs(Rec hav,int cur,int cn)
{
    if(cur>num)
        return ;

    for(int i=cur;i<=num;i++)
    {
        Rec temp=cal(hav,rec[pp[i]]);

        if(!temp.vv)  //没有公共部分
            continue;

        if(cn&1)
            ans+=area(temp); //求公共部分面积
        else
            ans-=area(temp);

        dfs(temp,i+1,cn^1);
    }
}
int main()
{
   //freopen("in.txt","r",stdin);
   //freopen("out.txt","w",stdout);

   int ca=0;

   while(scanf("%d%d",&n,&m)&&n+m)
   {
       printf("Case %d:\n",++ca);

       for(int i=1;i<=n;i++)
           scanf("%d%d%d%d",&rec[i].x1,&rec[i].y1,&rec[i].x2,&rec[i].y2);

       for(int qu=1;qu<=m;qu++)
       {
           ans=0;

           scanf("%d",&num);
           for(int i=1;i<=num;i++)
               scanf("%d",&pp[i]);

           for(int i=1;i<=num;i++)
           {
               ans+=area(rec[pp[i]]);
               dfs(rec[pp[i]],i+1,0);

           }
           printf("Query %d: %d\n",qu,ans);

       }
       putchar(‘\n‘);
   }
   return 0;
}




[容斥原理] hdu 2461 Rectangles,布布扣,bubuko.com

[容斥原理] hdu 2461 Rectangles

标签:des   style   blog   class   code   java   

原文地址:http://blog.csdn.net/cc_again/article/details/25377597

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!