日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【HDU - 1542】Atlantis (线段树,扫描线)

發布時間:2023/12/10 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【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 0

Sample Output

Test case #1 Total explored area: 180.00

?

解題報告:

? ?掃描線模板,裸題。tree[].f代表完全覆蓋的次數。注意到,區間更新沒有pushdown,是因為我們最終只是使用了tree[1].val這樣一個變量。

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++) {//左閉右開區間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 ; }

?

總結

以上是生活随笔為你收集整理的【HDU - 1542】Atlantis (线段树,扫描线)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。