POJ 3695 Rectangles (矩形并 状压+容斥定理 好题)

Rectangles

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 3730   Accepted: 1082

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
X
1,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<=RN), 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

2008 Asia Hefei Regional Contest Online by USTC

题目链接:http://poj.org/problem?id=3695

题目大意:给出n个矩形,用它们的对顶点表示,m个查询,每个查询给出r个数,求这r个矩形并在一起的图形面积

题目分析:本题有个特点就是n很小但是m很大,因此我们可以把询问的状态用二进制压缩,然后直接计算出这些询问的状态,O(1)查询即可,计算每种状态时,在DFS中容斥,如三个矩形两两相交,设面积为A,B,C则A∪B∪C = A + B + C - A∩B - A∩C - B∩C + A∩B∩C,交的时候可以通过对顶角的坐标来计算相交部分的面积,至于容斥时的符号,在DFS里用一个参数sgin表示,每一次交完变一下号即可,对于每个矩形(包括并出来的小矩形)分别搜索选和不选两种状态

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int const INF = 0x3fffffff;
int const MAX = 1 << 21;
int q[MAX], ans[MAX];
int n, m;

struct R
{
    int x1, y1;
    int x2, y2;
}r[25];

void DFS(int x1, int y1, int x2, int y2, int pos, int sgin, int sta)
{
    if(x1 >= x2 || y1 >= y2)
        return;
    if(pos == n + 1)
    {
        if(sta)
            for(int i = 1; i <= m; i++)
                if((q[i] & sta) == sta)
                    ans[q[i]] += sgin * (x2 - x1) * (y2 - y1);
        return;
    }
    int xx1 = max(x1, r[pos + 1].x1);
    int yy1 = max(y1, r[pos + 1].y1);
    int xx2 = min(x2, r[pos + 1].x2);
    int yy2 = min(y2, r[pos + 1].y2);
    DFS(x1, y1, x2, y2, pos + 1, sgin, sta);
    DFS(xx1, yy1, xx2, yy2, pos + 1, -sgin, sta | (1 << pos));
    return;
}

int main()
{
    int ca = 1;
    while(scanf("%d %d", &n, &m) != EOF && (n + m))
    {
        memset(q, 0, sizeof(q));
        memset(ans, 0, sizeof(ans));
        printf("Case %d:\n", ca++);
        for(int i = 1; i <= n; i++)
            scanf("%d %d %d %d", &r[i].x1, &r[i].y1, &r[i].x2, &r[i].y2);
        for(int i = 1; i <= m; i++)
        {
            int num;
            scanf("%d", &num);
            while(num --)
            {
                int tmp;
                scanf("%d", &tmp);
                q[i] |= (1 << (tmp - 1));
            }
        }
        DFS(0, 0, INF, INF, 0, -1, 0);
        for(int i = 1; i <= m; i++)
            printf("Query %d: %d\n", i, ans[q[i]]);
        printf("\n");
    }
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-06 03:38:54

POJ 3695 Rectangles (矩形并 状压+容斥定理 好题)的相关文章

poj 2411 Mondriaan&#39;s Dream 状压dp入门

题意: 求h*w的矩形被1*2的小矩形覆盖的方案数. 分析: 状压dp入门,<挑战程序设计竞赛>上讲的很好,好几天才看懂. 代码: #include <iostream> using namespace std; __int64 ans[16][16]; int n,m; __int64 dp[2][1<<16]; __int64 solve() { int i,j,used; memset(dp,0,sizeof(dp)); __int64 *crt=dp[0],*n

POJ 2411 Mondriaan&#39;s Dream (状压DP)

题意:给出一个n*m的棋盘,及一个小的矩形1*2,问用这个小的矩形将这个大的棋盘覆盖有多少种方法. 析:对第(i,j)位置,要么不放,要么竖着放,要么横着放,如果竖着放,我们记第 (i,j)位置为0,(i+1,j)为1,如果横着放,那么我们记 (i,j),(i,j+1)都为1,然后dp[i][s]表示 第 i 行状态为 s 时,有多少方法,那么我们就可以考虑与和匹配的状态,这个也很容易判断. 代码如下: #pragma comment(linker, "/STACK:1024000000,102

POJ 2411 Mondriaan&#39;s Dream (状压+dfs)

Language: Default Mondriaan's Dream Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 12405   Accepted: 7239 Description Squares and rectangles fascinated the famous Dutch painter Piet Mondriaan. One night, after producing the drawings in

POJ 2411 Mondriaan&#39;s Dream(状压DP)

http://poj.org/problem?id=2411 求一个n*m矩阵用1*2方块去填满的情况有几种 思路:状压dp,先预处理那些状态之间能互相到达,情况就几种,上一个两个1,下一个状态也两个1,上一个为0,下一个必须为1,还有一种是上一个为1,下一个为0的情况 然后就一层层往后递推即可 代码: #include <stdio.h> #include <string.h> #include <algorithm> using namespace std; int

POJ 2411 Mondriaan&#39;s Dream ——状压DP 插头DP

[题目分析] 用1*2的牌铺满n*m的格子. 刚开始用到动规想写一个n*m*2^m,写了半天才知道会有重复的情况. So Sad. 然后想到数据范围这么小,爆搜好了.于是把每一种状态对应的转移都搜了出来. 加了点优(gou)化(pi),然后poj上1244ms垫底. 大概的方法就是考虑每一层横着放的情况,剩下的必须竖起来的情况到下一层取反即可. 然后看了 <插头DP-从入门到跳楼> 这篇博客,怒抄插头DP 然后16ms了,自己慢慢YY了一下,写出了风(gou)流(pi)倜(bu)傥(tong)

POJ 1185 炮兵阵地(状压DP入门)

http://poj.org/problem?id=1185 状压DP: 1 #include<iostream> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 6 int dp[105][100][100]; 7 int ma[105],st[105]; 8 9 int ok(int x) 10 { 11 return (x&(x<<1))+(x&(x&

poj 1085 Triangle War (状压+记忆化搜索)

Triangle War Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2685   Accepted: 1061 Description Triangle War is a two-player game played on the following triangular grid: Two players, A and B, take turns filling in any dotted line connect

POJ 3254 Corn Fields 【状压DP】

[题目大意]一个矩阵里有很多格子,每个格子有两种状态,可以放牧和不可以放牧,可以放牧用1表示,否则用0表示,在这块牧场放牛,要求两个相邻的方格不能同时放牛,即牛与牛不能相邻.问有多少种放牛方案(一头牛都不放也是一种方案) [解析]根据题意,把每一行的状态用二进制的数表示,0代表不在这块放牛,1表示在这一块放牛.首先很容易看到,每一行的状态要符合牧场的硬件条件,即牛必须放在能放牧的方格上.这样就能排除一些状态.另外,牛与牛之间不能相邻,这样就要求每一行中不能存在两个相邻的1,这样也能排除很多状态.

POJ 3254 Corn Fields //入门状压dp

Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7578   Accepted: 4045 Description Farmer John has purchased a lush new rectangular pasture composed of M by N (1 ≤ M ≤ 12; 1 ≤ N ≤ 12) square parcels. He wants to grow some yumm