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 operator < (Seg a,Seg b)
    {
        return a.h<b.h;
    }
}seg[210];
double _hash[210],sum[10000];
int lz[10000];
void push_up(int rt,int l,int r)
{
    if(lz[rt])
    {
        sum[rt]=_hash[r-1]-_hash[l-1];
    }
    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);
        return ;
    }
    int mid=(l+r)/2;
    update(rt*2,l,mid,ql,qr,v);
    update(rt*2+1,mid,r,ql,qr,v);
    push_up(rt,l,r);
}
int main()
{
    int n,i,j,k=1;
    double x,y,r;
    while(~scanf("%d",&n))
    {
        memset(sum,0,sizeof(sum));
        memset(_hash,0,sizeof(_hash));
        memset(lz,0,sizeof(lz));
        if(!n)break;
        int m=0;
        double ans=0;
        for(i=0;i<n;i++)
        {
            scanf("%lf%lf%lf",&x,&y,&r);
            _hash[i]=x-r;
            _hash[i+n]=x+r;
            seg[i].lx=x-r,seg[i].rx=x+r,seg[i].h=y-r,seg[i].v=1;
            seg[i+n].lx=x-r,seg[i+n].rx=x+r,seg[i+n].h=y+r,seg[i+n].v=-1;
        }
        sort(seg,seg+n*2);
        sort(_hash,_hash+n*2);
        m=unique(_hash,_hash+n*2)-_hash;
        for(i=0;i<n*2;i++)
        {
            int ql=lower_bound(_hash,_hash+m,seg[i].lx)-_hash+1;
            int qr=lower_bound(_hash,_hash+m,seg[i].rx)-_hash+1;
            update(1,1,m,ql,qr,seg[i].v);
            ans+=sum[1]*(seg[i+1].h-seg[i].h);
        }
        printf("%d %.2lf\n",k++,ans);
    }
    return 0;
}

Mobile Phone Coverage


Time Limit: 2 Seconds      Memory Limit: 65536 KB


A mobile phone company ACMICPC (Advanced Cellular, Mobile, and Internet-Connected Phone Corporation) is planning to set up a collection of antennas for mobile phones in a city called
Maxnorm. The company ACMICPC has several collections for locations of antennas as their candidate plans, and now they want to know which collection is the best choice.

For this purpose, they want to develop a computer program to find the coverage of a collection of antenna locations. Each antenna Ai has power ri, corresponding to "radius". Usually,
the coverage region of the antenna may be modeled as a disk centered at the location of the antenna (xi, yi) with radius ri. However, in this city Maxnorm such a coverage region becomes the square [xi - ri, xi + ri] x [yi - ri, yi + ri]. In other words, the
distance between two points (xp, yp) and (xq, yq) is measured by the max norm max{|xp - xq|, |yp - yq|}, or, the L norm, in this city Maxnorm instead of the ordinary Euclidean norm sqrt((xp - xq)^2 + (yp - yq)^2).

As an example, consider the following collection of 3 antennas

4.0 4.0 3.0

5.0 6.0 3.0

5.5 4.5 1.0

depicted in the following figure

where the i-th row represents xi, yi, ri such that (xi, yi) is the position of the i-th antenna and ri is its power. The area of regions of points covered by at least one antenna is 52.00
in this case.

Write a program that finds the area of coverage by a given collection of antenna locations.

Input

The input contains multiple data sets, each representing a collection of antenna locations. A data set is given in the following format.

n

x1, y1, r1

x2, y2, r2

??

xn, yn, rn

The first integer n is the number of antennas, such that 2 <= n <= 100. The coordinate of the i-th antenna is given by (xi, yi), and its power is ri, xi, yi and ri are fractional numbers
between 0 and 200 inclusive.

The end of the input is indicated by a data set with 0 as the value of n.

Output

For each data set, your program should output its sequence number (1 for the first data set, 2 for the second, etc.) and the area of the coverage region. The area should be printed with
two digits to the right of the decimal point, after rounding it to two decimal places.

The sequence number and the area should be printed on the same line with no spaces at the beginning and end of the line. The two numbers should be separated by a space.

Sample Input

3

4.0 4.0 3.0

5.0 6.0 3.0

5.5 4.5 1.0

2

3.0 3.0 3.0

1.5 1.5 1.0

0

Sample Output

1 52.00

2 36.00

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

时间: 2024-10-29 19:07:39

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

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

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

HNU12884_Area Coverage(扫描线/线段树+离散化)

解题报告 题目传送门 题意: 又是求面积并 思路: 又是求面积并,还被坑了,题目明明描述的是int坐标,用了double才过... #include <algorithm> #include <iostream> #include <cstring> #include <cstdio> using namespace std; struct Seg { double lx,rx,h; int v; friend bool operator <(Seg

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

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

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]