[容斥原理] hdu 2461 Rectangles
来源:互联网 发布:平面设计的软件 编辑:程序博客网 时间:2024/05/23 15:06
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=2461
Rectangles
Time Limit: 5000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1268 Accepted Submission(s): 665
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.
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.
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 20 0 2 21 1 3 31 12 1 22 10 1 1 22 1 3 22 1 20 0
Sample Output
Case 1:Query 1: 4Query 2: 7Case 2:Query 1: 2
Source
2008 Asia Hefei Regional Contest Online by USTC
Recommend
teddy | We have carefully selected several similar problems for you: 2462 2463 2457 2458 2456
题目意思:
给n(n<=20)个矩形,有m(m<=100000)个询问,每个询问求给定矩形的并的面积。
解题思路:
简单容斥原理
对于指定的矩形并面积,先求出所有单个矩形的和,再减去两个矩形的交,再加上三个矩形的交,以此类推。
问题就转化为求多个矩形的交,这个转化为求两个矩形的交。
代码:
//#include<CSpreadSheet.h>#include<iostream>#include<cmath>#include<cstdio>#include<sstream>#include<cstdlib>#include<string>#include<string.h>#include<cstring>#include<algorithm>#include<vector>#include<map>#include<set>#include<stack>#include<list>#include<queue>#include<ctime>#include<bitset>#include<cmath>#define eps 1e-6#define INF 0x3f3f3f3f#define PI acos(-1.0)#define ll __int64#define LL long long#define lson l,m,(rt<<1)#define rson m+1,r,(rt<<1)|1#define M 1000000007//#pragma comment(linker, "/STACK:1024000000,1024000000")using namespace std;#define Maxn 33struct Rec{ int x1,y1,x2,y2; bool vv;}rec[Maxn];int n,m,num,pp[Maxn],ans;Rec cal(Rec a,Rec b) //求两个矩形的交的矩形{ Rec res; if(a.x1>b.x1) swap(a,b); if(b.x1>=a.x2||b.y1>=a.y2||b.y2<=a.y1) { res.vv=false; //说明交的面积为0 return res; } res.vv=true; //注意要置为true res.x1=b.x1; res.y1=max(a.y1,b.y1); res.x2=min(a.x2,b.x2); res.y2=min(a.y2,b.y2); return res;}int area(Rec cur){ return (cur.x2-cur.x1)*(cur.y2-cur.y1);}void dfs(Rec hav,int cur,int cn){ if(cur>num) return ; for(int i=cur;i<=num;i++) { Rec temp=cal(hav,rec[pp[i]]); if(!temp.vv) //没有公共部分 continue; if(cn&1) ans+=area(temp); //求公共部分面积 else ans-=area(temp); dfs(temp,i+1,cn^1); }}int main(){ //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); int ca=0; while(scanf("%d%d",&n,&m)&&n+m) { printf("Case %d:\n",++ca); for(int i=1;i<=n;i++) scanf("%d%d%d%d",&rec[i].x1,&rec[i].y1,&rec[i].x2,&rec[i].y2); for(int qu=1;qu<=m;qu++) { ans=0; scanf("%d",&num); for(int i=1;i<=num;i++) scanf("%d",&pp[i]); for(int i=1;i<=num;i++) { ans+=area(rec[pp[i]]); dfs(rec[pp[i]],i+1,0); } printf("Query %d: %d\n",qu,ans); } putchar('\n'); } return 0;}
0 0
- HDU 2461 Rectangles 容斥原理
- [容斥原理] hdu 2461 Rectangles
- HDU-2461 Rectangles(容斥原理)
- hdu-2461-Rectangles(容斥)
- Rectangles - POJ 3695 容斥原理
- poj 3695 Rectangles(容斥原理)
- hdu2461 Rectangles-----容斥
- hdu 2461 容斥原理 Rectangle
- 【容斥原理】HDU 4135
- hdu 4135 容斥原理
- hdu 4336 容斥原理
- hdu 1695 容斥原理
- hdu 4407 容斥原理
- HDU 1796 容斥原理
- hdu 1796 容斥原理
- hdu 4135 (容斥原理)
- HDU 1695 容斥原理
- HDU 3929 容斥原理
- JS中的onload与jQuery中的ready区别
- Substring——细节处见功底
- phpcms v9 成功安装后无法登陆后台问题与解决
- Java获取工程路径(包括非web工程和web工程)
- 关于console.read的小问题
- [容斥原理] hdu 2461 Rectangles
- UITableViewCell点击时背景颜色
- Java Reflection 反射机制_学习
- uva331
- 关于FFT的相位谱(转)--关于正弦信号FFT之后相位-90
- VirtualBox Host Only网络配置使用心得
- eclipse主题设置--即编码区呈现各种颜色
- StringCollection,Array,ArrayList,IList,Item,Queue,Stack,Hashtable,SortedList
- JS获取各种高度宽度、浏览器窗口滚动条的位置、元素的几何尺寸