题目链接:[ HDU - 1255 ]

题目大意:给你n个矩形的左下角坐标和右上角坐标,求矩形相交至少覆盖两次以上的面积。

题解

代码如下

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

const int maxn = 2010;

double dif_x[maxn]; //记录不同的x坐标
int n, t = 0;

struct node {
    double x1, x2, y;
    int flag; 
    void init(double l, double r, double h, int key) {
        x1 = l, x2 = r, y = h, flag = key;
    }
    bool operator < (const node& a) const {
        return y < a.y;
    }
}line[maxn];

struct Node {
    int l, r, cnt;
    double len;
    double len2;
}tree[maxn * 4];

void build_tree(int id, int l, int r)
{
    tree[id].l = l;
    tree[id].r = r;
    tree[id].cnt = 0;
    tree[id].len = 0;
    tree[id].len2 = 0;

    if (l == r)
        return;
    int mid = (r + l) >> 1;
    build_tree(id << 1, l, mid);
    build_tree(id << 1 | 1, mid + 1, r);
}

void getlen(int id)
{
    if (tree[id].cnt) //如果该段被覆盖那么就直接由dif_x数组获得长度
        tree[id].len = dif_x[tree[id].r + 1] - dif_x[tree[id].l]; 
    else if (tree[id].l == tree[id].r)
        tree[id].len = 0;
    else              //如果没有被覆盖,那么应该是由左右孩子的和
        tree[id].len = tree[id << 1].len + tree[id << 1 | 1].len; 

    if (tree[id].cnt >= 2)
        tree[id].len2 = dif_x[tree[id].r + 1] - dif_x[tree[id].l];
    else if (tree[id].l == tree[id].r)
        tree[id].len2 = 0;
    else if (tree[id].cnt == 1)
        tree[id].len2 = tree[id << 1].len + tree[id << 1 | 1].len;
    else
        tree[id].len2 = tree[id << 1].len2 + tree[id << 1 | 1].len2;
}

void update(int id, int l, int r, int v)
{
    if (tree[id].l == l && tree[id].r == r) {
        tree[id].cnt += v;
        getlen(id);
        return;
    }

    int mid = (tree[id].l + tree[id].r) >> 1;
    if (r <= mid)
        update(id << 1, l, r, v);
    else if (l > mid)
        update(id << 1 | 1, l, r, v);
    else {
        update(id << 1, l, mid, v);
        update(id << 1 | 1, mid + 1, r, v);
    }
    getlen(id);
}

int main(void)
{
    ios::sync_with_stdio(false);
    scanf("%d", &t);
    while (t--) {
        scanf("%d", &n);
        int line_num = 0; //一共有多少条横线
        double x1, y11, x2, y2;
        for (int i = 0; i < n; i++) {
            scanf("%lf%lf%lf%lf", &x1, &y11, &x2, &y2);
            line[line_num].init(x1, x2, y11, 1);
            dif_x[line_num++] = x1;
            line[line_num].init(x1, x2, y2, -1);
            dif_x[line_num++] = x2;
        }

        sort(line, line + line_num);
        sort(dif_x, dif_x + line_num);  //对dif_x去重,要先排个序,这样更方便
        int dif_x_num = unique(dif_x, dif_x + line_num) - dif_x; //dif_x_num表示去重后不同的x坐标的数量
        build_tree(1, 0, dif_x_num - 1);

        double ans = 0.00;
        for (int i = 0; i < line_num - 1; i++) {
            int ll = lower_bound(dif_x, dif_x + dif_x_num, line[i].x1) - dif_x;
            int rr = lower_bound(dif_x, dif_x + dif_x_num, line[i].x2) - dif_x - 1;
            update(1, ll, rr, line[i].flag);
            ans += tree[1].len2 * (line[i + 1].y - line[i].y);
        }
        printf("%.2lf\n", ans + 0.000001);
    }

    return 0;
}