Rectangles
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 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). Sample Input 2 2 Sample Output Case 1: Source |
题意:给你一些矩形,编号为1~n。有一些询问,每次问你其中的一些矩形的面积并。
题解:
矩形切割
矩形切割裸题。。。当然也可以用线段树(大坑。。。)
直接挂个模版就过了。
看到Discuss中有人说矩形切割会TLE。可能是常数太大了吧。但我没有TLE呀,而且貌似跑的飞快(时间好像排32名,内存也很小。。。)
#include<bits/stdc++.h>
using namespace std;
struct node
{
int X1,Y1,X2,Y2;
}R[];
int s1,cc[],ans[];
int read()
{
int s=,fh=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')fh=-;ch=getchar();}
while(ch>=''&&ch<=''){s=s*+(ch-'');ch=getchar();}
return s*fh;
}
void Cover(int X1,int Y1,int X2,int Y2,int k,int k1)
{
while(k<=s1&&(X1>=R[cc[k]].X2||X2<=R[cc[k]].X1||Y1>=R[cc[k]].Y2||Y2<=R[cc[k]].Y1))k++;
if(k>=s1+){ans[k1]+=(X2-X1)*(Y2-Y1);return;}
if(X1<R[cc[k]].X1){Cover(X1,Y1,R[cc[k]].X1,Y2,k+,k1);X1=R[cc[k]].X1;}
if(X2>R[cc[k]].X2){Cover(R[cc[k]].X2,Y1,X2,Y2,k+,k1);X2=R[cc[k]].X2;}
if(Y1<R[cc[k]].Y1){Cover(X1,Y1,X2,R[cc[k]].Y1,k+,k1);Y1=R[cc[k]].Y1;}
if(Y2>R[cc[k]].Y2){Cover(X1,R[cc[k]].Y2,X2,Y2,k+,k1);Y2=R[cc[k]].Y2;}
}
int main()
{
int n,m,i,case1=,C=,j,sum;
while()
{
n=read();m=read();if(n==&&m==)break;
for(i=;i<=n;i++){R[i].X1=read();R[i].Y1=read();R[i].X2=read();R[i].Y2=read();}
printf("Case %d:\n",++case1);
C=;
for(i=;i<=m;i++)
{
s1=read();
for(j=;j<=s1;j++)cc[j]=read();
for(j=s1;j>=;j--)Cover(R[cc[j]].X1,R[cc[j]].Y1,R[cc[j]].X2,R[cc[j]].Y2,j+,j);//矩形切割法
sum=;
for(j=;j<=s1;j++){sum+=ans[j];ans[j]=;}
printf("Query %d: %d\n",++C,sum);
}
printf("\n");
}
fclose(stdin);
fclose(stdout);
return ;
}