Area covered by hdu1255 [scanning line]

Source: Internet
Author: User

Covered Area time limit: 10000/5000 MS (Java/others) memory limit: 65536/32768 K (Java/Others) Total submission (s): 3743 accepted submission (s): 1838

Problem description is a number of rectangles on the plane. The area of the area covered by these rectangles is obtained at least twice.


 
The first line of input data is a positive integer T (1 <= T <= 100), representing the number of test data. the first row of each test data is a positive integer N (1 <= n <= 1000), representing the number of rectangles, followed by N rows of data, each row contains four floating point numbers, it indicates the coordinates of the upper left corner and lower right corner of a rectangle on the plane. The upper and lower sides of the rectangle are parallel to the X axis, and the left and right sides are parallel to the Y axis. the coordinate ranges from 0 to 100000.

Note: This question contains a large amount of input data. We recommend that you use scanf to read data.
 
Output for each group of test data, calculate the area of the area covered by these rectangles at least twice. The result is retained with two decimal places.
 
Sample Input
251 1 4 21 3 3 72 1.5 5 4.53.5 1.25 7.5 46 3 10 730 0 1 11 0 2 12 0 3 1
 
Sample output
7.630.00
Given the coordinates in the lower left corner of some Rectangles and the upper right corner, find the area and number of overlaps between these rectangles twice or more times. Question: This is very similar to poj1151. The main difference lies in the pushup function. Each segment is divided into one-time coverage and multiple-times coverage. Therefore, we need to calculate their length separately so that we can easily find the final area.

#include <stdio.h>#include <string.h>#include <algorithm>#define maxn 2002#define lson l, mid, rt << 1#define rson mid, r, rt << 1 | 1using namespace std;struct Node {    double y1, y2, onceLen, moreLen;    int covers;} T[maxn << 2];struct Node2 {    double x, y1, y2;    int isLeft;} xNode[maxn];double yNode[maxn];bool cmp(Node2 a, Node2 b) {    return a.x < b.x;}void pushUp(int l, int r, int rt) {    if(T[rt].covers > 1) {        T[rt].moreLen = T[rt].y2 - T[rt].y1;        T[rt].onceLen = 0.0;    } else if(T[rt].covers == 1) {        if(r - l == 1) {            T[rt].moreLen = 0.0;            T[rt].onceLen = T[rt].y2 - T[rt].y1;            return;        }        T[rt].moreLen = T[rt << 1].onceLen + T[rt << 1 | 1].onceLen                        + T[rt << 1].moreLen + T[rt << 1 | 1].moreLen;        T[rt].onceLen = T[rt].y2 - T[rt].y1 - T[rt].moreLen;    } else {        if(r - l == 1) {            T[rt].onceLen = T[rt].moreLen = 0.0;            return;        }        T[rt].onceLen = T[rt << 1].onceLen + T[rt << 1 | 1].onceLen;        T[rt].moreLen = T[rt << 1].moreLen + T[rt << 1 | 1].moreLen;    }}void build(int l, int r, int rt) {    T[rt].onceLen = T[rt].moreLen = 0.0;    T[rt].covers = 0;    T[rt].y1 = yNode[l];    T[rt].y2 = yNode[r];    if(r - l == 1) return;    int mid = (l + r) >> 1;    build(lson);    build(rson);}void update(Node2 x, int l, int r, int rt) {    if(x.y1 == T[rt].y1 && x.y2 == T[rt].y2) {        T[rt].covers += x.isLeft;        pushUp(l, r, rt);        return;    }    int mid = (l + r) >> 1;    if(x.y2 <= yNode[mid]) update(x, lson);    else if(x.y1 >= yNode[mid]) update(x, rson);    else {        Node2 x1 = x, x2 = x;        x1.y2 = x2.y1 = yNode[mid];        update(x1, lson);        update(x2, rson);    }    pushUp(l, r, rt);}int main() {    //freopen("stdin.txt", "r", stdin);    int n, i, id, t;    double x1, y1, x2, y2, sum;    scanf("%d", &t);    while(t--) {        scanf("%d", &n);        for(i = id = 0; i < n; ++i) {            scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2);            yNode[id] = y1;            xNode[id].x = x1;            xNode[id].y1 = y1;            xNode[id].y2 = y2;            xNode[id++].isLeft = 1;            yNode[id] = y2;            xNode[id].x = x2;            xNode[id].y1 = y1;            xNode[id].y2 = y2;            xNode[id++].isLeft = -1;        }        sort(yNode, yNode + id);        build(0, id - 1, 1);        sort(xNode, xNode + id, cmp);        for(i = 0, sum = 0.0; i < id - 1; ++i) {            update(xNode[i], 0, id - 1, 1);            sum += T[1].moreLen * (xNode[i + 1].x - xNode[i].x);        }        printf("%.2lf\n", sum);     }    return 0;}


Area covered by hdu1255 [scanning line]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.