USACO 3.1.4
USACO_3.1.4Shaping Regions形成的区域
Time Limit:1000MS Memory Limit:65536K
Total Submit:7 Accepted:2
Description
N个不同的颜色的不透明的长方形(1 <= N <= 1000)被放置在一张宽为A长为B的白纸上。这些长方形被放置时,保证了它们的边与白纸的边缘平行。所有的长方形都放置在白纸内,所以我们会看到不同形状的各种颜色。坐标系统的原点(0,0)设在这张白纸的左下角,而坐标轴则平行于边缘。
Input
按顺序输入放置长方形的方法。第一行输入的是那个放在底的长方形(即白纸)。
第 1 行: A , B 和 N, 由空格分开 (1 <=A, B<=10,000)
第 2 到N+1行: 为五个整数 llx, lly, urx, ury, color 这是一个长方形的左下角坐标,右上角坐标和颜色。
颜色 1和底部白纸的颜色相同。 (1 <= color <= 2500)
Output
输出文件应该包含一个所有能被看到颜色连同该颜色的总面积的清单( 即使颜色的区域不是连续的),按color增序排列。
不要显示没有出现过的颜色。
Sample Input
20 20 3
2 2 18 18 2
0 8 19 19 3
8 0 10 19 4
Sample Output
1 91
2 84
3 187
4 38
娘的。用面积树做了两三天都没做出来。WA90,尝试了各种离散化方法。。最后花了15分钟打了一个矩形切割就过了。。原来矩形切割不仅仅能求并。
/*
ID: wuyihao1
PROG: rect1
LANG: C++
*/
#include <cstdio>
long now;
long n;
long x1[1010];
long x2[1010];
long y1[1010];
long y2[1010];
long c[1010];
long ans[2510];
void cut(long fx1,long fx2,long fy1,long fy2,long i)
{
while (i<n+1 && (fx1>x2[i]||fx2<x1[i]||fy1>y2[i]||fy2<y1[i]))i++;
if (i==n+1) {ans[now]+=(fy2-fy1)*(fx2-fx1);return;}
if (x1[i]>=fx1) {cut(fx1,x1[i],fy1,fy2,i+1);fx1=x1[i];}
if (x2[i]<=fx2) {cut(x2[i],fx2,fy1,fy2,i+1);fx2=x2[i];}
if (y1[i]>=fy1) {cut(fx1,fx2,fy1,y1[i],i+1);}//fx1=x2[i]-1;}
if (y2[i]<=fy2) {cut(fx1,fx2,y2[i],fy2,i+1);}//fx2=x1[i]+1;}
}
int main()
{
freopen("rect1.in","r",stdin);
freopen("rect1.out","w",stdout);
scanf("%ld%ld%ld",x2+1,y2+1,&n);
x1[1] = 0;y1[1] = 0;c[1] = 1;n ++;
long maxc = 0;
for (long i=2;i<n+2;i++)
{
scanf("%ld%ld%ld%ld%ld",x1+i,y1+i,x2+i,y2+i,c+i);
maxc = maxc>c[i]?maxc:c[i];
}
for (long i=n;i>0;i--)
{
now = c[i];
cut(x1[i],x2[i],y1[i],y2[i],i+1);
}
for (long i=1;i<maxc+1;i++)
{
if (ans[i])
printf("%ld %ld\n",i,ans[i]);
}
return 0;
}