您的位置:首页 > 其它

Rectangles(hdu2461)

2016-05-05 18:18 302 查看

Rectangles

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


[align=left]Problem Description[/align]
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.

[align=left]Input[/align]
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.

[align=left]Output[/align]
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.
思路:容斥原理;
感觉这题的数据有点水,按照这个复杂度O(2^n*m)是 感觉会超时的;
跑了1200多Ms。
思路很简单,就是容斥求面积。

#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<string.h>
#include<queue>
#include<stack>
#include<map>
using namespace std;
typedef struct pp
{
int x1;
int y1;
int x2;
int y2;
} ss;
ss ju[30];
int quer[1<<22];
int cnt[30];
int aa[1<<22];
int bt[30];
int cp[100005];
int ask[100005];
int tt[100];
int N;
void dfs(int k,int u);
int main(void)
{
int i,j,k,p,q;
int kk=0;
while(scanf("%d %d",&p,&q),p!=0&&q!=0)
{
kk++;
memset(ask,0,sizeof(ask));
for(i=0; i<p; i++)
{
scanf("%d %d %d %d",&ju[i].x1,&ju[i].y1,&ju[i].x2,&ju[i].y2);
}
int s;
int n;
for(s=0; s<q; s++)
{
N=0;
scanf("%d",&n);
int mn=0;
int dd;
for(i=0; i<n; i++)
{
scanf("%d",&dd);
mn|=(1<<(dd-1));
}
cp[s]=mn;
}
int flag[23];
for(i=1; i<=(1<<p)-1; i++)
{
int cn=0;
memset(flag,0,sizeof(flag));
for(j=0; j<p; j++)
{
if(i&(1<<j))
{
cn++;
flag[j]=1;
}
}
int nn;
int ak=0;
for(nn=0; nn<22; nn++)
{
if(flag[nn])
{
cnt[ak++]=nn;
}
}
int mm;
int xx1,yy1,xx2,yy2;
xx1=ju[cnt[0]].x1;
yy1=ju[cnt[0]].y1;
xx2=ju[cnt[0]].x2;
yy2=ju[cnt[0]].y2;
int uu=0;
for(nn=1; nn<ak; nn++)
{
if(xx1>=ju[cnt[nn]].x2)
{
uu=1;
aa[i]=0;
break;
}
else if(xx2<=ju[cnt[nn]].x1)
{
uu=1;
aa[i]=0;
break;
}
else if(yy1>=ju[cnt[nn]].y2)
{
uu=1;
aa[i]=0;
break;
}
else if(yy2<=ju[cnt[nn]].y1)
{
uu=1;
aa[i]=0;
break;
}
else
{
xx1=max(xx1,ju[cnt[nn]].x1);
yy1=max(yy1,ju[cnt[nn]].y1);
xx2=min(xx2,ju[cnt[nn]].x2);
yy2=min(yy2,ju[cnt[nn]].y2);
}
}
if(!uu)
{
int miji=abs(xx1-xx2)*abs(yy1-yy2);
aa[i]=miji;
for(s=0; s<q; s++)
{
int gg=cp[s]|i;
if(gg<=cp[s])
{
if(cn%2)
{
ask[s]+=aa[i];
}
else ask[s]-=aa[i];
}
}
}
}
printf("Case %d:\n",kk);
for(j=0; j<q; j++)
{
printf("Query %d: ",j+1);
printf("%d\n",ask[j]);
}
printf("\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: