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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

poj 1151(线段树求面积并)

發布時間:2025/3/16 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 poj 1151(线段树求面积并) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

解題思路:線段樹求面積并,水題


#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<vector> using namespace std;const int maxn = 1005; const double eps = 1e-8; struct Segment {int l,r,cover;double len; }tree[maxn<<2]; struct Line {int val;double x,y1,y2;Line(){}Line(double _x,double _y1,double _y2,int _val){x = _x;y1 = _y1;y2 = _y2;val = _val;} }; int n,tmpsize,size; double edge[maxn],tmp[maxn]; vector<Line> vec;void build(int rt,int l,int r) {tree[rt].l = l, tree[rt].r = r;tree[rt].cover = tree[rt].len = 0;if(l + 1 == r) return;int mid = (l + r) >> 1;build(rt<<1,l,mid);build(rt<<1|1,mid,r); }void insert(int rt,int l,int r,int val) {if(l <= tree[rt].l && tree[rt].r <= r){tree[rt].cover += val;if(tree[rt].cover == 0){if(tree[rt].l + 1 == tree[rt].r) //葉子節點,沒有被覆蓋的話長度只能為0tree[rt].len = 0;else tree[rt].len = tree[rt<<1].len + tree[rt<<1|1].len;}else tree[rt].len = edge[tree[rt].r] - edge[tree[rt].l];return;}int mid = (tree[rt].l + tree[rt].r) >> 1;if(l < mid) insert(rt<<1,l,r,val);if(mid < r) insert(rt<<1|1,l,r,val);if(tree[rt].cover == 0)tree[rt].len = tree[rt<<1].len + tree[rt<<1|1].len;else tree[rt].len = edge[tree[rt].r] - edge[tree[rt].l]; }bool cmp(Line a,Line b) {return a.x < b.x; }void DiscretData() {sort(tmp,tmp+tmpsize);size = 0;edge[++size] = tmp[0];for(int i = 1; i < tmpsize; i++)if(fabs(tmp[i] - tmp[i-1]) > eps)edge[++size] = tmp[i]; }int bisearch(double val) {int l = 0, r = size, mid;while(l <= r){mid = (l + r) >> 1;if(fabs(edge[mid] - val) < eps) return mid;else if(edge[mid] - val > eps)r = mid - 1;else l = mid + 1;} }int main() {int cas = 1;double x1,y1,x2,y2;while(scanf("%d",&n),n){tmpsize = 0;vec.clear();for(int i = 1; i <= n; i++){scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);vec.push_back(Line(x1,y1,y2,1));vec.push_back(Line(x2,y1,y2,-1));tmp[tmpsize++] = y1;tmp[tmpsize++] = y2;}DiscretData();sort(vec.begin(),vec.end(),cmp);build(1,1,size);double ans = 0;for(int i = 0; i < vec.size(); i++){if(i > 0)ans += tree[1].len * (vec[i].x - vec[i-1].x);int l = bisearch(vec[i].y1);int r = bisearch(vec[i].y2);insert(1,l,r,vec[i].val);}printf("Test case #%d\nTotal explored area: %.2f\n\n",cas++,ans);}return 0; }

總結

以上是生活随笔為你收集整理的poj 1151(线段树求面积并)的全部內容,希望文章能夠幫你解決所遇到的問題。

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