zoukankan      html  css  js  c++  java
  • POJ1177 Picture —— 求矩形并的周长 线段树 + 扫描线 + 离散化

    题目链接:https://vjudge.net/problem/POJ-1177

    A number of rectangular posters, photographs and other pictures of the same shape are pasted on a wall. Their sides are all vertical or horizontal. Each rectangle can be partially or totally covered by the others. The length of the boundary of the union of all rectangles is called the perimeter. 

    Write a program to calculate the perimeter. An example with 7 rectangles is shown in Figure 1. 

    The corresponding boundary is the whole set of line segments drawn in Figure 2. 

    The vertices of all rectangles have integer coordinates. 

    Input

    Your program is to read from standard input. The first line contains the number of rectangles pasted on the wall. In each of the subsequent lines, one can find the integer coordinates of the lower left vertex and the upper right vertex of each rectangle. The values of those coordinates are given as ordered pairs consisting of an x-coordinate followed by a y-coordinate. 

    0 <= number of rectangles < 5000 
    All coordinates are in the range [-10000,10000] and any existing rectangle has a positive area.

    Output

    Your program is to write to standard output. The output must contain a single line with a non-negative integer which corresponds to the perimeter for the input rectangles.

    Sample Input

    7
    -15 0 5 10
    -5 8 20 25
    15 -4 24 14
    0 -6 16 4
    2 15 10 22
    30 10 36 20
    34 0 40 16

    Sample Output

    228

    代码如下:

      1 #include <iostream>
      2 #include <cstdio>
      3 #include <cstring>
      4 #include <cmath>
      5 #include <algorithm>
      6 #include <vector>
      7 #include <queue>
      8 #include <stack>
      9 #include <map>
     10 #include <string>
     11 #include <set>
     12 using namespace std;
     13 typedef long long LL;
     14 const double EPS = 1e-8;
     15 const int INF = 2e9;
     16 const LL LNF = 2e18;
     17 const int MAXN = 1e4+10;
     18 
     19 struct line
     20 {
     21     int le, ri, h;
     22     int id;
     23     bool operator<(const line &a)const{
     24         return h<a.h;
     25     }
     26 }Line[MAXN];
     27 
     28 //X用于离散化横坐标,times为此区间被覆盖的次数,block为有多少块子区间, len为被覆盖的长度
     29 int X[MAXN<<1], times[MAXN<<2], block[MAXN<<2], len[MAXN];
     30 bool usedl[MAXN<<2], usedr[MAXN<<2];
     31 //usedl用于表示区间的左端是否被覆盖, usedr亦如此
     32 
     33 void push_up(int u, int l, int r)
     34 {
     35     if(times[u]>0) //该区间有被覆盖
     36     {
     37         len[u] = X[r] - X[l];
     38         block[u] = 1;
     39         usedl[u] = usedr[u] = true;
     40     }
     41     else    //该区间没有被覆盖
     42     {
     43         if(l+1==r)  //该区间为单位区间
     44         {
     45             len[u] = 0;
     46             block[u] = 0;
     47             usedl[u] = usedr[u] = false;
     48         }
     49         else      //该区间至少包含两个单位区间
     50         {
     51             len[u] = len[u*2] + len[u*2+1];
     52             block[u] = block[u*2] + block[u*2+1];
     53             if(usedr[u*2] && usedl[u*2+1]) //如果左半区间的右端与右半区间的左端均被覆盖,则他们合成一个子区间
     54                 block[u]--;
     55             usedl[u] = usedl[u*2];
     56             usedr[u] = usedr[u*2+1];
     57         }
     58     }
     59 }
     60 
     61 //此种线段树的操作对象为连续型,即最小的元素为长度为1的区间[l,r],其中l和r只代表端点(r-l>=1),用于确定
     62 //区间的位置和长度,l和r本身没有特别的含义。而以往做的什么单点更新之类的,都属于离散型,在l处和r处是有含义的
     63 void add(int u, int l, int r, int x, int y, int v)
     64 {
     65     if(x<=l && r<=y)
     66     {
     67         times[u] += v;
     68         push_up(u, l, r);
     69         return;
     70     }
     71 
     72     int mid = (l+r)>>1;
     73     if(x<=mid-1) add(u*2, l, mid, x, y, v);
     74     if(y>=mid+1) add(u*2+1, mid, r, x, y, v);
     75     push_up(u, l, r);
     76 }
     77 
     78 int main()
     79 {
     80     int n;
     81     while(scanf("%d", &n)!=EOF)
     82     {
     83         for(int i = 1; i<=n; i++)
     84         {
     85             int x1, y1, x2, y2;
     86             scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
     87             Line[i].le = Line[i+n].le = x1;
     88             Line[i].ri = Line[i+n].ri = x2;
     89             Line[i].h = y1; Line[i+n].h = y2;
     90             Line[i].id = 1; Line[i+n].id = -1;
     91             X[i] = x1; X[i+n] = x2;
     92         }
     93 
     94         sort(Line+1, Line+1+2*n);
     95         sort(X+1, X+1+2*n);
     96         int m = unique(X+1, X+1+2*n) - (X+1);
     97 
     98         memset(times, 0, sizeof(times));
     99         memset(len, 0, sizeof(len));
    100         memset(block, 0, sizeof(block));
    101         memset(usedl, false, sizeof(usedl));
    102         memset(usedr, false, sizeof(usedr));
    103 
    104         int ans = 0, pre_len = 0;
    105         Line[2*n+1].h = Line[2*n].h;    //边界条件
    106         for(int i = 1; i<=2*n; i++)
    107         {
    108             int l = upper_bound(X+1, X+1+m, Line[i].le) - (X+1);
    109             int r = upper_bound(X+1, X+1+m, Line[i].ri) - (X+1);
    110             add(1, 1, m, l, r, Line[i].id);
    111             ans += abs(len[1] - pre_len);   //变化的长度即为显露出来的横向边
    112             ans += 2*block[1]*(Line[i+1].h-Line[i].h);  //如果有cnt个连续的区间,那么就有2*cnt条显露出来的纵向边
    113             pre_len = len[1];
    114         }
    115 
    116         printf("%d
    ", ans);
    117     }
    118 }
    View Code
  • 相关阅读:
    Android应用开发提高系列(1)——《Practical Java 中文版》读书笔记(上)
    Android开发指南(35) —— Toast Notifications
    Android开发指南(37) —— Data Backup
    Android开发指南(36) —— Search
    [转]闲话操作系统1
    [从架构到设计]第二回:对象的旅行对象和人,两个世界,一样情怀
    [Thinking]从赢在中国,思考博客园的商业化
    [你必须知道的.NET]目录导航
    070809
    [CLR团队公告]CLR基础研究团队纲领
  • 原文地址:https://www.cnblogs.com/DOLFAMINGO/p/7748018.html
Copyright © 2011-2022 走看看