[Segment Tree && Scanning Line && Area Intersection] Area Covered by HDU-1255

Problem Dscription

Give you n rectangles, each rectangle gives you the lower left subscript and the lower right upper corner, let you find the area intersection.

Train of thought:

Reference Blog: http://www.cnblogs.com/scau20110726/archive/2013/04/14/3020998.html It is suggested that we do hdu 1542 first and then look at this question. The code was only slightly changed.
The segment tree maintains len2 of cover >= 2, len of cover > 0, and whether the whole segment of the interval is valid or not. Compared with hdu 1542, there is an extra len2. So get_len2() function has changed, specific details refer to the above blog, very detailed.

#include<bits/stdc++.h>
using namespace std;
#define lson root<<1
#define rson root<<1|1
#define N 1005
#define MID int mid = (l + r) / 2
struct node
{
    int cover;
    double len, len2;//There are more len2 in different places.
};
node tree[N<<3];
double yy[N<<2];
struct Node
{
    int cover;
    double x;
    double yd, yu;
    bool operator < (const Node &b) const{
        return x < b.x;
    }
};
Node a[N<<2];
void build(int root, int l, int r)
{
    tree[root].cover = tree[root].len = tree[root].len2 = 0;
    if(l + 1 == r)
        return;
    MID;
    build(lson, l, mid);
    build(rson, mid, r);
}
void get_len2(int root, int l, int r)
{
    if(tree[root].cover)
        tree[root].len = yy[r] - yy[l];
    else if(l + 1 == r)
        tree[root].len = 0;
    else tree[root].len = tree[lson].len + tree[rson].len;
    //Here are some differences
    if(tree[root].cover > 1) tree[root].len2 = yy[r] - yy[l];//Cove >= 2 interval length is valid
    else if(l + 1 == r) tree[root].len2 = 0;//Leaf nodes, and the whole section of the interval is not valid
    else if(tree[root].cover == 1)//With len of left and right children, len 2 will be satisfied.
        tree[root].len2 = tree[lson].len + tree[rson].len;
    else tree[root].len2 = tree[lson].len2 + tree[rson].len2;//The sum of left and right children len2
    //
}
void updata(int root, int l, int r, int ul, int ur, int cover)
{
    if(ul <= l && r <= ur)
    {
        tree[root].cover += cover;
        get_len2(root, l, r);
        return;
    }
    MID;
    if(ul < mid) updata(lson, l, mid, ul, ur, cover);
    if(ur > mid) updata(rson, mid, r, ul, ur, cover);
    get_len2(root, l, r);
}
int main()
{
    int n, cnt, i, T;
    double x1, x2, y1, y2;
    scanf("%d", &T);
    while(T--)
    {
        scanf("%d", &n);
        cnt = 0;
        for(i = 0; i < n; i++)
        {
            scanf("%lf %lf %lf %lf", &x1, &y1, &x2, &y2);
            yy[cnt] = y1;
            a[cnt++] = ((Node){1, x1, y1, y2});
            yy[cnt] = y2;
            a[cnt++] = ((Node){-1, x2, y1, y2});
        }
        sort(yy, yy + cnt);
        n = unique(yy, yy + cnt) - yy;
        build(1, 0, n - 1);
        sort(a, a + cnt);
        int ul, ur;
        double ans = 0;
        for(i = 0; i < cnt - 1; i++)
        {
            ul = lower_bound(yy, yy + n, a[i].yd) - yy;
            ur = lower_bound(yy, yy + n, a[i].yu) - yy;
            updata(1, 0, n - 1, ul, ur, a[i].cover);
            ans += tree[1].len2 * (a[i + 1].x - a[i].x);
        }
        printf("%.2lf\n", ans);
    }
}

Posted by synapp2 on Thu, 14 Feb 2019 01:30:20 -0800