HDU1542_Atlantis(扫描线/线段树+离散)

解题报告

题目传送门

题意:

求矩形并面积。

思路:

离散+线段树+扫描线。

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>

using namespace std;
struct Seg {
    int v;
    double h,lx,rx;
    friend bool operator < (Seg a,Seg b) {
        return a.h<b.h;
    }
} seg[4000];
struct R {
    double lx,ly,rx,ry;
} re[1100];
int lz[401000];
double _hash[101000],sum[401000];
void push_up(int rt,int l,int r) {
    if(lz[rt])sum[rt]=_hash[r+1]-_hash[l];
    else if(l==r)sum[rt]=0;
    else
        sum[rt]=sum[rt*2]+sum[rt*2+1];
}
void update(int rt,int l,int r,int ql,int qr,int v) {
    if(ql>r||qr<l)return;
    if(ql<=l&&r<=qr) {
        lz[rt]+=v;
        push_up(rt,l,r);
        //sum[rt]+=_hash[r+1]-_hash[l];
        return ;
    }
    int mid=(l+r)/2;
    update(rt*2,l,mid,ql,qr,v);
    update(rt*2+1,mid+1,r,ql,qr,v);
    push_up(rt,l,r);
}
int main() {
    int n,i,j,k=1;
    while(~scanf("%d",&n)) {
        if(!n)break;
        int cnt=0;
        memset(lz,0,sizeof(lz));
        memset(sum,0,sizeof(sum));
        double ans=0;
        for(i=0; i<n; i++) {
            scanf("%lf%lf%lf%lf",&re[i].lx,&re[i].ly,&re[i].rx,&re[i].ry);
            _hash[i]=re[i].lx;
            _hash[i+n]=re[i].rx;
            seg[cnt].h=re[i].ly;
            seg[cnt].lx=re[i].lx;
            seg[cnt].rx=re[i].rx;
            seg[cnt++].v=1;
            seg[cnt].h=re[i].ry;
            seg[cnt].lx=re[i].lx;
            seg[cnt].rx=re[i].rx;
            seg[cnt++].v=-1;
        }
        sort(seg,seg+cnt);
        sort(_hash,_hash+n*2);
        int m=unique(_hash,_hash+n*2)-_hash;
        for(i=0; i<cnt-1; i++) {
            int l=lower_bound(_hash,_hash+m,seg[i].lx)-_hash;
            int r=lower_bound(_hash,_hash+m,seg[i].rx)-_hash-1;
            update(1,0,m-1,l,r,seg[i].v);
            ans+=sum[1]*(seg[i+1].h-seg[i].h);
        }
        printf("Test case #%d\nTotal explored area: ",k++);
        printf("%.2lf\n",ans);
        printf("\n");
    }
    return 0;
}

Atlantis

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 6942    Accepted Submission(s): 3046

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 

Source

Mid-Central European Regional Contest 2000

HDU1542_Atlantis(扫描线/线段树+离散)

时间: 2024-09-29 18:22:25

HDU1542_Atlantis(扫描线/线段树+离散)的相关文章

hdu1542 Atlantis(扫描线+线段树+离散)矩形相交面积

题目链接:点击打开链接 题目描写叙述:给定一些矩形,求这些矩形的总面积.假设有重叠.仅仅算一次 解题思路:扫描线+线段树+离散(代码从上往下扫描) 代码: #include<cstdio> #include <algorithm> #define MAXN 110 #define LL ((rt<<1)+1) #define RR ((rt<<1)+2) using namespace std; int n; struct segment{ double l

POJ训练计划1177_Picture(扫描线/线段树+离散)

解题报告 题意: 求矩形周长和. 思路: 左扫上扫,扫过了. #include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #include <cmath> using namespace std; struct Seg { int lx,rx,ly,ry,h,v; friend bool operator < (Seg a,Seg b) { re

HDU1255_覆盖的面积(扫描线/线段树+离散)

解题报告 题目传送门 题意: 求面积交. 思路: 不会呀. 只知道线段树应该维护覆盖数大于2的线段长度. 不会更新,看了别人写的理解的,太菜了. 用sum1和sum2分别来表示覆盖数为1的区间长度和覆盖数为2的区间长度. 更新时即要更新sum1也要更新sum2: 区间如果被覆盖 sum1为实际区间长度,如果覆盖一次,sum2为左右子树的sum1和,覆盖两次就为实际区间长度. 没有被覆盖就直接等于左右子树的和. #include <algorithm> #include <iostream

ZOJ1659_Mobile Phone Coverage(扫描线/线段树+离散)

解题报告 题目传送门 题意: 求矩形面积并 思路: 扫描线+线段树.要离散化,坐标是浮点型的. 对于线段树(区间)与点坐标对应起来可以这样 区间[1,4]对应的线段树. #include <algorithm> #include <iostream> #include <cstring> #include <cstdio> using namespace std; struct Seg { int v; double lx,rx,h; friend bool

POJ 1151 Atlantis 扫描线+线段树

点击打开链接 Atlantis Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 17252   Accepted: 6567 Description There are several ancient Greek texts that contain descriptions of the fabled island Atlantis. Some of these texts even include maps of pa

poj 1151 Atlantis (离散化 + 扫描线 + 线段树)

题目链接题意:给定n个矩形,求面积并,分别给矩形左上角的坐标和右上角的坐标. 分析: 1 #include <iostream> 2 #include <cstdio> 3 #include <vector> 4 #include <cstring> 5 #include <cstdlib> 6 #include <algorithm> 7 #define LL __int64 8 #define lson l, mid, 2*rt

HDU3265_Posters(扫描线/线段树)

解题报告 题意: 给定的矩形里面有镂空的矩阵,求矩阵面积并. 思路: 直接把一个图形拆成4个矩形,进行面积并. 扫描线+线段树 #include <algorithm> #include <iostream> #include <cstring> #include <cstdio> #define LL __int64 using namespace std; struct Seg { int lx,rx,h,v; friend bool operator

HDU1377_Counting Squares(扫描线/线段树)

解题报告 题意: 矩形面积并. 思路: 扫描线+线段树 #include <algorithm> #include <iostream> #include <cstring> #include <cstdio> using namespace std; struct Seg { int lx,rx,h,v; friend bool operator < (Seg a,Seg b) { return a.h<b.h; } } seg[500000]

HDU 1255 覆盖的面积 (扫描线 线段树 离散化)

题目链接 题意:中文题意. 分析:纯手敲,与上一道题目很相似,但是刚开始我以为只是把cnt>=0改成cnt>=2就行了,. 但是后来发现当当前加入的线段的范围之前 还有线段的时候就不行了,因为虽然现在都不等于 2,但是之前的那个线段加上现在的已经覆盖2次了. 1 #include <iostream> 2 #include <cstdio> 3 #include <vector> 4 #include <cstring> 5 #include &