这是一道模板题,描述了一种叫扫描线的算法
题意就是给出一个坐标系和 nnn 个矩形,求面积并,如下图:
矩形有凹凸不是太好直接算出
试想,如果我们用一条竖线从左到右扫过坐标系,那么直线上的并集图形覆盖长度只会在每个矩形左右边界处发生变化
换言之,其实就是 2∗n2*n2∗n 个线段,每一段在直线上覆盖的长度固定,因此此块的面积就是 L∗L*L∗ 该段的宽度,各段面积之和即为所求,这条直线称为扫描线,我们称这个算法叫扫描线算法
具体来说,我们可以取 nnn 个矩形的左右边界,若一个矩阵的两个对角顶点坐标为 (x1,y1)(x_1,y_1)(x1,y1) 和 (x2,y2)(x_2,y_2)(x2,y2),不妨设 x1<x2x_1<x_2x1<x2,y1<y2y_1<y_2y1<y2,则左边界为四元组 (x1,y1,y2,1)(x_1,y_1,y_2,1)(x1,y1,y2,1),右边界为 (x2,y1,y2,−1)(x_2,y_1,y_2,-1)(x2,y1,y2,−1),不妨把这些四元组按照 xxx 递增排序,如图所示(比较模糊,见谅):
显然本题需要对坐标进行离散化,设 val(y)val(y)val(y) 表示 yyy 被离散化后对应的数值, raw(x)raw(x)raw(x) 表示 xxx 对应的原数
离散化后,假设有 mmm 个数值,分别对应 raw(1),raw(2),...raw(m)raw(1),raw(2),...raw(m)raw(1),raw(2),...raw(m),则扫描线至多被分成 m−1m-1m−1 段,第 iii 段为 [raw(i),raw(i+1)][raw(i),raw(i+1)][raw(i),raw(i+1)]
建立数组 c[i]c[i]c[i] 表示第 iii 段被覆盖的次数,初始 ccc 数组为 000
对于一个排序完后的数组 (x,y1,y2,k)(x,y_1,y_2,k)(x,y1,y2,k),他会对 c[val(y1)],c[val(y1)+1],...,c[val(y2)−1]c[val(y_1)],c[val(y_1)+1],...,c[val(y_2)-1]c[val(y1)],c[val(y1)+1],...,c[val(y2)−1] 都加 kkk,相当于覆盖 [y1,y2][y_1,y_2][y1,y2] 区间,此时如果下一个四元组横坐标为 x2x_2x2,那么扫描线从 xxx 扫到 x2x_2x2 时,被覆盖的长度固定为 ∑c[i]>0(raw(i+1)−raw(i))\sum\limits_{c[i]>0}(raw(i+1)-raw(i))c[i]>0∑(raw(i+1)−raw(i)),即数组 ccc 中至少被覆盖一次的“段”的长度
于是,我们就让最终的答案 ansansans 累加上 (x2−x)∗∑c[i]>0(raw(i+1)−raw(i))(x_2-x)*\sum\limits_{c[i]>0}(raw(i+1)-raw(i))(x2−x)∗c[i]>0∑(raw(i+1)−raw(i))
对于每个四元组,采用暴力在 ccc 数组上执行修改与统计,即可在 O(n2)O(n^2)O(n2) 的时间复杂度内完成
值得说明的是,四元组 (y1,y2)(y_1,y_2)(y1,y2) 都是坐标,是一个点
我们需要维护的是扫描线上每一段被覆盖的次数及其长度,对“点”的覆盖次数进行统计是没有意义的
因为我们把 ccc 数组中的每一个值定义成扫描线上一个区间的覆盖次数,四元组 (x,y1,y2,k)(x,y_1,y_2,k)(x,y1,y2,k) 对 c[val(y1) val(y2)−1]c[val(y_1)~val(y_2)-1]c[val(y1) val(y2)−1] 产生影响。读者在解题时一定要注意此类边界情况
通过线段树维护 ccc 数组,使得复杂度为 O(n log n)O(n~log~n)O(n log n)
#include <map>
#include <set>
#include <ctime>
#include <queue>
#include <stack>
#include <cmath>
#include <vector>
#include <bitset>
#include <cstdio>
#include <cctype>
#include <string>
#include <numeric>
#include <cstring>
#include <cassert>
#include <climits>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std ;
//#define int long long
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define loop(s, v, it) for (s::iterator it = v.begin(); it != v.end(); it++)
#define cont(i, x) for (int i = head[x]; i; i = e[i].nxt)
#define clr(a) memset(a, 0, sizeof(a))
#define ass(a, sum) memset(a, sum, sizeof(a))
#define lowbit(x) (x & -x)
#define all(x) x.begin(), x.end()
#define ub upper_bound
#define lb lower_bound
#define pq priority_queue
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define iv inline void
#define enter cout << endl
#define siz(x) ((int)x.size())
#define file(x) freopen(#x".in", "r", stdin),freopen(#x".out", "w", stdout)
typedef long long ll ;
typedef unsigned long long ull ;
typedef pair <int, int> pii ;
typedef vector <int> vi ;
typedef vector <pii> vii ;
typedef queue <int> qi ;
typedef queue <pii> qii ;
typedef set <int> si ;
typedef map <int, int> mii ;
typedef map <string, int> msi ;
const int N = 410 ;
const int INF = 0x3f3f3f3f ;
const int iinf = 1 << 30 ;
const ll linf = 2e18 ;
const int MOD = 1000000007 ;
const double eps = 1e-7 ;
void print(int x) { cout << x << endl ; exit(0) ; }
void PRINT(string x) { cout << x << endl ; exit(0) ; }
void douout(double x){ printf("%lf\n", x + 0.0000000001) ; }
int tag[N << 2] ;
double sum[N << 2], X[N] ;
int n, num, Case ;
struct node {
double l, r, y ; int fl ;
bool operator < (node a) const {
return y < a.y ;
}
} l[N] ;
#define ls(x) x << 1
#define rs(x) x << 1 | 1
void pushup(int x, int l, int r) {
if (tag[x]) sum[x] = X[r + 1] - X[l] ;
else if (l == r) sum[x] = 0 ;
else sum[x] = sum[ls(x)] + sum[rs(x)] ;
}
void modify(int x, int l, int r, int L, int R, int c) {
if (L > r || R < l) return ;
if (L <= l && r <= R) {
tag[x] += c ;
pushup(x, l, r) ;
return ;
}
int mid = (l + r) >> 1 ;
modify(ls(x), l, mid, L, R, c) ;
modify(rs(x), mid + 1, r, L, R, c) ;
pushup(x, l, r) ;
}
signed main(){
while (scanf("%d", &n) != EOF && n) {
clr(tag) ; clr(sum) ; num = 0 ;
rep(i, 1, n) {
double x1, x2, y1, y2 ;
scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2) ;
l[++num] = (node) {x1, x2, y1, 1} ; X[num] = x1 ;
l[++num] = (node) {x1, x2, y2, -1} ; X[num] = x2 ;
}
sort(l + 1, l + num + 1) ; sort(X + 1, X + num + 1) ;
double ans = 0 ;
int tt = unique(X + 1, X + num + 1) - (X + 1) ;
rep(i, 1, num - 1) {
int ll = lb(X + 1, X + tt + 1, l[i].l) - X, rr = lb(X + 1, X + tt + 1, l[i].r) - (X + 1) ;
modify(1, 1, tt, ll, rr, l[i].fl) ;
ans += sum[1] * (l[i + 1].y - l[i].y) ;
}
printf("Test case #%d\n", ++Case) ;
printf("Total explored area: %.2f\n\n", ans) ;
}
return 0 ;
}
/*
写代码时请注意:
1.ll?数组大小,边界?数据范围?
2.精度?
3.特判?
4.至少做一些
思考提醒:
1.最大值最小->二分?
2.可以贪心么?不行dp可以么
3.可以优化么
4.维护区间用什么数据结构?
5.统计方案是用dp?模了么?
6.逆向思维?
*/