题意:给出n个矩形,矩形间可能有重复的区域。问所有矩形覆盖的面积?【题目中说给的信息是矩形左上和右下的坐标信息(因为它的坐标面是y的正方向是向下的),无所谓,反过来就好】
思路:扫描线的模板题。
FEELING:目前写了两遍。第一遍当然自己写不下来。大致理解了之后开始回忆着敲,有很多bug。然后发现其实还是有不懂的地方。就是对于线段树的建树和pushup上。
//以下扫描线的写法都是横向扫描线竖着扫的(反过来都一样)
CODE FIRST:
这个是最开始的写法。是对“块”建线段树。注意离散化后的x有up个,那么所谓“块”就有up - 1个。【开始就是因为讲题的学长这讲的是up,让我迷了好久】
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#define INF 0x3f3f3f3f
#define MID r + l >> 1
#define lsn rt << 1
#define rsn rt << 1 | 1
#define Lson lsn, l, mid
#define Rson rsn, mid + 1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxN = 200 + 5;//每一次输入有两个x, y,开两倍
int n, up;
struct node{
double lx, rx, high;
int add;
node(double a = 0, double b = 0, double c = 0, int d = 0) : lx(a), rx(b), high(c), add(d) {}
friend bool operator < (node n1, node n2) { return n1.high < n2.high; }
}info[maxN];
double discx[maxN];
double tree[maxN << 2];
int cover[maxN << 2];
void pushup(int rt, int l, int r)
{
if(cover[rt]) { tree[rt] = discx[r + 1] - discx[l]; return ; }//因为是对块建线段树,它对应右边界应该是r + 1
else if(l == r) tree[rt] = 0;//如果到达叶子结点,并且还没有cover,那它对应块的区间长度一定是0
else tree[rt] = tree[lsn] + tree[rsn];
}
void build_tree(int rt, int l, int r)
{
tree[rt] = 0;
cover[rt] = 0;
if(l == r) return;
int mid = MID;
build_tree(Lson);
build_tree(Rson);
}
void update(int rt, int l, int r, int ql, int qr, int val)
{
if(ql <= l && qr >= r)
{
cover[rt] += val;
pushup(rt, l, r);//该区间的cover改变了,但是要将它涵盖的所有小区间pushup了
//比如说一个小区间还没有到达上界,但是包含这个小区间的一个大区间到达上界了。大区间的cover+=val之后变为了0
//但是因为有这个小区间还没有到达上界,于是这个小区间仍然是扫面线间的有效面积
return ;
}
int mid = MID;
if(qr <= mid) update(QL, val);
else if(ql > mid) update(QR, val);
else { update(QL, val); update(QR, val); }
pushup(rt, l, r);
}
int main()
{
int Case = 0;
while(~scanf("%d", &n) && n)
{
int tot = 0;
for(int i = 0; i < n; i ++ )
{
double x1, y1, x2, y2;
scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2);
info[++ tot] = node(x1, x2, y1, 1);
discx[tot] = x1;
info[++ tot] = node(x1, x2, y2, -1);
discx[tot] = x2;
}
sort(info + 1, info + tot + 1);
sort(discx + 1, discx + tot + 1);
up = unique(discx + 1, discx + tot + 1) - discx - 1;
//对离散化后的x对应的块建线段树,例如有三个x点:1,2,3
//那么我们的线段树中就只有两个块:[1, 2],[2, 3],我们将这两个块用构成它们的点除去最后一个点的所有点表示也就是1,2
build_tree(1, 1, up - 1);//点有up个,那么块就是up - 1个
double ans = 0;
for(int i = 1; i < tot; i ++ )
{
int lx = lower_bound(discx + 1, discx + up + 1, info[i].lx) - discx;
int rx = lower_bound(discx + 1, discx + up + 1, info[i].rx) - discx - 1;
update(1, 1, up - 1, lx, rx, info[i].add);
ans += tree[1] * (info[i + 1].high - info[i].high);
}
printf("Test case #%d\nTotal explored area: %.2f\n\n", ++ Case, ans);
}
return 0;
}
CODE SECOND
这个就是另外一种对“块”建线段树的方式【叶子结点为区间长度为1的区间】。因为我们存的是长度,所以区间长度至少为1。例如:[1, 2],[2, 3]。可以发现的就是区间的右端点和另外区间的左端点是可以重合的。【这个也是上面附的那个博客上面的写法】。这个就是只需要改变线段树的板子为端点可以重合即可。
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#define INF 0x3f3f3f3f
#define lowbit(x) x & (-x)
#define MID (l + r ) >> 1
#define lsn rt << 1
#define rsn rt << 1 | 1
#define Lson lsn, l, mid
#define Rson rsn, mid, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxN = 200 + 5;//每一次输入有两个x, y,开两倍
int n;
struct node{
double lx, rx, high;
int add;
node(double a = 0, double b = 0, double c = 0, int d = 0) : lx(a), rx(b), high(c), add(d) {}
friend bool operator < (node n1, node n2) { return n1.high < n2.high; }
}info[maxN];
double discx[maxN]; //discretize
int up;//x坐标离散化后上界
double tree[maxN << 2]; //扫描线覆盖的某个区间的长度
int cover[maxN << 2];//某个区间的覆盖的点次数
void pushup(int rt, int l, int r)
{
if(cover[rt])
tree[rt] = discx[r] - discx[l];// 区间长度,[1, r]区间上的长度
else if(r - l == 1)//区间长度为1,也就是一个块,而且这个块没有被覆盖,所以是0
tree[rt] = 0;
else tree[rt] = tree[lsn] + tree[rsn];
}
void build_tree(int rt, int l, int r)
{
tree[rt] = 0;
cover[rt] = 0;
if(r - l == 1) return;
int mid = MID;
build_tree(Lson);
build_tree(Rson);
}
void update(int rt, int l, int r, int ql, int qr, int val)
{
if(ql <= l && qr >= r)
{
cover[rt] += val;//当前区间覆盖点的次数的变化
pushup(rt, l, r); //它包含的小区间的覆盖的扫描线的长度给pushup到tree[rt]上来
return ;
}
int mid = MID;
if(qr <= mid)
update(QL, val);
else if(ql >= mid)
update(QR, val);
else { update(QL, val); update(QR, val); }
pushup(rt, l, r);
}
int main()
{
int Case = 0;
while(~scanf("%d", &n) && n)
{
int tot = 0;
for(int i = 0; i < n; i ++ )
{
double x1, y1, x2, y2;
scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2);//左下,右上
info[++ tot] = node(x1, x2, y1, 1);
discx[tot] = x1;
info[++ tot] = node(x1, x2, y2, -1);
discx[tot] = x2;
}
sort(info + 1, info + tot + 1); // 对y轴坐标排序,从小y开始扫描
sort(discx + 1, discx + tot + 1);
up = unique(discx + 1, discx + tot + 1) - discx - 1;//对x轴坐标的离散化
//对离散化后的x坐标建线段树
build_tree(1, 1, up);
double ans = 0;
//枚举y坐标,也就是扫描线的位置
for(int i = 1, lx, rx; i < tot; i ++ )//不需要跑到tot,tot是最后一条扫描线,后面不会再形成区间
{
lx = lower_bound(discx + 1, discx + up + 1, info[i].lx) - discx;
rx = lower_bound(discx + 1, discx + up + 1, info[i].rx) - discx;
update(1, 1, up, lx, rx, info[i].add);
ans += tree[1] * (info[i + 1].high - info[i].high);
}
printf("Test case #%d\nTotal explored area: %.2f\n\n", ++ Case, ans);
}
return 0;
}