zoukankan      html  css  js  c++  java
  • POJ1151(线段树+扫描线)

    Atlantis

    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 21410   Accepted: 8070

    Description

    There are several ancient Greek texts that contain descriptions of the fabled island Atlantis. Some of these texts even include maps of parts of the island. But unfortunately, these maps describe different regions of Atlantis. Your friend Bill has to know the total area for which maps exist. You (unwisely) volunteered to write a program that calculates this quantity.

    Input

    The input consists of several test cases. Each test case starts with a line containing a single integer n (1 <= n <= 100) of available maps. The n following lines describe one map each. Each of these lines contains four numbers x1;y1;x2;y2 (0 <= x1 < x2 <= 100000;0 <= y1 < y2 <= 100000), not necessarily integers. The values (x1; y1) and (x2;y2) are the coordinates of the top-left resp. bottom-right corner of the mapped area. 
    The input file is terminated by a line containing a single 0. Don't process it.

    Output

    For each test case, your program should output one section. The first line of each section must be "Test case #k", where k is the number of the test case (starting with 1). The second one must be "Total explored area: a", where a is the total explored area (i.e. the area of the union of all rectangles in this test case), printed exact to two digits to the right of the decimal point. 
    Output a blank line after each test case.

    Sample Input

    2
    10 10 20 20
    15 15 25 25.5
    0

    Sample Output

    Test case #1
    Total explored area: 180.00 
     1 //2016.8.18
     2 #include<iostream>
     3 #include<cstdio>
     4 #include<algorithm>
     5 #include<cstring>
     6 #define N 405
     7 #define lson (id<<1)
     8 #define rson ((id<<1)|1)
     9 #define mid ((r+l)>>1)
    10 
    11 using namespace std;
    12 
    13 struct node
    14 {
    15     double l, r, y;
    16     int fg;
    17     bool operator<(node a) const
    18     {
    19         return y < a.y;
    20     }
    21 }line[N*5];
    22 int n, tim[N<<2];
    23 double x[N<<2], sum[N<<2];
    24 
    25 int bsearch(double a)
    26 {
    27     int l = 0, r = n+1;
    28     while(r-l>1)
    29     {
    30         if(x[mid]<=a)l = mid;
    31         else r = mid;
    32     }
    33     return l;
    34 }
    35 
    36 void update(int a, int b, int l, int r, int id)
    37 {
    38     if(l==r)
    39     {
    40         tim[a] += b;
    41         if(tim[a])sum[id] = x[a+1]-x[a];
    42         else sum[id] = 0;
    43         return ;
    44     }
    45     if(a<=mid) update(a, b, l, mid, lson);
    46     else update(a, b, mid+1, r, rson);
    47     sum[id] = sum[rson]+sum[lson];
    48     return ;
    49 }
    50 
    51 int main()
    52 {
    53     int cnt, kase = 0;
    54     double ans = 0, x1, x2, y1, y2;
    55     while(cin>>n&&n)
    56     {
    57         cnt = 0; ans = 0;
    58         for(int i = 1; i <= n; i++)
    59         {
    60             scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2);
    61             int a = i*2-1, b = i*2;
    62             line[a].y=y1, line[a].l=x1, line[a].r=x2, line[a].fg=1;
    63             line[b].y=y2, line[b].l=x1, line[b].r=x2, line[b].fg=-1;
    64             x[++cnt] = x1, x[++cnt] = x2;
    65         }
    66         n*=2;
    67         sort(x+1, x+1+cnt);
    68         sort(line+1, line+1+n);
    69         memset(sum, 0, sizeof(sum));
    70         memset(tim, 0, sizeof(tim));
    71         for(int i = 1; i <= n; i++)
    72         {
    73             ans += sum[1]*(line[i].y-line[i-1].y);
    74             int l, r;
    75             l = bsearch(line[i].l);
    76             r = bsearch(line[i].r)-1;
    77             for(int j = l; j <= r; j++)
    78                   update(j, line[i].fg, 1, n-1, 1);
    79         }
    80         printf("Test case #%d
    ", ++kase);
    81         printf("Total explored area: %.2f
    
    ", ans);
    82     }
    83 
    84     return 0; 
    85 }
  • 相关阅读:
    3.STM32复位系统
    3.CM3内核架构-寄存器
    2.STM32启动文件
    java线程池
    java多线程
    动态规划(dynamic programming)(二、最优子问题与重叠子问题,以及与贪心的区别)
    SOAP协议
    动态规划(dynamic programming)(一、简介,举例)
    红黑树-RBT(二、基本操作之插入)
    红黑树-RBT(二、基本操作之左旋)
  • 原文地址:https://www.cnblogs.com/Penn000/p/5784414.html
Copyright © 2011-2022 走看看