zoukankan      html  css  js  c++  java
  • poj 1151 Atlantis (线段树+扫描线)

    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 file 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 #include<cstdio>
      2 #include<cstring>
      3 #include<algorithm>
      4 using namespace std;
      5 struct p1
      6 {
      7     double x,y1,y2;
      8     int v;
      9 };p1 str[2010];
     10 bool com(p1 a,p1 b)
     11 {
     12     return a.x<b.x;
     13 }
     14 double y[2010];
     15 struct p2
     16 {
     17     int l,r,c;
     18     double s,ll,rr;
     19 };p2 tree[4100];
     20 void build(int l,int r,int p)
     21 {
     22     tree[p].l=l;tree[p].r=r;
     23     tree[p].s=0;
     24     tree[p].c=0;
     25     tree[p].ll=y[l];
     26     tree[p].rr=y[r];
     27     if (l+1==r) return ;
     28     int m=(l+r)/2;
     29     build(l,m,p*2);
     30     build(m,r,p*2+1);
     31 }
     32 void pushup(int p)
     33 {
     34     if (tree[p].c>0)
     35     {
     36         tree[p].s=tree[p].rr-tree[p].ll;
     37         return ;
     38     }
     39     if (tree[p].l+1==tree[p].r) tree[p].s=0;
     40     else tree[p].s=tree[p*2].s+tree[p*2+1].s;
     41 }
     42 void un(int p,p1 e)
     43 {
     44     if (e.y1==tree[p].ll&&e.y2==tree[p].rr)
     45     {
     46         tree[p].c+=e.v;
     47         pushup(p);
     48         return ;
     49     }
     50     if (e.y2<=tree[p*2].rr) un(p*2,e);
     51     else if (e.y1>=tree[p*2+1].ll) un(p*2+1,e);
     52     else
     53     {
     54         p1 ee=e;
     55         ee.y2=tree[p*2].rr;
     56         un(p*2,ee);
     57         ee=e;
     58         ee.y1=tree[p*2+1].ll;
     59         un(p*2+1,ee);
     60     }
     61     pushup(p);
     62 }
     63 int main()
     64 {
     65     int n,i,j,k=1,t;
     66     double x1,x2,y1,y2,ans;
     67     while (~scanf("%d",&n))
     68     {
     69         t=1;
     70         if (n==0) break;
     71         for (i=1;i<=n;i++)
     72         {
     73             scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
     74             str[t].x=x1;
     75             str[t].y1=y1;
     76             str[t].y2=y2;
     77             str[t].v=1;
     78             y[t]=y1;
     79             t++;
     80             str[t].x=x2;
     81             str[t].y1=y1;
     82             str[t].y2=y2;
     83             str[t].v=-1;
     84             y[t]=y2;
     85             t++;
     86         }
     87         sort(str+1,str+t,com);
     88         sort(y+1,y+t);
     89         build(1,t-1,1);
     90          ans=0.0;
     91         un(1,str[1]);
     92         for (i=2;i<t;i++)
     93         {
     94             ans+=tree[1].s*(str[i].x-str[i-1].x);
     95             un(1,str[i]);
     96         }
     97         printf("Test case #%d
    ",k);
     98         printf("Total explored area: %.2lf
    
    ",ans);
     99         k++;
    100     }
    101 }
  • 相关阅读:
    js 截取指定的字符串
    WebSocket实例 Vue中使用websoket
    Vue 水半球样式、圆形水进度条、在线编辑
    Elasticsearch学习笔记3 -- 文档操作
    Elasticsearch学习笔记2 -- 索引库的操作
    Elasticsearch学习笔记1 -- 安装elasticsearch
    Docker学习笔记5 -- Docker-compose
    Docker学习笔记4 -- Dockerfile
    Docker学习笔记3 -- 数据卷
    Docker学习笔记2 -- 常用命令
  • 原文地址:https://www.cnblogs.com/pblr/p/4731999.html
Copyright © 2011-2022 走看看