POJ 1151 Atlantis 矩形面积求交/线段树扫描线
Atlantis
Time Limit: 1 Sec Memory Limit: 256 MB
题目连接
http://poj.org/problem?id=1151Description
here 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.
1000000000.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
10 10 20 20
15 15 25 25.5
0
Sample Output
Test case #1
Total explored area: 180.00
Total explored area: 180.00
HINT
题意
给你N个矩形,求矩形相交的面积题解:
具体请看这一篇博文:http://blog.csdn.net/shiqi_614/article/details/6821814
代码:
//qscqesze #include <cstdio> #include <cmath> #include <cstring> #include <ctime> #include <iostream> #include <algorithm> #include <set> #include <vector> #include <sstream> #include <queue> #include <typeinfo> #include <fstream> #include <map> #include <stack> typedef long long ll; using namespace std; //freopen("D.in","r",stdin); //freopen("D.out","w",stdout); #define sspeed ios_base::sync_with_stdio(0);cin.tie(0) #define maxn 20001 #define mod 10007 #define eps 1e-9 //const int inf=0x7fffffff; //无限大 const int inf=0x3f3f3f3f; /* int buf[10]; inline void write(int i) { int p = 0;if(i == 0) p++; else while(i) {buf[p++] = i % 10;i /= 10;} for(int j = p-1; j >=0; j--) putchar('0' + buf[j]); printf("\n"); } */ //************************************************************************************** inline ll read() { int x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } struct Line { int flag; double x,y1,y2; Line(double a,double b,double c,int d) {x=a;y1=b;y2=c;flag=d;} bool operator<(const Line &b)const {return x<b.x;} }; struct node { int l,r,flag; double ll,rr,len; void fun(int val) { flag+=val; if(flag==0) len=0; else len=rr-ll; } }; vector<double> y; vector<Line> line; map<double,int>H; node tree[maxn*4]; void build(int l,int r,int x) { tree[x].l=l,tree[x].r=r; tree[x].flag=0,tree[x].len=0; tree[x].ll=y[l],tree[x].rr=y[r]; if(l+1!=r) { int mid=(l+r)>>1; build(l,mid,x<<1); build(mid,r,x<<1|1); } } void updata(int st,int ed,int x,int val) { int l=tree[x].l,r=tree[x].r; if(l+1==r)tree[x].fun(val); else { int mid=(l+r)>>1; if(st<mid)updata(st,ed,x<<1,val); if(ed>mid)updata(st,ed,x<<1|1,val); tree[x].len=tree[x<<1].len+tree[x<<1|1].len; } } int main() { int n,cas=1; while(scanf("%d",&n)!=EOF) { if(n==0) break; line.clear(),y.clear(),H.clear(); memset(tree,0,sizeof(tree)); double x1,y1,x2,y2; for(int i=1;i<=n;i++) { scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2); line.push_back((Line){x1,y1,y2,1}); line.push_back((Line){x2,y1,y2,-1}); y.push_back(y1),y.push_back(y2); } sort(line.begin(),line.end()); sort(y.begin(),y.end()); y.erase(unique(y.begin(),y.end()),y.end()); for(int i=0;i<(int)y.size();i++) H[y[i]]=i; build(0,y.size()-1,1); double ans=0; for(int i=0;i<line.size();i++) { if(i!=0)ans+=(line[i].x-line[i-1].x)*tree[1].len; updata(H[line[i].y1],H[line[i].y2],1,line[i].flag); } printf("Test case #%d\nTotal explored area: %.2f\n\n",cas++,ans); } }