zoukankan      html  css  js  c++  java
  • hdu 1542 线段树 求矩形并

                                                   Atlantis

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 5932    Accepted Submission(s): 2599


    Problem 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 <iostream>
     2 #include <algorithm>
     3 #include <stdio.h>
     4 using namespace std;
     5 typedef struct node
     6 {
     7     double x,y1,y2;
     8     int flag;
     9 } node;
    10 typedef struct tree
    11 {
    12     double y1,y2,x;
    13     double cover;
    14     bool flag;
    15 } treee;
    16 node n[400];
    17 treee tree[10000000];
    18 double q[400];
    19 bool cmp(node a,node b)
    20 {
    21     return a.x<b.x;
    22 }
    23 void build(int l,int r,int t)
    24 {
    25     tree[t].cover=0;
    26     tree[t].flag=false;
    27     tree[t].x=-1;
    28     tree[t].y1=q[l];
    29     tree[t].y2=q[r];
    30     if(l+1==r)
    31     {
    32         tree[t].flag=true;
    33         return ;
    34     }
    35     int m=(l+r)>>1;
    36     build(l,m,t<<1);
    37     build(m,r,t<<1|1);
    38 }
    39 double insert(double x,double y1,double y2,int t,int cover)
    40 {
    41     if(tree[t].y1>=y2||tree[t].y2<=y1)
    42     {
    43         return 0;
    44     }
    45     if(tree[t].flag)
    46     {
    47         if(tree[t].cover>0)
    48         {
    49             double sum=0,x1=tree[t].x;
    50             sum=(x-x1)*(tree[t].y2-tree[t].y1);
    51             tree[t].cover+=cover;
    52             tree[t].x=x;
    53             return sum;
    54         }
    55         else
    56         {
    57             tree[t].cover+=cover;
    58             tree[t].x=x;
    59             return 0;
    60         }
    61     }
    62     return insert(x,y1,y2,t<<1,cover)+insert(x,y1,y2,t<<1|1,cover);
    63 }
    64 int main()
    65 {
    66     double x,y,xx,yy;
    67     int m,t,i,ww=1;
    68     while(~scanf("%d",&m)&&m)
    69     {
    70         t=1;
    71         for(i=0; i<m; i++)
    72         {
    73             scanf("%lf%lf%lf%lf",&x,&y,&xx,&yy);
    74             q[t]=y;
    75             n[t].flag=1;
    76             n[t].x=x;
    77             n[t].y1=y;
    78             n[t++].y2=yy;
    79             q[t]=yy;
    80             n[t].flag=-1;
    81             n[t].x=xx;
    82             n[t].y1=y;
    83             n[t++].y2=yy;
    84         }
    85         sort(n+1,n+t,cmp);
    86         sort(q+1,q+t);
    87         build(1,t-1,1);
    88         double sum=0;
    89         for(i=1; i<t; i++)
    90         {
    91             sum+=insert(n[i].x,n[i].y1,n[i].y2,1,n[i].flag);
    92         }
    93         cout<<"Test case #"<<ww++<<endl;
    94         cout<<"Total explored area: ";
    95         printf("%.2lf
    
    ",sum);
    96     }
    97 }
    View Code
     
     
     
  • 相关阅读:
    利用游标循环插入数据
    局部临时表
    表值函数
    SQL2000自动备份压缩删除数据库
    insert select、select into 的用法
    判断每个页面是否登以及捕捉页面异常录解决方案
    合并查询结果
    字符串分隔(转自别处)
    生成行号
    C#计算字符串中子串出现次数的另类方法
  • 原文地址:https://www.cnblogs.com/ERKE/p/3571318.html
Copyright © 2011-2022 走看看