朴素 O(n^2) 扫描线不过 #1 #2,但过了样例,求调
查看原帖
朴素 O(n^2) 扫描线不过 #1 #2,但过了样例,求调
999274
CNS_5t0_0r2楼主2024/1/26 19:05

打算先把暴力扫描线写过了再写线段树

#include<iostream>
#include<algorithm>
using namespace std;
const int N = 1e5 + 9;
int n;
int c[N << 1];
struct rect{
	int x1,y1,x2,y2;
} a[N];
int cnt;
struct node{
	int x,y1,y2,k;
} seg[N << 1];
bool cmp(node elem1,node elem2){
	return elem1.x < elem2.x;
}
int tmp[N << 1],top;
long long ans;
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	cin >> n;
	for(int i = 1;i <= n;i++){
		cin >> a[i].x1 >> a[i].y1 >> a[i].x2 >> a[i].y2;
		tmp[++top] = a[i].y1;
		tmp[++top] = a[i].y2;
	}
	sort(tmp + 1,tmp + top + 1);
	top = unique(tmp + 1,tmp + top + 1) - tmp - 1;
	for(int i = 1;i <= n;i++){
		a[i].y1 = lower_bound(tmp + 1,tmp + top + 1,a[i].y1) - tmp;
		a[i].y2 = lower_bound(tmp + 1,tmp + top + 1,a[i].y2) - tmp;
		seg[++cnt] = (node){a[i].x1,a[i].y1,a[i].y2,1};
		seg[++cnt] = (node){a[i].x2,a[i].y1,a[i].y2,-1};
	}
	sort(seg + 1,seg + cnt + 1,cmp);
	for(int i = 1;i <= cnt;i++){
		int l = seg[i].y1,r = seg[i].y2,k = seg[i].k;
		int x = seg[i].x,x2 = seg[i + 1].x;
		for(int j = l;j < r;j++){
			c[j] += k;
		}
		for(int j = 1;j < top;j++){
			if(c[j] > 0){
				ans += (x2 - x) * (tmp[j + 1] - tmp[j]);
			}
		}
	}
	cout << ans;
	return 0;
}
2024/1/26 19:05
加载中...