HDU 1542 Atlantis (線段樹求矩陣覆蓋面積)

來源:互聯網
上載者:User

標籤:style   blog   color   2014   os   表   


題意:給你n個矩陣求覆蓋面積。

思路:看了別人的結題報告

給定一個矩形的左下角座標和右上方座標分別為:(x1,y1)、(x2,y2),對這樣的一個矩形,我們構造兩條線段,一條定位在x1,它在y座標的區間是[y1,y2],並且給定一個cover域值為1;另一條線段定位在x2,區間一樣是[y1,y2],給定它一個cover值為-1。根據這樣的方法對每個矩形都構造兩個線段,最後將所有的線段根據所定位的x從左至右進行排序


#include <iostream>#include <stdio.h>#include <string.h>#include <algorithm>using namespace std;#define M 100#define inf 0x3fffffff#define maxn 500000*2struct seg{int flag;double up,down,x;}line[M*5];int cmp(seg a,seg b){return a.x<b.x;}struct Tree{double up,down,x;int cover;bool flag;}tree[M*M];double y[M*3];//對y進行分割,建樹void build(int id,int l,int r){tree[id].down=y[l];tree[id].up=y[r];tree[id].flag=false;tree[id].cover=0;tree[id].x=-1;if(l+1==r){tree[id].flag=true;return ;}int mid=(l+r)/2;build(id*2,l,mid);build(id*2+1,mid,r);}double insert(int id,double x,double l,double r,int flag)//flag表示為左邊還是右邊{if(tree[id].down>=r||tree[id].up<=l) return 0;if(tree[id].flag){if(tree[id].cover>0)//遞迴到了葉子節點{double temp_x=tree[id].x;double ans=(x-temp_x)*(tree[id].up-tree[id].down);tree[id].x=x;//定位上一次的xtree[id].cover+=flag;return ans;}else {tree[id].cover+=flag;tree[id].x=x;return 0;}}double ans1,ans2;ans1=insert(id*2,x,l,r,flag);ans2=insert(id*2+1,x,l,r,flag);return ans1+ans2;}int main(){int t,ca=1;while(scanf("%d",&t)&&t){double x1,x2,y1,y2;int k=0;for(int i=0;i<t;i++){scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);k++;y[k]=y1;line[k].down=y1;line[k].up=y2;line[k].x=x1;line[k].flag=1;//1表示左邊k++;y[k]=y2;line[k].x=x2;line[k].down=y1;line[k].up=y2;line[k].flag=-1;//-1表示右邊}sort(y+1,y+k+1);sort(line+1,line+1+k,cmp);build(1,1,k);double ans=0;for(int i=1;i<=k;i++)ans+=insert(1,line[i].x,line[i].down,line[i].up,line[i].flag);printf("Test case #%d\nTotal explored area: %.2f\n\n",ca++,ans);}return 0;}/*210 10 20 2015 15 25 25.50*/


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.