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. 延迟实例化 Lazy<T>

    之前写的设计模式 单例模式中,推荐了使用Lazy<T>来达到线程安全和减少系统资源消耗的作用. 作用及优点: 创建某一个对象需要很大的消耗,而这个对象在运行过程中又不一定用到,为了避免每次 ...

  2. 使用Composer安装ThinkPHP5

    1.Windows 系统,下载并运行 Composer-Setup.exe. 2.安装compose.PHP采用的是wampserver集成开发环境(这也之后的问题埋下伏笔). 3.然后在命令行下面, ...

  3. [C#] .Net Core 全局配置读取管理方法 ConfigurationManager

    最近在学习.Net Core的过程中,发现.Net Framework中常用的ConfigurationManager在Core中竟然被干掉了. 也能理解.Core中使用的配置文件全是Json,不想F ...

  4. Centos7 & Docker & Jenkins & ASP.NET Core 2.0 自动化发布和部署

    写在前面 Docker一直很火热,一直想把原本的Jenkins自动部署工具搬到Docker上面,无奈今年一直忙于各种事情,迟迟未实施这个事情,正好迎来了dotnet core 2.0 的正式发布,升级 ...

  5. [UWP]使用Writeable​Bitmap创建HSV色轮

    1. HSV 1.1 HSV的定义 HSV都是一种将RGB色彩模型中的点在圆柱坐标系中的表示法,这种表示法试图做到比RGB基于笛卡尔坐标系的几何结构更加直观.HSV即色相.饱和度.明度(英语:Hue, ...

  6. 201521123081《java程序设计》 第7周学习总结

    1. 本周学习总结 以你喜欢的方式(思维导图或其他)归纳总结集合相关内容. 参考资料:XMind 2. 书面作业 Q1. ArrayList代码分析 1.1 解释ArrayList的 contains ...

  7. 201521123003《Java程序设计》第5周学习总结

    1. 本章学习总结 1.1 尝试使用思维导图总结有关多态与接口的知识点. 参考资料: 百度脑图 XMind 2. 书面作业 Q1.代码阅读:Child压缩包内源代码 1.1 com.parent包中C ...

  8. 201521123074 《Java程序设计》第4周学习总结

    1.本周学习总结 这次用XMind画的图,果然比百度脑图好用一点. 2.书面作业 Q1.注释的应用 使用类的注释与方法的注释为前面编写的类与方法进行注释,并在Eclipse中查看.(截图) 类注释 方 ...

  9. 201521044152<java程序设计>第一周学习总结

    本周学习总结 java开发时间虽然很短,但是发展迅速,已成为现在非常流行的一门语言,很开心能有幸学习java.第一周学习了java的平台,运行环境jdk以及jrt等等新名词,还了解了eclipse的基 ...

  10. Java-Properties用法-入门

    对于应用程序的配置,通常的做法是将其保存在独立的配置文件中,程序启动时加载,修改时保存.Java中Properties类就提供了这样一种机制,配置项以Key-Value的数据结构存储在文本文件中,扩展 ...