POJ 1151 Atlantis(掃描線)

來源:互聯網
上載者:User

題意:給你n個矩形,每個矩形給出左下點的座標,右上點的座標。最後以n=0為結束。要你求出矩形並後的面積。

         掃描線。 在陳宏的論文裡這麼定義超元線段:根據每個矩形縱向邊的橫座標縱向地對平面進行2*n次切割、根據矩形橫向邊的縱座標橫向地對矩形進行2*n次切割(n為矩形個數),切割後得到的矩形的被切割後的小段邊就是超元線段。

         現在我們僅考慮未被橫向的邊切割的超元線段(即矩形縱向的邊),這些邊上的直線把矩形分成這2*n-1塊地區,而這些地區的面積和也就是矩形並後的面積。

         如何求每一塊地區的面積呢?長可以通過兩條相鄰的超元線段的x座標差得到(即line[i].x-line[i-1].x)。如果求出寬呢?

         寬就是line[i]之前(不包括i)的所有線段全部投影到y軸上得到的長度,也就是說我們求出y軸上被覆蓋的地區的長度即可。

         而這個的實現方法是用維段樹去維護。當我們從左往右不斷地將線段放入線段樹以求得在y軸上投影得到的地區的長度時,我們把同一個矩形左邊的邊的權值設定為1(表示覆蓋),右邊的邊設定為-1(表示撤消覆蓋)。這樣就能實現對y軸上投影的更新,因為這是假想有一根線去不斷地從左往右掃,所以叫掃描線。

         但是還要注意,如果給出的矩形的範圍太大,如果10^9那麼我們要對它進行離散化,即線上段樹的節點裡增加兩個變數,表示這條線段真正的端點,而原來的兩個變數表示線上段樹中的端點。在建樹的時候還有點變化即(left,mid),(mid,right),因為節點裡存放的是線段,不是點。

/*代碼風格更新後*/#include <iostream>#include <cstdio>#include <cstring>#include <vector>#include <algorithm>#include <map>using namespace std;#define LL(x) (x<<1)#define RR(x) (x<<1|1)#define MID(a,b) (a+((b-a)>>1))const int N=205;struct Line{    int flag;    double x,y1,y2;    Line(){}    Line(double a,double b,double c,int d)    { x=a;y1=b;y2=c;flag=d; }    bool operator<(const Line &b)const    { return x<b.x; }};struct node{    int lft,rht,flag;    double ll,rr,len;    int mid(){return MID(lft,rht);}    void fun(int valu)    {        flag+=valu;        if(flag==0) len=0;        else len=rr-ll;    }};vector<double> y;vector<Line> line;map<double,int> H;struct Segtree{    node tree[N*4];    void build(int lft,int rht,int ind)    {        tree[ind].lft=lft;    tree[ind].rht=rht;        tree[ind].flag=0;    tree[ind].len=0;        tree[ind].ll=y[lft];    tree[ind].rr=y[rht];        if(lft+1!=rht)        {            int mid=tree[ind].mid();            build(lft,mid,LL(ind));            build(mid,rht,RR(ind));        }    }    void updata(int st,int ed,int ind,int valu)    {        int lft=tree[ind].lft,rht=tree[ind].rht;        if(lft+1==rht) tree[ind].fun(valu);        else        {            int mid=tree[ind].mid();            if(st<mid) updata(st,ed,LL(ind),valu);            if(ed>mid) updata(st,ed,RR(ind),valu);            tree[ind].len=tree[LL(ind)].len+tree[RR(ind)].len;        }    }}seg;int main(){    int n,t_cnt=0;    while(scanf("%d",&n)&&n)    {        H.clear(); line.clear(); y.clear();        double x1,y1,x2,y2;        for(int i=1;i<=n;i++)        {            scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);            line.push_back(Line(x1,y1,y2,1));            line.push_back(Line(x2,y1,y2,-1));            y.push_back(y1); y.push_back(y2);        }        sort(line.begin(),line.end());        sort(y.begin(),y.end());        y.erase(unique(y.begin(),y.end()),y.end());        for(int i=0;i<(int)y.size();i++) H[y[i]]=i;        seg.build(0,(int)y.size()-1,1);        double res=0;        printf("Test case #%d\n",++t_cnt);        for(int i=0;i<(int)line.size();i++)        {            if(i!=0) res+=(line[i].x-line[i-1].x)*seg.tree[1].len;            seg.updata(H[line[i].y1],H[line[i].y2],1,line[i].flag);        }        printf("Total explored area: %.2lf\n\n",res);    }    return 0;}

/*代碼風格更新前*/#include <iostream>#include <cstdio>#include <algorithm>using namespace std;const int N=100;double y[N*2];struct Line{    int co;    double x,y1,y2;    void fun(double a,double b,double c,int d){x=a;y1=b;y2=c;co=d;}}line[N*2];struct node{    int left,right,co;    double rf,lf,len;    int mid(){return left+(right-left)/2;}    void change(int a)    {        co+=a;        if(co==0){len=0;}        else len=rf-lf;    }};struct Segtree{    node tree[N*8];    void build(int left,int right,int r)    {        tree[r].left=left;  tree[r].right=right;        tree[r].lf=y[left]; tree[r].rf=y[right];        tree[r].len=0;        if(left+1<right)        {            int mid=tree[r].mid();            build(left,mid,r*2);            build(mid,right,r*2+1);        }    }    void updata(Line e,int r)    {        if(tree[r].left+1==tree[r].right)        {            tree[r].change(e.co);        }        else        {            if(e.y1<tree[r*2].rf) updata(e,r*2);            if(e.y2>tree[r*2+1].lf) updata(e,r*2+1);            tree[r].len=tree[r*2].len+tree[r*2+1].len;        }    }}seg;int deal(int);bool cmp(const Line &e1,const Line &e2){    return e1.x<e2.x;}int main(){    int n,t_cnt=0;    while(scanf("%d",&n)!=EOF)    {        if(n==0) break;        int d=0;        double x1,y1,x2,y2,ans=0;        for(int i=0;i<n;i++)        {            scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);            line[d].fun(x1,y1,y2,1);    y[d++]=y1;            line[d].fun(x2,y1,y2,-1);   y[d++]=y2;        }        int cnt=deal(d);        seg.build(0,cnt-1,1);        seg.updata(line[0],1);        for(int i=1;i<d;i++)        {            ans+=((line[i].x-line[i-1].x)*seg.tree[1].len);            seg.updata(line[i],1);        }        printf("Test case #%d\nTotal explored area: %.2f\n\n",++t_cnt,ans);        //POJ上%.2lf過不了。。。    }    return 0;}int deal(int n){    sort(y,y+n);    sort(line,line+n,cmp);    int cnt=1;    for(int i=1;i<n;i++)    {        if(y[i]!=y[i-1]) y[cnt++]=y[i];    }    return cnt;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.