• HDU 3265/POJ 3832 Posters(扫描线+线段树)(2009 Asia Ningbo Regional)


    Description

    Ted has a new house with a huge window. In this big summer, Ted decides to decorate the window with some posters to prevent the glare outside. All things that Ted can find are rectangle posters. 
    However, Ted is such a picky guy that in every poster he finds something ugly. So before he pastes a poster on the window, he cuts a rectangular hole on that poster to remove the ugly part. Ted is also a careless guy so that some of the pasted posters may overlap when he pastes them on the window. 
    Ted wants to know the total area of the window covered by posters. Now it is your job to figure it out. 
    To make your job easier, we assume that the window is a rectangle located in a rectangular coordinate system. The window’s bottom-left corner is at position (0, 0) and top-right corner is at position (50000, 50000). The edges of the window, the edges of the posters and the edges of the holes on the posters are all parallel with the coordinate axes. 
     

    Input

    The input contains several test cases. For each test case, the first line contains a single integer N (0<N<=50000), representing the total number of posters. Each of the following N lines contains 8 integers x1, y1, x2, y2, x3, y3, x4, y4, showing details about one poster. (x1, y1) is the coordinates of the poster’s bottom-left corner, and (x2, y2) is the coordinates of the poster’s top-right corner. (x3, y3) is the coordinates of the hole’s bottom-left corner, while (x4, y4) is the coordinates of the hole’s top-right corner. It is guaranteed that 0<=xi, yi<=50000(i=1…4) and x1<=x3<x4<=x2, y1<=y3<y4<=y2. 
    The input ends with a line of single zero. 
     

    Output

    For each test case, output a single line with the total area of window covered by posters.

    题目大意:墙上有n张长方形的纸,每张纸都被挖掉了一个长方形(有可能贴着原长方形的边界),给这n个长方形的坐标及被挖掉的长方形的坐标,问这些纸一共覆盖了多少面积。

    思路:把每个被挖掉一块的长方形分成4块或以下的真·长方形(怎么分随便你能AC就行),然后就是普通的扫描线+线段树的问题了。至于离散化,点数和数据范围一样大就省了。至于初始化线段树我想应该是不用的,如果代码是对的理论上来讲每做完一组数据,线段树都是空的才对。

    代码(HDU 484MS/POJ 532MS):

     1 #include <cstdio>
     2 #include <algorithm>
     3 #include <cstring>
     4 #include <iostream>
     5 using namespace std;
     6 typedef long long LL;
     7 
     8 const int MAXN = 50010;
     9 
    10 struct Line {
    11     int y, x_st, x_ed, flag;
    12     Line() {}
    13     Line(int y, int x_st, int x_ed, int flag):
    14         y(y), x_st(x_st), x_ed(x_ed), flag(flag) {}
    15     bool operator < (const Line &rhs) const {
    16         return y > rhs.y;
    17     }
    18 };
    19 
    20 Line a[MAXN * 8];
    21 int tree[MAXN * 4], sum[MAXN * 4];
    22 LL ans;
    23 
    24 void update(int x, int l, int r, int tl, int tr, int t) {
    25     int lc = x << 1, rc = lc ^ 1;
    26     if(tl <= l && r <= tr) {
    27         tree[x] += t;
    28         if(tree[x] > 0) sum[x] = r - l;
    29         else if(r - l == 1) sum[x] = 0;
    30         else sum[x] = sum[lc] + sum[rc];
    31     }
    32     else {
    33         int mid = (l + r) >> 1;
    34         if(tl < mid) update(lc, l, mid, tl, tr, t);
    35         if(tr > mid) update(rc, mid, r, tl, tr, t);
    36         if(tree[x] == 0) sum[x] = sum[lc] + sum[rc];
    37         else sum[x] = r - l;
    38     }
    39 }
    40 
    41 void solve(int n) {
    42     sort(a, a + n);
    43     ans = 0;
    44     for(int i = 0; i < n; ++i) {
    45         if(i > 0) ans += (a[i - 1].y - a[i].y) * LL(sum[1]);
    46         update(1, 0, 50000, a[i].x_st, a[i].x_ed, a[i].flag);
    47     }
    48 }
    49 
    50 int main() {
    51     int n, x[5], y[5];
    52     while(scanf("%d", &n) != EOF) {
    53         if(n == 0) break;
    54         int cnt = 0;
    55         for(int i = 1; i <= n; ++i) {
    56             for(int j = 1; j <= 4; ++j) scanf("%d%d", &x[j], &y[j]);
    57             if(x[1] != x[3]) {
    58                 a[cnt++] = Line(y[2], x[1], x[3], 1);
    59                 a[cnt++] = Line(y[1], x[1], x[3], -1);
    60             }
    61             if(x[2] != x[4]) {
    62                 a[cnt++] = Line(y[2], x[4], x[2], 1);
    63                 a[cnt++] = Line(y[1], x[4], x[2], -1);
    64             }
    65             if(y[1] != y[3]) {
    66                 a[cnt++] = Line(y[3], x[3], x[4], 1);
    67                 a[cnt++] = Line(y[1], x[3], x[4], -1);
    68             }
    69             if(y[2] != y[4]) {
    70                 a[cnt++] = Line(y[2], x[3], x[4], 1);
    71                 a[cnt++] = Line(y[4], x[3], x[4], -1);
    72             }
    73         }
    74         solve(cnt);
    75         cout<<ans<<endl;
    76     }
    77 }
    View Code
  • 相关阅读:
    vmware esxi 过期,激活
    mysql 导入csv文件
    定时登录下载sftp服务器上的某些有规则的文件
    修改tmp的临时目录的地址
    jetty权威指南
    将linux下的rm命令改造成mv到指定的目录下
    mysql 5.6 grant授权的时候出现问题
    Keras vs. PyTorch
    Visualizing LSTM Layer with t-sne in Neural Networks
    keras Lambda 层
  • 原文地址:https://www.cnblogs.com/oyking/p/3264814.html
Copyright © 2020-2023  润新知