首页 > 代码库 > ZOJ1659_Mobile Phone Coverage(扫描线/线段树+离散)
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; }
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