解二: 可以把相互重叠的块都切割出来,一个小块一个小块的计算面积的总和就行了.
解一: 这个方法不是线段树的,先放这吧,用线段树做出来后再编辑,
其中的map[ ][ ]很好,,
#include<iostream>
#include<stdlib.h>
#include<math.h>
#include<stdio.h>
#include<algorithm>
#include<queue>
#include<string.h>
#include<stack>
#include<math.h>
#include<string>
#include<stdlib.h>
#include<list>
#include<vector>
using namespace std;
#define N 500
int map[N][N];
double ans[N][5];
int main()
{
int i,j,k;
int t;
int n;
double x[N*2],y[N*2];
int num=0;
while (cin>>n,n)
{
num++;
j=0;
for (i=0;i<n;i++)
{
cin>>ans[i][0]>>ans[i][1]>>ans[i][2]>>ans[i][3];
y[j]=ans[i][1];
x[j++]=ans[i][0];
y[j]=ans[i][3];
x[j++]=ans[i][2];
}
sort(x,x+2*n);
sort(y,y+2*n);
memset(map,0,sizeof(map));
for (i=0;i<n;i++)
{
int j1,j2,i1,i2;
for (i1=0;i1<2*n;i1++)
if (x[i1]==ans[i][0])
break;
for (i2=i1;i2<2*n;i2++)
if (x[i2]==ans[i][2])
break;
for (j1=0;j1<2*n;j1++)
if (y[j1]==ans[i][1])
break;
for (j2=j1;j2<2*n;j2++)
if (y[j2]==ans[i][3])
break;
//cout<<i1<<' '<<i2<<' '<<j1<<' '<<j2<<endl;
for (k=i1;k<i2;k++)
{
for (j=j1;j<j2;j++)
{
map[k][j]=1;
}
}
}
double sum=0.00;
for (i=0;i<2*n;i++)
{
for (j=0;j<2*n;j++)
sum+=(double)map[i][j]*(x[i+1]-x[i])*(y[j+1]-y[j]);
}
printf("Test case #%d\nTotal explored area: %.2f\n\n",num,sum);
}
return 1;
}
Atlantis
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 2536 Accepted Submission(s): 1199
Problem Description
There are several ancient Greek texts that contain descriptions of the fabled island Atlantis. Some of these texts even include maps of parts of the island. But unfortunately, these maps describe different regions of Atlantis. Your friend Bill has to know the total area for which maps exist. You (unwisely) volunteered to write a program that calculates this quantity.
Input
The input file consists of several test cases. Each test case starts with a line containing a single integer n (1<=n<=100) of available maps. The n following lines describe one map each. Each of these lines contains four numbers x1;y1;x2;y2 (0<=x1<x2<=100000;0<=y1<y2<=100000), not necessarily integers. The values (x1; y1) and (x2;y2) are the coordinates of the top-left resp. bottom-right corner of the mapped area.
The input file is terminated by a line containing a single 0. Don’t process it.
The input file is terminated by a line containing a single 0. Don’t process it.
Output
For each test case, your program should output one section. The first line of each section must be “Test case #k”, where k is the number of the test case (starting with 1). The second one must be “Total explored area: a”, where a is the total explored area (i.e. the area of the union of all rectangles in this test case), printed exact to two digits to the right of the decimal point.
Output a blank line after each test case.
Output a blank line after each test case.
Sample Input
2 10 10 20 20 15 15 25 25.5 0
Sample Output
Test case #1 Total explored area: 180.00
Source
Recommend
linle