【HDU - 1542】Atlantis (线段树,扫描线)
題干:
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.
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.?
Sample Input
2 10 10 20 20 15 15 25 25.5 0Sample Output
Test case #1 Total explored area: 180.00?
解題報(bào)告:
? ?掃描線模板,裸題。tree[].f代表完全覆蓋的次數(shù)。注意到,區(qū)間更新沒有pushdown,是因?yàn)槲覀冏罱K只是使用了tree[1].val這樣一個(gè)變量。
AC代碼:
#include<bits/stdc++.h>using namespace std; const double eps = 1e-8; const int MAX = 100 + 5; double pre[MAX << 2]; //空間需要開8倍。。 int n; struct Node {double l,r,h;int f;Node(){}Node(double l,double r,double h,int f):l(l),r(r),h(h),f(f){}} seg[16*MAX]; struct TREE {int l,r;double val;int f; } tree[16*MAX]; bool cmp(const Node & a,const Node & b) {return a.h < b.h; } void pushup(int cur) {if(tree[cur].f >0) tree[cur].val = pre[tree[cur].r+1] - pre[tree[cur].l];else {if(tree[cur].l == tree[cur].r) tree[cur].val = 0;else tree[cur].val=tree[2*cur].val+tree[2*cur+1].val;} } void build(int l,int r,int cur) {tree[cur].l = l;tree[cur].r = r;tree[cur].val = 0;tree[cur].f = 0;if(l == r) return ;int m = (l + r)/2;build(l,m,2*cur);build(m+1,r,2*cur+1); } void scan(int pl,int pr,int f,int cur) {if(pl <= tree[cur].l && pr>=tree[cur].r) {tree[cur].f+=f;pushup(cur);return;}if(pl<=tree[2*cur].r) scan(pl,pr,f,2*cur);if(pr>=tree[2*cur+1].l) scan(pl,pr,f,2*cur+1);pushup(cur); } int main() {int iCase = 0;double x1,x2,y1,y2,ans;while(~scanf("%d",&n) ) {if(n == 0) break;//離散化int top = 0;for(int i = 1; i<=n; i++) {scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);seg[++top] = Node(x1,x2,y1,1); pre[top] = x1;seg[++top] = Node(x1,x2,y2,-1); pre[top] = x2;} sort(pre+1,pre+2*n+1);int x = unique(pre+1,pre+2*n+1) - pre-1; // for(int i = 1; i<=x; i++) printf("%f ",pre[i]); // printf("\n"); build(1,x,1);sort(seg+1,seg+2*n+1,cmp);ans = 0;for(int i = 1; i<2*n; i++) {//左閉右開區(qū)間int ll = lower_bound(pre+1,pre+x+1,seg[i].l) - (pre+1);int rr = lower_bound(pre+1,pre+x+1,seg[i].r) - pre-2;ll++,rr++;// printf("ll = %d rr = %d\n",ll,rr);scan(ll,rr,seg[i].f,1);ans += (seg[i+1].h - seg[i].h) * tree[1].val;// printf("ans = %lf h2=%f h1=%f val = %f\n",ans,seg[i+1].h,seg[i].h,tree[1].val);}printf("Test case #%d\n",++iCase);printf("Total explored area: %.2f\n\n",ans);}return 0 ; }?
總結(jié)
以上是生活随笔為你收集整理的【HDU - 1542】Atlantis (线段树,扫描线)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 申请信用卡填写公司电话 最好填座机号码
- 下一篇: 【POJ - 2965】The Pilo