Blue Bridge Cup paint area (line segment tree interval modification + discrete + scan line)

10. Title: Painted area

A group of archaeological robots on Planet X are archaeologically ruining.
The ground in this area is as hard as stone and flat as a mirror.
For the convenience of managers, a standard rectangular coordinate system has been established.

Each robot has its own strengths and special skills. They are also interested in different content.
After various measurements, each robot will report one or more rectangular areas as a priority archaeological area.

The representation format of the rectangle is (x1, y1, x2, y2), which represents the coordinates of two diagonal points of the rectangle.

To be eye-catching, the headquarters required yellow paint on the rectangular areas selected by all robots.
Xiaoming doesn't need to be a painter, but he needs to calculate how much paint will be spent.

In fact, this is not difficult, as long as you calculate the total area covered by all rectangles.
Note that the rectangles may overlap.

The input for this question is a number of rectangles, and it is required to output the total area covered by it.

Input format: the
first line, an integer n, indicating how many rectangles (1 <= n <10000) the
next n lines, each line has 4 integers x1 y1 x2 y2, separated by spaces, indicating two pairs of rectangles Corner vertex coordinates.
(0 <= x1, y1, x2, y2 <= 10000)

Output format:
an integer on a line, representing the total area covered by the rectangle.

For example,
enter:
3
1 5 10 10
3 1 20 20
2 7 15 17

The program should output:
340

For another example,
enter:
3
5 2 10 6
2 7 12 10
8 1 15 15

The program should output:
128

Resource convention:
peak memory consumption (including virtual machine) <256M
CPU consumption <2000ms
 

 

Remove the duplicate points, and then count the number of points. Assuming that there are x points, then there are x-1 intervals.

At this time, each interval is maintained. Whenever a group of points starts and ends, the interval contained in the group of points is maintained.

Scan the lines from bottom to top after sorting by height

#include<bits/stdc++.h>
using namespace std;

int X[20005];
int n;
struct Lines{
	int x1,x2,h,f;
	

}lines[20000];

struct segtree{
	int l,r,cnt,len;
	
}tree[4*20000];
bool compare(Lines &a,Lines &b){
		return a.h<b.h;
	}
	 
void buildtree(int o,int pl,int pr){
	tree[o].l=pl;
	tree[o].r=pr;
	tree[o].cnt=tree[o].len=0;
	if(pl==pr){
		return ;
	}
	int mid=(pl+pr)>>1;
	buildtree(o*2,pl,mid);
	buildtree(o*2+1,mid+1,pr);

}
void updatelength(int o, int tl, int tr) {
    if (tree[o].cnt > 0) {
      tree[o].len = X[tr] - X[tl - 1];//将区间树上的端点(序号)代入到X中求得二维坐标上的实际横坐标
    } else if (tl == tr) {
      tree[o].len = 0;
    } else {//负数
      tree[o].len = tree[o*2].len + tree[o*2+1].len;
    } 
  }	 
	 
void update(int o,int pl,int pr,int value){
	int lchild=o*2;
	int rchild=o*2+1;
	if(pl<=tree[o].l && pr>=tree[o].r){
		tree[o].cnt+=value;
		updatelength(o,tree[o].l,tree[o].r);
		return ;
	}
	int mid=(tree[o].l+tree[o].r)>>1;
	if(pl<=mid) update(lchild,pl,mid,value);
	if(pr>mid)  update(rchild,mid+1,pr,value);
	updatelength(o,tree[o].l,tree[o].r);
}	 

int unique(int *x){
	int index=0;
	for(int i=1;i<=2*n-1;i++){
		if(x[i]==x[index])
		continue;
		else{
			index++;
			x[index]=x[i];
		}	
	}
	return index;
}
	 
int binsearch(int a,int X_end){
	int l=0,r=X_end;
	while(l<r){
		int mid=(l+r)/2;
		if(X[mid]==a) return mid;
		else if(X[mid]<a)
		l=mid+1;
		else
		r=mid;
	}
		return l; 
}
	 
int main(){
	scanf("%d",&n);
	int x1,y1,x2,y2;
	int index=0;
	int pl,pr;
	int ans=0;
	for(int i=0;i<n;i++)
	{
		scanf("%d %d %d %d",&x1,&y1,&x2,&y2);
		X[index]=x1;
		lines[index].x1=x1;
		lines[index].x2=x2;
		lines[index].h=y1;
		lines[index].f=1;
		index++;
		
		X[index]=x2;
		lines[index].x1=x1;
		lines[index].x2=x2;
		lines[index].h=y2;
		lines[index].f=-1;
		index++;
	}
	
	
	sort(X,X+index);
	sort(lines,lines+index,compare);

	int X_end=unique(X);
	buildtree(1,1,X_end);

	for(int i=0;i<index-1;i++){
		pl=binsearch(lines[i].x1,X_end);
		pr=binsearch(lines[i].x2,X_end);
		update(1,pl+1,pr,lines[i].f);
		ans+=tree[1].len*(lines[i+1].h-lines[i].h);
	}
	
	printf("\n%d",ans);
}

 

Published 75 original articles · praised 77 · views 4017

Guess you like

Origin blog.csdn.net/weixin_43568895/article/details/104931906