hdu1264之线段树+扫描线

Counting Squares

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1049    Accepted Submission(s): 523


Problem Description
Your input is a series of rectangles, one per line. Each rectangle is specified as two points(X,Y) that specify the opposite corners of a rectangle. All coordinates will be integers in the range 0 to 100. For example, the line
5 8 7 10
specifies the rectangle who's corners are(5,8),(7,8),(7,10),(5,10).
If drawn on graph paper, that rectangle would cover four squares. Your job is to count the number of unit(i.e.,1*1) squares that are covered by any one of the rectangles given as input. Any square covered by more than one rectangle should only be counted once.
 

Input
The input format is a series of lines, each containing 4 integers. Four -1's are used to separate problems, and four -2's are used to end the last problem. Otherwise, the numbers are the x-ycoordinates of two points that are opposite corners of a rectangle.
 

Output
Your output should be the number of squares covered by each set of rectangles. Each number should be printed on a separate line.
 

Sample Input
 
   
5 8 7 10 6 9 7 8 6 8 8 11 -1 -1 -1 -1 0 0 100 100 50 75 12 90 39 42 57 73 -2 -2 -2 -2
 

Sample Output
 
   
8 10000
 

题目意思给定若干矩形,求这些矩形总的覆盖的面积,重叠的只算一次
输入的四个数表示矩形对角坐标,输入四个-1结束一次输入,输入四个-2结束输入

本题由于数据小可以hash+暴力,但是为了防止碰到类似的题并且数据大还是统一用线段树写好

#include
#include
#include
#include
#include
#include
#include
#include
#include
#define INF 99999999
using namespace std;

const int MAX=200+10;
int mark[MAX<<2];//记录某个区间的下底边比上底边多的个数
double sum[MAX<<2];//记录某个区间下底边比上底边多的个数总长度 
double hash[MAX];//对横坐标x离散化

struct seg{//线段 
	double l,r,h;
	int d;
	seg(){}
	seg(double x1,double x2,double H,int D):l(x1),r(x2),h(H),d(D){}
	bool operator<(const seg &a)const{
		return h>1;
	if(L<=mid)Update(L,R,d,n<<1,left,mid);
	if(R>mid)Update(L,R,d,n<<1|1,mid+1,right);
	Upfather(n,left,right);
}

int search(double key,double *x,int n){
	int left=0,right=n-1;
	while(left<=right){
		int mid=left+right>>1;
		if(x[mid] == key)return mid;
		else if(x[mid]>key)right=mid-1;
		else left=mid+1;
	}
	return -1;
}

int main(){
	double x1=0,y1=0,x2=0,y2=0;
	while(x1 != -2){
		int size=0;
		while(cin>>x1>>y1>>x2>>y2,x1>=0){
			if(x1>x2)swap(x1,x2);
			if(y1>y2)swap(y1,y2);
			hash[size]=x1;
			s[size++]=seg(x1,x2,y1,1);
			hash[size]=x2;
			s[size++]=seg(x1,x2,y2,-1);
		}
		sort(hash,hash+size);
		sort(s,s+size);
		int k=1;
		for(int i=1;i


你可能感兴趣的:(线段树)