Rectangles hdu2461容斥定理
Rectangles
Time Limit: 5000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1259 Accepted Submission(s): 661
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.
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.
#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容斥定理的更多相关文章
- HDU 1796How many integers can you find(简单容斥定理)
How many integers can you find Time Limit: 12000/5000 MS (Java/Others) Memory Limit: 65536/32768 ...
- 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 ...
- hdu_5213_Lucky(莫队算法+容斥定理)
题目连接:hdu_5213_Lucky 题意:给你n个数,一个K,m个询问,每个询问有l1,r1,l2,r2两个区间,让你选取两个数x,y,x,y的位置为xi,yi,满足l1<=xi<=r ...
- How Many Sets I(容斥定理)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3556 How Many Sets I Time Limit: 2 ...
- HDU - 4135 Co-prime 容斥定理
题意:给定区间和n,求区间中与n互素的数的个数, . 思路:利用容斥定理求得先求得区间与n互素的数的个数,设表示区间中与n互素的数的个数, 那么区间中与n互素的数的个数等于.详细分析见求指定区间内与n ...
- BZoj 2301 Problem b(容斥定理+莫比乌斯反演)
2301: [HAOI2011]Problem b Time Limit: 50 Sec Memory Limit: 256 MB Submit: 7732 Solved: 3750 [Submi ...
- BZOJ2839 : 集合计数 (广义容斥定理)
题目 一个有 \(N\) 个 元素的集合有 \(2^N\) 个不同子集(包含空集), 现在要在这 \(2^N\) 个集合中取出若干集合(至少一个), 使得它们的交集的元素个数为 \(K\) ,求取法的 ...
- HDU 1695 GCD 欧拉函数+容斥定理 || 莫比乌斯反演
GCD Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- HDU 4135 Co-prime 欧拉+容斥定理
Co-prime Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
随机推荐
- 运用jQuery写的验证表单
//运用jQuery写的验证表单 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "h ...
- 使用jmeter执行多条sql语句
注意2个地方 1. [JDBC Connection Configuration] 在配置DataBase URL的时候,加上allowMultiQueries=true参数如: jdbc:MySQL ...
- epoll全面讲解:从实现到应用
多路复用的适用场合 • 当客户处理多个描述符时(例如同时处理交互式输入和网络套接口),必须使用I/O复用. • 如果一个TCP服务器既要处理监听套接口,又要处理已连接套接口,一般也要用 ...
- 9-9害死人不偿命的(3n+1)猜想
1001. 害死人不偿命的(3n+1)猜想 (15) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 卡拉兹(Ca ...
- ActiveMQ笔记——技术点汇总
目录 · Introduction to ActiveMQ · Installing ActiveMQ · Message-oriented middleware · JMS specificatio ...
- C# 获取exe、dll中的图标,支持获取256x256分辨率
在网上找过许多文章,都没有成功获取过大图标,只能获取最大32x32.最后自己尝试了相关的windows api,终于找到一个可用的. 主要用到的C++的PrivateExtractIcons函数,具体 ...
- Vuex 最简单的数量增减实例
Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式.它采用集中式存储管理应用的所有组件的状态,并以相应的规则保证状态以一种可预测的方式发生变化.Vuex 也集成到 Vue 的官方调试工具 ...
- 从聚合数据请求菜谱大全接口数据,解析显示到ListView
- 聊聊click延迟和点击穿透
博客原文地址:Claiyre的个人博客 https://claiyre.github.io/ 如需转载,请在文章开头注明原文地址 移动端click事件被延迟 移动端的开发经常需要监听用户的双击行为,所 ...
- ★浅谈Spanking情节