Rectangles

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

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
 
 
没优化版:
 #include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
using namespace std;
#define INF 100000000
typedef struct point
{
int x1,y1,x2,y2;
}point;
point p[];
int ans[]={};
int n;
void dfs(int x1,int y1,int x2,int y2,int deep,int sign,int sta)
{
if( x1 >= x2 || y1 >= y2 ) return;
if(deep==n)
{
if(sta)
for(int i=;i<(<<n);i++)
{
if((i|sta)<=i)
ans[i]+=sign*(x2-x1)*(y2-y1);
}
return ;
}
dfs(x1,y1,x2,y2,deep+,sign,sta);
dfs(max(x1,p[deep].x1),max(y1,p[deep].y1),min(x2,p[deep].x2),min(y2,p[deep].y2),deep+,-sign,sta|(<<deep));
}
int main()
{
int m,i,ss,cas=,mm,x,cass;
while(scanf("%d%d",&n,&m),(n||m))
{
memset(ans,,sizeof(ans));
for(i=;i<n;i++)
scanf("%d%d%d%d",&p[i].x1,&p[i].y1,&p[i].x2,&p[i].y2);
dfs(,,INF,INF,,-,);
printf("Case %d:\n",cas++);
cass=;
while(m--)
{
scanf("%d",&mm);
ss=;
for(i=;i<mm;i++)
{
scanf("%d",&x);
ss|=(<<(x-));
}
printf("Query %d: %d\n",cass++,ans[ss]);
}
printf("\n");
}
}

优化版:

 #include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
using namespace std;
#define INF 100000000
typedef struct point
{
int x1,y1,x2,y2;
} point;
point p[];
int ans[],staa[];
int n,m;
void dfs(int x1,int y1,int x2,int y2,int deep,int sign,int sta)
{
if( x1 >= x2 || y1 >= y2 ) return;
if(deep==n)
{
if(sta)
for(int i=; i<m; i++)
{
if((staa[i]|sta)<=staa[i])
ans[staa[i]]+=sign*(x2-x1)*(y2-y1);
}
return ;
}
dfs(x1,y1,x2,y2,deep+,sign,sta);
dfs(max(x1,p[deep].x1),max(y1,p[deep].y1),min(x2,p[deep].x2),min(y2,p[deep].y2),deep+,-sign,sta|(<<deep));
}
int main()
{
int i,cas=,mm,x,cass;
while(scanf("%d%d",&n,&m),(n||m))
{
memset(ans,,sizeof(ans));
memset(staa,,sizeof(staa));
for(i=; i<n; i++)
scanf("%d%d%d%d",&p[i].x1,&p[i].y1,&p[i].x2,&p[i].y2);
printf("Case %d:\n",cas++);
cass=;
while(m--)
{
scanf("%d",&mm);
for(i=; i<mm; i++)
{
scanf("%d",&x);
staa[cass]|=(<<(x-));
}
cass++;
}
m=cass;
dfs(,,INF,INF,,-,);
for(i=; i<=cass; i++)
printf("Query %d: %d\n",i,ans[staa[i-]]);
printf("\n");
}
}

Rectangles hdu2461容斥定理的更多相关文章

  1. HDU 1796How many integers can you find(简单容斥定理)

    How many integers can you find Time Limit: 12000/5000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  2. Codeforces Round #330 (Div. 2) B. Pasha and Phone 容斥定理

    B. Pasha and Phone Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/595/pr ...

  3. hdu_5213_Lucky(莫队算法+容斥定理)

    题目连接:hdu_5213_Lucky 题意:给你n个数,一个K,m个询问,每个询问有l1,r1,l2,r2两个区间,让你选取两个数x,y,x,y的位置为xi,yi,满足l1<=xi<=r ...

  4. How Many Sets I(容斥定理)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3556 How Many Sets I Time Limit: 2 ...

  5. HDU - 4135 Co-prime 容斥定理

    题意:给定区间和n,求区间中与n互素的数的个数, . 思路:利用容斥定理求得先求得区间与n互素的数的个数,设表示区间中与n互素的数的个数, 那么区间中与n互素的数的个数等于.详细分析见求指定区间内与n ...

  6. BZoj 2301 Problem b(容斥定理+莫比乌斯反演)

    2301: [HAOI2011]Problem b Time Limit: 50 Sec  Memory Limit: 256 MB Submit: 7732  Solved: 3750 [Submi ...

  7. BZOJ2839 : 集合计数 (广义容斥定理)

    题目 一个有 \(N\) 个 元素的集合有 \(2^N\) 个不同子集(包含空集), 现在要在这 \(2^N\) 个集合中取出若干集合(至少一个), 使得它们的交集的元素个数为 \(K\) ,求取法的 ...

  8. HDU 1695 GCD 欧拉函数+容斥定理 || 莫比乌斯反演

    GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  9. HDU 4135 Co-prime 欧拉+容斥定理

    Co-prime Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

随机推荐

  1. spring mvc:@RequestParam与@ModelAttribute异同

    关于spring mvc中的两个注解:@RequestParam.@ModelAttribute区别,原先并没有特别注意,直到最近找别人开发的一个小模块的bug时,才有意识的比较了两者的区别. 1.@ ...

  2. 小而美的 React Form 组件

    背景 之间在一篇介绍过 Table 组件< React 实现一个漂亮的 Table > 的文章中讲到过,在企业级后台产品中,用的最多且复杂的组件主要包括 Table.Form.Chart, ...

  3. ASP.NET Core的身份认证框架IdentityServer4(7)- 使用客户端证书控制API访问

    前言 今天(2017-9-8,写于9.8,今天才发布)一口气连续把最后几篇IdentityServer4相关理论全部翻译完了,终于可以进入写代码的过程了,比较累.目前官方的文档和Demo以及一些相关组 ...

  4. REST:JAX-RS 与 Spring

    原文:https://developer.okta.com/blog/2017/08/09/jax-rs-vs-spring-rest-endpoints 作者:Brian Demers 译者:htt ...

  5. Java jdk 快速配置

    JAVA_HOME C:\Program Files\Java\jdk1.8.0_92   Path %JAVA_HOME%\bin;%JAVA_HOME%\jre\bin;   CLASSPATH ...

  6. display:inline-block间隙问题

    display:inline-block 是让块级元素变成行内元素 在一行显示, 然而不幸的是,它并没有得到所有浏览器的支持,比如ie6.7和古老一点的firefox完全无视它,由于firefox的老 ...

  7. Mysql 分区详解

    详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt120 一.什么是表分区通俗地讲表分区是将一大表,根据条件分割成若干个小表.m ...

  8. Centos6.6下安装Python3.5

    centos6.6自带的Python2.6,如果想要安装新版本的Python例如Python2.7+或者Python3.5,不能够用yum安装,那么只能从源码编译安装. Step 1: 安装依赖库和编 ...

  9. hdu 3342 拓扑排序 水

    好久没切题  先上水题! 拓扑排序! 代码: #include<iostream> #include<cstdio> #include<cstring> using ...

  10. poj 2063完全背包

    题意:给出总资金和投资年份 ,n个股票 给出股票价格和其一年的利润.问如何选择能获得最大利润. 思路:股票可以重复选择,完全背包问题,完全背包也是从01背包衍生而行的,其主要区别在于中间那层循环的次序 ...