先用好像不是线段树的方法模仿做出,,然后再做一下线段做的方法吧
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<vector>
#include<cmath>
#include<set>
#include<cstring>
using namespace std;
#define N 333
struct my
{
int i;
double num;
bool operator<(my b)
{
return num<b.num;
}
}x[N],y[N];
int n;
double ans;
int a[N],b[N];
bool map[N][N];
int main()
{
freopen("in.txt","r",stdin);
int i,j,k;
int num=0;
while (cin>>n,n)
{
num++;
k=0;
for (i=0;i<n;i++)
{
cin>>x[k].num>>y[k].num;
x[k].i=i;
y[k].i=i;
k++;
cin>>x[k].num>>y[k].num;
x[k].i=i+n;
y[k].i=i+n;
k++;
//cout<<x[i].num<<' '<<y[i].num<<endl;
}
sort(x,x+2*n);
sort(y,y+2*n);
for (i=0;i<2*n;i++)
{
a[x[i].i]=i;
b[y[i].i]=i;
}
//for (i=0;i<2*n;i++)
{
//cout<<a[i]<<' '<<b[i]<<endl;
}
ans=0;
memset(map,false,sizeof(map));
//cout<<"x id y id"<<endl;
//for (i=0;i<2*n;i++)
//cout<<x[i].num<<' '<<x[i].i<<' '<<y[i].num<<' '<<y[i].i<<endl;
for (i=0;i<n;i++)
{
int x1=a[i];
int y1=b[i];
int x2=a[i+n];
int y2=b[i+n];
//cout<<" "<<x1<<' '<<y1<<' '<<x2<<' '<<y2<<endl;
for (j=x1;j<x2;j++)
{
for (k=y1;k<y2;k++) //这里不能直接用y1累加
{
map[j][k]=true;
//cout<<x1<<' '<<y1<<endl;
}
}
//cout<<endl;
}
for (i=0;i<2*n+4;i++)
{
for (j=0;j<2*n+4;j++)
{
if (map[i][j])
{
//cout<<i<<' '<<j<<endl;
//cout<<x[i+1].num<<' '<<x[i].num<<' '<<y[j+1].num<<' '<<y[j].num<<endl;
ans+=(x[i+1].num-x[i].num)*(y[j+1].num-y[j].num);
}
}
}
printf("Test case #%d\n",num);
printf("Total explored area: %.2f\n\n",ans);
//cout<<ans<<endl;
}
return 0;
}
Atlantis
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 11634 | Accepted: 4527 |
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 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