POJ 1151 Atlantis 矩形面积求交/线段树扫描线
Atlantis
题目连接
http://poj.org/problem?id=1151
Description
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
有一个很坑的地方是 printf("Total explored area: %.2f\n\n", ans);
我一开始写的printf("Total explored area: %.2lf\n\n", ans);wa了好久。。。我也不知道为什么,如果有大佬知道欢迎指教。
有一天 OYJY大佬告诉我这是因为POJ评测机有小脾气。
代码:
1 //#include<bits/stdc++.h> 2 #include<cstdio> 3 #include<iostream> 4 #include<algorithm> 5 using namespace std; 6 #define N 10050 7 int n,cnt,tot,cas; 8 double kth[N]; 9 struct Query 10 { 11 double l,r,h; int id; 12 bool operator <(const Query&b)const 13 {return h<b.h;} 14 }que[N<<1]; 15 struct Tree{int l,r,lazy;double sum;}tr[N<<2]; 16 template<typename T>void read(T&x) 17 { 18 int k=0;char c=getchar(); 19 x=0; 20 while(!isdigit(c)&&c!=EOF)k^=c=='-',c=getchar(); 21 if(c==EOF)exit(0); 22 while(isdigit(c))x=x*10+c-'0',c=getchar(); 23 x=k?-x:x; 24 } 25 void push_up(int x) 26 { 27 double len=(kth[tr[x].r]-kth[tr[x].l]); 28 tr[x].sum=0; 29 if (tr[x].lazy>0)tr[x].sum=len; 30 else if(tr[x].r-tr[x].l>1)tr[x].sum=tr[x<<1].sum+tr[x<<1|1].sum; 31 } 32 void bt(int x,int l,int r) 33 { 34 tr[x]=Tree{l,r,0,0}; 35 if(r-l==1)return; 36 int mid=(l+r)>>1; 37 bt(x<<1,l,mid); 38 bt(x<<1|1,mid,r); 39 } 40 void update(int x,int l,int r,int tt) 41 { 42 if (l<=tr[x].l&&tr[x].r<=r) 43 { 44 tr[x].lazy+=tt; 45 push_up(x); 46 return; 47 } 48 int mid=(tr[x].l+tr[x].r)>>1; 49 if(l<mid)update(x<<1,l,r,tt); 50 if(mid<r)update(x<<1|1,l,r,tt); 51 push_up(x); 52 } 53 double query(int x,int l,int r) 54 { 55 if (l<=tr[x].l&&tr[x].r<=r) return tr[x].sum; 56 int mid=(tr[x].l+tr[x].r)>>1; 57 double ans=0; 58 if(l<mid)ans+=query(x<<1,l,r); 59 if(mid<r)ans+=query(x<<1|1,l,r); 60 return ans; 61 } 62 void input() 63 { 64 read(n); 65 if (n==0)exit(0); 66 double x1,y1,x2,y2; 67 for(int i=1;i<=n;i++) 68 { 69 //read(x1);read(y1); read(x2);read(y2); 70 scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2); 71 que[++tot]=Query{x1,x2,y1,1}; 72 que[++tot]=Query{x1,x2,y2,-1}; 73 kth[++cnt]=x1;kth[++cnt]=y1; 74 kth[++cnt]=x2;kth[++cnt]=y2; 75 } 76 } 77 void work() 78 { 79 double ans=0; 80 sort(que+1,que+tot+1); 81 sort(kth+1,kth+cnt+1); 82 cnt=unique(kth+1,kth+cnt+1)-kth-1; 83 bt(1,1,cnt); 84 for(int i=1;i<=tot-1;i++) 85 { 86 int l=lower_bound(kth+1,kth+cnt+1,que[i].l)-kth; 87 int r=lower_bound(kth+1,kth+cnt+1,que[i].r)-kth; 88 update(1,l,r,que[i].id); 89 ans+=tr[1].sum*(que[i+1].h-que[i].h); 90 } 91 //printf("Test case #%d\nTotal explored area: %.2lf\n\n",++cas,ans); 92 printf("Test case #%d\n", ++cas); 93 printf("Total explored area: %.2f\n\n", ans); 94 } 95 void clear(){cnt=0;tot=0;} 96 int main() 97 { 98 #ifndef ONLINE_JUDGE 99 freopen("aa.in","r",stdin); 100 #endif 101 while(1) 102 { 103 clear(); 104 input(); 105 work(); 106 } 107 }