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. [2012-06-29]sed根据行号范围执行替换

    测试数据: personball@vostro:SHELL$cat aaa <instrumentation android:name="aaa" android:name= ...

  2. java值传递与引用传递实例

    public class Test2 { public static void main(String[] args) { int[] arr=new int[5]; arr[0]=10; arr[1 ...

  3. linux下操作mysql

    有关mysql数据库方面的操作,必须首先登录到mysql中. 开启MySQL服务后,使用MySQL命令可以登录.一般使用mysql -uroot -p即可.如果数据库不是本机,则需要加参数,常用参数如 ...

  4. spring mvc:日志对象logger的复用

    在采用Spring mvc+org.slf4j.Logger开发项目时,发现几乎每个controller或者manager都有的一个标配: private final static Logger LO ...

  5. [js高手之路]深入浅出webpack系列1-安装与基本打包用法和命令参数

    webpack,我想大家应该都知道或者听过,Webpack是前端一个工具,可以让各个模块进行加载,预处理,再进行打包.现代的前端开发很多环境都依赖webpack构建,比如vue官方就推荐使用webpa ...

  6. Varnish后端主机的健康状态检查

    author:JevonWei 版权声明:原创作品 配置后端主机的Health Check 环境 Varnish 192.168.198.139 图片服务端 192.168.198.120 程序服务端 ...

  7. MySQL(六)之MySQL常用操作符

    前言 在前面的MySQL学习中,我们学习了MySQL的安装,管理以及配置,还有是它的DDL.今天给大家分享一下,MySQL的操作符和它的函数这部分. 千里之行始于足下,做什么事情都要脚踏实地的去做才能 ...

  8. java常见加密方式介绍

    详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt260 本篇内容简要介绍BASE64.MD5.SHA.HMAC几种加密算法.  ...

  9. 移动端rem使用

    let $html=document.documentElement,windowW = window.innerWidth,ratio = windowW / 750if (windowW > ...

  10. Spring Cloud官方文档中文版-客户端负载均衡:Ribbon

    官方文档地址为:http://cloud.spring.io/spring-cloud-static/Dalston.SR2/#_spring_cloud_netflix 文中例子我做了一些测试在:h ...