您的位置:首页 > 其它

Rectangles - POJ 3695 容斥原理

2015-04-04 16:47 344 查看
Rectangles

Time Limit: 2000MSMemory Limit: 65536K
Total Submissions: 3672Accepted: 1052
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


题意:求给定的R个矩形的面积的并。

思路:容斥原理,加上奇数的矩阵的面积的交,减去偶数的矩阵的面积的交。数据比较大,容易TLE。

AC代码如下:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct rec
{
    int x1,y1,x2,y2,area;
}c[30],s[30];
int t,n,m,k,ans;
rec mul(rec a,rec b)
{
    a.x1=max(a.x1,b.x1);a.y1=max(a.y1,b.y1);
    a.x2=min(a.x2,b.x2);a.y2=min(a.y2,b.y2);
    if(a.x1>=a.x2 || a.y1>=a.y2)
      a.area=0;
    else
      a.area=(a.x2-a.x1)*(a.y2-a.y1);
    return a;
}
void get_sum(rec a,int id,int num)
{
    if(num&1)
      ans+=a.area;
    else
      ans-=a.area;
    int i,j;
    for(i=id;i<=k;i++)
    {
        rec b=mul(a,s[i]);
        if(b.area<=0)
          continue;
        get_sum(b,i+1,num+1);
    }
}
void scan( int& x )
{
	char c;
	while( c = getchar(), c < '0' || c > '9' );
	x = c - '0';
	while( c = getchar(), c >= '0' && c <= '9' ) x = x * 10 + c - '0';
}
int main()
{
    int i,j,k2;
    while(~scanf("%d%d",&n,&m) &&(n!=0 || m!=0))
    {
        for(i=1;i<=n;i++)
        {
            scan(c[i].x1);scan(c[i].y1);scan(c[i].x2);scan(c[i].y2);
            c[i].area=(c[i].x2-c[i].x1)*(c[i].y2-c[i].y1);
        }
        printf("Case %d:\n",++t);
        for(i=1;i<=m;i++)
        {
            scan(k);
            for(j=1;j<=k;j++)
            {
                scan(k2);
                s[j]=c[k2];
            }
            ans=0;
            for(j=1;j<=k;j++)
               get_sum(s[j],j+1,1);
            printf("Query %d: %d\n",i,ans);
        }
        printf("\n");
    }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: