hdu2461 Rectangles-----容斥

Rectangles

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


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

题意:给n个矩形,然后每次涂m个矩形,问涂上色的面积是多少。

利用容斥原理即可。

#include<iostream>
#include<cstdlib>
#include<stdio.h>
#include<algorithm>
using namespace std;
int n,m,num,res;
int a[25];
struct Node
{
    int x1,y1,x2,y2;
    int s;
}node[25];
bool check(Node x,int u)
{
    if(node[u].x1>=x.x2) return 0;
    if(x.x1>=node[u].x2) return 0;
    if(node[u].y1>=x.y2) return 0;
    if(x.y1>=node[u].y2) return 0;
    return 1;
}
void solve(Node rec,int id,int k)
{
   Node temp;
   if(id==num+1) return ;
   for(int i=id;i<=num;i++)
   {
       if(check(rec,a[i]))
       {
         temp.x1=rec.x1>node[a[i]].x1?rec.x1:node[a[i]].x1;
         temp.y1=rec.y1>node[a[i]].y1?rec.y1:node[a[i]].y1;
         temp.x2=rec.x2<node[a[i]].x2?rec.x2:node[a[i]].x2;
         temp.y2=rec.y2<node[a[i]].y2?rec.y2:node[a[i]].y2;
         temp.s=(temp.x2-temp.x1)*(temp.y2-temp.y1);
         if(k%2) res+=temp.s;
         else res-=temp.s;
         solve(temp,i+1,k+1);
       }
   }
}
int main()
{
    int count=1;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        if(n==0&&m==0) break;
        for(int i=1;i<=n;i++)
        {
            scanf("%d%d%d%d",&node[i].x1,&node[i].y1,&node[i].x2,&node[i].y2);
            node[i].s=(node[i].x2-node[i].x1)*(node[i].y2-node[i].y1);
        }
        printf("Case %d:\n",count++);
        for(int i=1;i<=m;i++)
        {
            scanf("%d",&num);
            res=0;
            for(int j=1;j<=num;j++)
            {
                scanf("%d",&a[j]);
                res+=node[a[j]].s;
            }
            for(int j=1;j<=num;j++)
            {
                solve(node[a[j]],j+1,0);
            }
            printf("Query %d: ",i);
            printf("%d\n",res);
        }
        puts("");
    }
    return 0;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值