#include<iostream>
#include<cstring>
#include<string>
#include<queue>
#include<map>
using namespace std;
struct rec {
int x1, y1, x2, y2, c;
} recs[1001], t1[1000001], t2[1000001];
int num1, num2;
void add(int x1, int y1, int x2, int y2, int c) {
t2[num2].x1 = x1;
t2[num2].x2 = x2;
t2[num2].y1 = y1;
t2[num2].y2 = y2;
t2[num2++].c = c;
}
void modify(int k) {
int i;
int x1 = recs[k].x1;
int x2 = recs[k].x2;
int y1 = recs[k].y1;
int y2 = recs[k].y2;
for (i = 0; i < num1; i++) {
int cx1 = t1[i].x1;
int cx2 = t1[i].x2;
int cy1 = t1[i].y1;
int cy2 = t1[i].y2;
if (cx1 > x2 || cx2 < x1 || cy1 > y2 || cy2 < y1) {
t2[num2++] = t1[i];
continue;
}
if (cx1 < x1)add(cx1, cy1, x1, cy2, t1[i].c);
if (cx2 > x2)add(x2, cy1, cx2, cy2, t1[i].c);
t1[i].x1 = max(t1[i].x1, x1);
t1[i].x2 = min(t1[i].x2, x2);
if (t1[i].y1 < y1)add(t1[i].x1, t1[i].y1, t1[i].x2, y1, t1[i].c);
if (t1[i].y2 > y2)add(t1[i].x1, y2, t1[i].x2, t1[i].y2, t1[i].c);
}
for (i = 0; i < num2; i++) {
t1[i] = t2[i];
}
t1[i] = recs[k];
num1 = num2 + 1;
num2 = 0;
}
void solve(int n) {
num1 = num2 = 0;
for (int i = 0; i < n; i++) {
modify(i);
}
}
int main() {
int n, c, i, x1, x2, y1, y2, ans,coun,c1;
coun=0;
while (scanf("%d%d", &n, &c) && n) {
for (i = 0; i < n; i++) {
scanf("%d%d%d%d%d", &c1, &x1, &y1, &x2, &y2);
if (x1 > x2)swap(x1, x2);
if (y1 > y2)swap(y1, y2);
recs[i].x1 = x1;
recs[i].x2 = x2;
recs[i].y1 = y1;
recs[i].y2 = y2;
recs[i].c = c1;
}
solve(n);
ans = 0;
for (i = 0; i < num1; i++) {
if (t1[i].c == c) {
ans+=(t1[i].x2-t1[i].x1)*(t1[i].y2-t1[i].y1);
}
}
printf("The area of color %d in dataset %d is: %d\n",c,++coun,ans);
}
}
zoj 1752 Rectangular Rectitude
最新推荐文章于 2024-10-31 18:26:46 发布