zoukankan      html  css  js  c++  java
  • Overlapping Rectangles

    There are nnn 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 AAA with the bottom left corner located at (0,0) and the top right corner at (2,2), and the other rectangleB with the bottom left corner located at (1,1) and the top right corner at (3,3), it follows that the area of the union of A and B should be 7, instead of 8.

    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,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 1000. After n, there will be n lines representing the n rectangles; each line contains four integers <a,b,c,d> , which means that the bottom left corner of the rectangle is located at (a,b), and the top right corner of the rectangle is located at (c,d). Note that integers aaa, bbb, ccc, ddd can be as large as 1,000,0001,000,0001,000,000.

    These configurations of rectangles occur repetitively in the input as the pattern described above. An integer n=0n = 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.

    样例输入

    2
    0 0 2 2
    1 1 3 3
    3
    0 0 1 1
    2 2 3 3
    4 4 5 5
    0

    样例输出

    7
    3
    *

    题目来源

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

    线段是之扫描线的魔板题

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 
     4 #define lz 2*u,l,mid
     5 #define rz 2*u+1,mid+1,r
     6 const int N=10000;
     7 int sum[N];
     8 int flag[N];
     9 int X[N];
    10 
    11 struct Node {
    12     int lx, rx, y;
    13     int s;
    14     Node(){};
    15     Node(int lx_, int rx_, int y_, int s_) {
    16         lx = lx_, rx = rx_, y = y_, s = s_;
    17     }
    18     bool operator < (const Node &S) const {
    19         return y < S.y;
    20     }
    21 }line[N];
    22 
    23 int find(int tmp, int n) {
    24     int l = 1, r = n, mid;
    25     while(l <= r) {
    26         mid = (l+r) >> 1;
    27         if(X[mid] == tmp) return mid;
    28         else if(X[mid] < tmp) l = mid+1;
    29         else r = mid-1;
    30     }
    31 }
    32 
    33 void push_up(int u, int l, int r) {
    34     if(flag[u]) sum[u] = X[r+1]-X[l];
    35     else if(l == r) sum[u] = 0;
    36     else sum[u] = sum[2*u] + sum[2*u+1];
    37 }
    38 
    39 void Update(int u, int l, int r, int tl, int tr, int c) {
    40     if(tl <= l&&r <= tr) {
    41         flag[u] += c;
    42         push_up(u,l,r);
    43         return ;
    44     }
    45     int mid = (l + r) >> 1;
    46     if(tr <= mid) Update(lz, tl, tr, c);
    47     else if(tl > mid) Update(rz, tl, tr, c);
    48     else {
    49         Update(lz, tl, mid, c);
    50         Update(rz, mid+1, tr, c);
    51     }
    52     push_up(u,l,r);
    53 }
    54 
    55 int main() {
    56     int n;
    57     while(scanf("%d", &n) && n) {
    58         int num=0;
    59         memset(flag, 0, sizeof(flag));
    60         memset(sum, 0, sizeof(sum));
    61         for(int i = 0; i < n; i ++) {
    62             int x1, x2, y1, y2;
    63             scanf("%d%d%d%d",&x1, &y1, &x2, &y2);
    64             line[++num] = Node(x1,x2,y1,1);
    65             X[num] = x1;
    66             line[++num] = Node(x1,x2,y2,-1);
    67             X[num] = x2;
    68         }
    69         sort(X + 1, X + num + 1);
    70         sort(line + 1,line + num + 1);
    71         int k = 1;
    72         for(int i = 2; i <= num; i ++)
    73             if(X[i] != X[i+1]) X[++k] = X[i];
    74         int ans = 0;
    75         for(int i = 1; i < num; i ++) {
    76             int l = find(line[i].lx, k);
    77             int r = find(line[i].rx, k) - 1;
    78             Update(1, 1, k, l, r, line[i].s);
    79             ans += sum[1] * (line[i+1].y - line[i].y);
    80         }
    81         printf("%d
    ",ans);
    82     }
    83     printf("*
    ");
    84     return 0;
    85 }
  • 相关阅读:
    不自导会专门样
    人撒娇地撒基督教扫ID祭扫我京东is啊单间
    随机生成字符
    阿朵洒洒的撒多撒多撒啊
    死循环
    ui其实比接口好做
    租到房子了
    阿里PTS基础版真的坑
    終于解決调用wordpress 4.3 xmlrpc api 发布包含分类的文章时返回“抱歉,文章类型不支持您的分类法”错误的問題
    诸恶莫作、众善奉行、自净其意、是诸佛教
  • 原文地址:https://www.cnblogs.com/xingkongyihao/p/7588579.html
Copyright © 2011-2022 走看看