codeforces#337 D. Vika and Segments

D. Vika and Segments(离散+扫描+线段数)

                time limit per test2 seconds
            memory limit per test256 megabytes
                    inputstandard input
                    outputstandard output

Vika has an infinite sheet of squared paper. Initially all squares are white. She introduced a two-dimensional coordinate system on this sheet and drew n black horizontal and vertical segments parallel to the coordinate axes. All segments have width equal to 1 square, that means every segment occupy some set of neighbouring squares situated in one row or one column.

Your task is to calculate the number of painted cells. If a cell was painted more than once, it should be calculated exactly once.

Input
The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the number of segments drawn by Vika.

Each of the next n lines contains four integers x1, y1, x2 and y2 ( - 109 ≤ x1, y1, x2, y2 ≤ 109) — the coordinates of the endpoints of the segments drawn by Vika. It is guaranteed that all the segments are parallel to coordinate axes. Segments may touch, overlap and even completely coincide.

Output
Print the number of cells painted by Vika. If a cell was painted more than once, it should be calculated exactly once in the answer.

Sample test(s)
input
3
0 1 2 1
1 4 1 2
0 3 2 3
output
8
input
4
-2 -1 2 -1
2 1 -2 1
-1 -2 -1 2
1 2 1 -2
output
16
Note
In the first sample Vika will paint squares (0, 1), (1, 1), (2, 1), (1, 2), (1, 3), (1, 4), (0, 3) and (2, 3).
题目大意:给出一定数量的线段,且这些线段的宽度为1(即为矩形),求这些线段包括的总点数(即这些矩形的总面积)。
解题思路:
1、修改每条线段最小的x坐标和y坐标,将其减一,变成求这些矩形的总面积。
2、每个矩形可以由两条水平线段表示,上水平线和下水平线进行存储(并标记1为下水平线,-1为上水平线)。
3、将这些水平线用其所处的高度进行从底端到顶端排序。
4、并用集合set记录这些水平线左右两个端点的坐标,进行排序。
5、用线段树表示每两个x坐标之间存在点的个数,并用cover数组记录是否两个x坐标之间被覆盖的次数进行更新。
6、开始对水平线从底端到顶端进行扫描,维持线段树。
7、当为下水平线时,就将其覆盖的两个x坐标之间的数组覆盖次数加一,并对其点数进行更新。当为上水平线时,就将其覆盖的两个x坐标之间的数组覆盖次数减一,并对其点数进行更新。算出每层高度之间的总面积并进行相加求出总点数。
总结:
用线段树存储的好处在于每层高度之间记录总的水平线长度总是最大的,并不受当前水平线影响,之前覆盖过的水平线段仍能发挥其作用直到其对应的上水平线出现才失去作用,就算有水平线重叠也只计算一次。

#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int maxn = 100010;
struct lines {
    int x1;
    int x2;
    int h;
    int flag;
    friend bool operator< (lines a, lines b) {
        return a.h < b.h;
    }
};
long long cover[maxn<<4], sum[maxn<<4];
int w[maxn<<2], tot = 0, lenx = 0;
lines edges[maxn<<2];
void add_edge(int x, int x1, int h, int cover) { //存储每个矩形的水平线
    edges[tot].x1 = x;
    edges[tot].x2 = x1;
    edges[tot].h = h;
    edges[tot++].flag = cover;
}
void unite() { //合并重合的x坐标
    int k = lenx;
    lenx = 1;
    for (int i = 1; i < k; i++) {
        if (w[i] != w[i-1]) {
            w[lenx++] = w[i];
        }
    }
}
int binary_find(int x) { //查找对应的x坐标,返回下标
    int l = 0, r = lenx;
    while (l <= r) {
        int mid = (l+r)/2;
        if (w[mid] == x)
            return mid;
        else if (w[mid] < x)
            l = mid+1;
        else
            r = mid-1;
    }
    return -1;
}
void maintain(int l, int r, int root) {
    if (cover[root] > 0) {
        sum[root] = w[r] - w[l];
    } else if (l == r-1) {
        sum[root] = 0;
    } else {
        sum[root] = sum[root<<1] + sum[root<<1 | 1];
    }
}
void query(int L, int R, int flag, int l, int r, int root) {
    if (L <= l && r <= R) {
        cover[root] += flag;
        maintain(l, r, root);
        return;
    } else if (r-1 == l) {
        return;
    }
    int mid = (l+r)/2;
    if (mid >= L) query(L, R, flag, l, mid, root << 1);
    if (mid < R) query(L, R, flag, mid, r, (root << 1 | 1));
    //正常的线段树传递的是mid+1,而这里结合实际意义需要传递mid,防止mid和mid+1之间有点数存在却无法存储之间的点数
    maintain(l, r, root);
}
int main() {
    int n, x1, x2, y1, y2;
    scanf("%d", &n);
    for (int i = 0;i < n; i++) {
        scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
        if (x2 < x1 || y2 < y1) {
            swap(x1, x2);
            swap(y1, y2);
        }
        x1--;
        y1--;//将宽度变为1,点数增加1,计算方便
        add_edge(x1, x2, y1, 1); // 上水平线赋值为1
        add_edge(x1, x2, y2, -1);  //下水平线赋值为-1
        w[lenx++] = x1;
        w[lenx++] = x2;
    }
    memset(cover, 0, sizeof(cover));
    memset(sum, 0, sizeof(sum));
    sort(w, w+lenx);//进行排序,用线段树对其每两个x坐标之间存在的点数进行更新
    unite();
    sort(edges, edges+tot);
    long long ans = 0;
    for (int i = 0; i < tot-1; i++) {
        int L = binary_find(edges[i].x1);
        int R = binary_find(edges[i].x2);
        query(L, R, edges[i].flag, 0, lenx-1, 1);//更新线段树
        ans += (edges[i+1].h - edges[i].h) * sum[1];//计算每层高度之间的总面积
    }
    printf("%lld\n", ans);
}

你可能感兴趣的:(codeforces)