(概念题)线段树 HOJ 1119 Atlantis

Atlantis

Source : ACM ICPC Mid-Central European 2000
Time limit : 3 secMemory limit : 32 M

Submitted : 338, Accepted : 171

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 it1.


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 


题意:求矩形的覆盖面积
做法:对x轴建树,然后从上面到下面插入矩形的上边和下边,进行从上到下扫描。
其中线段树的点需要根据矩形的竖边进行离散化,然后每一点[L,L]代表点L到点L+1这一段区间,用cover代表该区间有没有被覆盖,在加边的时候ans就加上一次加边后覆盖的长度*上一条边与现在将要加的变的距离

代码:
#include<iostream>
#include<string.h>
#include<cstdio>
#include<stdio.h>
#include<map>
#include<algorithm>
#include<vector>
using namespace std;
#define MAX 2000+10
#define MOD 100000000
const int inf = 0x7fffffff;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1

struct Seg
{
double l,r,h;
int cover;
}seg[MAX];

int cover[MAX<<2];
double X[MAX];
double sum[MAX<<2];

void PushUp(int rt,int l,int r)
{
if (cover[rt]) sum[rt] = X[r+1]-X[l];
else if (l==r) sum[rt] = 0;
else sum[rt] = sum[rt<<1] + sum[rt<<1|1];
}

void Update(int L,int R,int c,int l,int r,int rt)
{
if (L<=l && r<=R)
{
cover[rt] += c;
PushUp(rt,l,r);
return;
}
int m = (l+r)>>1;
if (L<=m) Update(L,R,c,lson);
if (R>m) Update(L,R,c,rson);
PushUp(rt,l,r);
}

int Bisearch(int left,int right,double x)
{
int mid = (left+right)>>1;
while (left<=right)
{
if (X[mid]<x-1e-6)
left = mid+1;
else if (X[mid]>x+1e-6)
right = mid-1;
else
return mid;
mid = (left+right)>>1;
}
return -1;
}

bool cmp(const Seg& s1,const Seg& s2)
{
return s1.h<s2.h;
}

int main()
{
int n , t = 0;
while (scanf("%d",&n),n)
{
++t;
double a,b,c,d;
int m = 0;
for (int i = 0 ; i < n ; ++i)
{
scanf("%lf%lf%lf%lf",&a,&b,&c,&d);
X[m] = a;
seg[m].l = seg[m+1].l = a;
seg[m].r = seg[m+1].r = c;
seg[m].h = b;
seg[m].cover = 1;
++m;
X[m] = c;
seg[m].h = d;
seg[m].cover = -1;
++m;
}
sort(X,X+m);
sort(seg,seg+m,cmp);
int mm = 0;
for (int i = 0 ; i < m ; ++i) if (X[i]!=X[i+1])
X[mm++] = X[i];

memset(sum,0,sizeof(sum));
memset(cover,0,sizeof(cover));
double ans = 0;
for (int i = 0 ; i < m-1 ; ++i)
{
int L = Bisearch(0,mm-1,seg[i].l);
int R = Bisearch(0,mm-1,seg[i].r)-1;
Update(L,R,seg[i].cover,0,mm-1,1);
ans += sum[1]*(seg[i+1].h-seg[i].h);
}
printf("Test case #%d\nTotal explored area: %.2lf\n\n",t,ans);
}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值