POJ1177+线段树+扫描线

思路:

以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;
	}
	else
		ST[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
",Ans);
	}
	return 0;
}



原文地址:https://www.cnblogs.com/jiangu66/p/3235322.html