POJ1177+線段樹+掃描線

來源:互聯網
上載者:User

思路:

以y的值進行離散化

根據x的值 對每一條y軸邊進行處理,如果是"左邊"則插入,是"右邊"則刪除。

/*掃描線+線段樹+離散化求多個矩形的周長*/#include<stdio.h>#include<string.h>#include<stdlib.h>#include<algorithm>#include<iostream>#include<queue>#include<stack>#include<math.h>#include<map>using namespace std;const int maxn = 5005;const int maxm = 10010;struct SegTree{int l,r;int len;//區間代表的長度int segnum;//區間被分成的段數int cover;//區間被覆蓋的次數int sum;//區間被覆蓋的總長度bool lcover,rcover;}ST[ maxm<<2 ];struct Line{int st,ed,x;//豎邊的兩個y值bool InOut;//是否為左邊bool operator < (Line L) const{return x<L.x;}};Line yLine[ maxm ];int yIndex[ maxm ];int n;void build( int L,int R,int n ){ST[ n ].l = L;ST[ n ].r = R;ST[ n ].len = yIndex[ R ]-yIndex[ L ];ST[ n ].sum = ST[ n ].cover = ST[ n ].segnum = 0;ST[ n ].lcover = ST[ n ].rcover = false;if( R-L>1 ){int mid = (L+R)/2;build( L,mid,2*n );build( mid,R,2*n+1 );}return ;}void Update_Len( int n ){if( ST[n].cover>0 ){ST[n].sum = ST[n].len;}else if( ST[n].r-ST[n].l>1 ){ST[n].sum = ST[2*n].sum+ST[2*n+1].sum;}elseST[n].sum = 0;}void Update_Segnum( int n ){if( ST[n].cover>0 ){ST[n].lcover = ST[n].rcover = true;ST[n].segnum = 1;}else if( ST[n].r-ST[n].l>1 ){ST[n].lcover = ST[2*n].lcover;ST[n].rcover = ST[2*n+1].rcover;ST[n].segnum = ST[2*n].segnum+ST[2*n+1].segnum-ST[2*n].rcover*ST[2*n+1].lcover;}else{ST[n].segnum = 0;ST[n].lcover = ST[n].rcover = false;}}void PushUp ( int n ){Update_Len( n );Update_Segnum( n );}void Insert( int left,int right,int n ){if( ST[ n ].l==left&&ST[ n ].r==right ){ST[ n ].cover++;}else {int mid = (ST[ n ].l+ST[ n ].r)/2;if( right<=mid )Insert( left,right,2*n );else if( left>=mid )Insert( left,right,2*n+1 );else{Insert( left,mid,2*n );Insert( mid,right,2*n+1 );}}PushUp( n );}void Delete( int left,int right,int n ){if( ST[ n ].l==left&&ST[ n ].r==right ){ST[ n ].cover--;}else {int mid = (ST[ n ].l+ST[ n ].r)/2;if( right<=mid )Delete( left,right,2*n );else if( left>=mid )Delete( left,right,2*n+1 );else{Delete( left,mid,2*n );Delete( mid,right,2*n+1 );}}PushUp( n );}int GetIndex( int value ,int cnt ){return lower_bound(yIndex,yIndex+cnt,value )-yIndex;}int main(){while( scanf("%d",&n)==1 ){int cnt = 0;int x1,y1,x2,y2;for( int i=0;i<n;i++ ){scanf("%d%d%d%d",&x1,&y1,&x2,&y2);yLine[ 2*i ].x = x1;yLine[ 2*i+1 ].x = x2;yLine[ 2*i ].st = yLine[ 2*i+1 ].st = y1; yLine[ 2*i ].ed = yLine[ 2*i+1 ].ed = y2;yLine[ 2*i ].InOut = true;yLine[ 2*i+1 ].InOut = false;yIndex[ 2*i ] = y1;yIndex[ 2*i+1 ] = y2;}sort( yIndex,yIndex+2*n );sort( yLine,yLine+2*n );for( int i=1;i<2*n;i++ ){if( yIndex[i]!=yIndex[i-1] )yIndex[cnt++] = yIndex[i-1];}yIndex[cnt++] = yIndex[2*n-1];build( 0,cnt-1,1 );int Ans = 0;int PreSum = 0;;for( int i=0;i<2*n-1;i++ ){if( yLine[i].InOut ){Insert( GetIndex(yLine[i].st,cnt),GetIndex(yLine[i].ed,cnt),1 );}else{Delete( GetIndex(yLine[i].st,cnt),GetIndex(yLine[i].ed,cnt),1 );}Ans += ST[1].segnum*2*(yLine[i+1].x-yLine[i].x);Ans += abs(ST[1].sum-PreSum);PreSum = ST[1].sum;}Delete( GetIndex(yLine[2*n-1].st,cnt),GetIndex(yLine[2*n-1].ed,cnt),1 );Ans += abs(ST[1].sum-PreSum);printf("%d\n",Ans);}return 0;}

聯繫我們

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