2017 icpc 南宁赛区 F.Overlapping Rectangles(重叠矩形的最大面积+线段树模板)

来源:互联网 发布:java与xml 第3版 pdf 编辑:程序博客网 时间:2024/06/05 08:19

There are nn rectangles on the plane. The problem is to find the area of the union of these rectangles. Note that these rectangles might overlap with each other, and the overlapped areas of these rectangles shall not be counted more than once. For example, given a rectangle AA with the bottom left corner located at (0, 0)(0,0) and the top right corner at (2, 2)(2,2), and the other rectangle BB with the bottom left corner located at (1,1)(1,1) and the top right corner at (3,3)(3,3), it follows that the area of the union of AA and BB should be 77, instead of 88.

Although the problem looks simple at the first glance, it might take a while to figure out how to do it correctly. Note that the shape of the union can be very complicated, and the intersected areas can be overlapped by more than two rectangles.

Note:

(1) The coordinates of these rectangles are given in integers. So you do not have to worry about the floating point round-off errors. However, these integers can be as large as 1,000,0001,000,000.

(2) To make the problem easier, you do not have to worry about the sum of the areas exceeding the long integer precision. That is, you can assume that the total area does not result in integer overflow.

Input Format

Several sets of rectangles configurations. The inputs are a list of integers. Within each set, the first integer (in a single line) represents the number of rectangles, n, which can be as large as 10001000. After n, there will be n lines representing the n rectangles; each line contains four integers <a, b, c, d><a,b,c,d> , which means that the bottom left corner of the rectangle is located at (a, b)(a,b), and the top right corner of the rectangle is located at (c, d)(c,d). Note that integers aabbccdd can be as large as 1,000,0001,000,000.

These configurations of rectangles occur repetitively in the input as the pattern described above. An integer n = 0n=0 (zero) signifies the end of input.

Output Format

For each set of the rectangles configurations appeared in the input, calculate the total area of the union of the rectangles. Again, these rectangles might overlap each other, and the intersecting areas of these rectangles can only be counted once. Output a single star '*' to signify the end of outputs.

样例输入

20 0 2 21 1 3 330 0 1 12 2 3 34 4 5 50

样例输出

73*

题目来源

2017 ACM-ICPC 亚洲区(南宁赛区)网络赛

【题解】

  给n个矩形,求出这n个矩形的围成的最大面积。

 每给一个矩形坐标,就遍历前i-1个矩形,每次把当前输入的矩行与第k(0<k<=i-1)个矩形的重合面积求出来,然后两个矩形面积和减去重叠部分面积,每次都这样保存结果,维护一个ans就好。


【AC代码】

#include <algorithm>#include <cctype>#include <cmath>#include <cstdio>#include <cstdlib>#include <cstring>#include <iomanip>#include <iostream>#include <map>#include <queue>#include <string>#include <set>#include <vector>using namespace std;#define pr(x) cout << #x << " = " << x << "  "#define prln(x) cout << #x << " = " << x << endlconst int N =5000, INF = 0x3f3f3f3f, MOD = 1e9 + 7;int n;struct Seg {    double l, r, h; int d;    Seg() {}    Seg(double l, double r, double h, int d): l(l), r(r), h(h), d(d) {}    bool operator< (const Seg& rhs) const {return h < rhs.h;}} a[N];int cnt[N << 2];double sum[N << 2], all[N];#define lson l, m, rt << 1#define rson m + 1, r, rt << 1 | 1void push_up(int l, int r, int rt) {    if(cnt[rt]) sum[rt] = all[r + 1] - all[l];    else if(l == r) sum[rt] = 0;    else sum[rt] = sum[rt << 1] + sum[rt << 1 | 1];}void update(int L, int R, int v, int l, int r, int rt) {    if(L <= l && r <= R) {        cnt[rt] += v;        push_up(l, r, rt);        return;    }    int m = l + r >> 1;    if(L <= m) update(L, R, v, lson);    if(R > m) update(L, R, v, rson);    push_up(l, r, rt);}int main() {    int kase = 0;    while(scanf("%d", &n) == 1 && n) {        for(int i = 1; i <= n; ++i) {            double x1, y1, x2, y2;            scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2);            a[i] = Seg(x1, x2, y1, 1);            a[i + n] = Seg(x1, x2, y2, -1);            all[i] = x1; all[i + n] = x2;        }        n <<= 1;        sort(a + 1, a + 1 + n);        sort(all + 1, all + 1 + n);        int m = unique(all + 1, all + 1 + n) - all - 1;        memset(cnt, 0, sizeof cnt);        memset(sum, 0, sizeof sum);        double ans = 0;        for(int i = 1; i < n; ++i) {            int l = lower_bound(all + 1, all + 1 + m, a[i].l) - all;            int r = lower_bound(all + 1, all + 1 + m, a[i].r) - all;            if(l < r) update(l, r - 1, a[i].d, 1, m, 1);            ans += sum[1] * (a[i + 1].h - a[i].h);        }        printf("%lld\n",(long long)ans);    }    printf("*\n");    return 0;}




阅读全文
0 0
原创粉丝点击