(概念题)线段树 HOJ 1860 Mars Maps

Mars Maps

My Tags  (Edit)
Source : BOI 2001
Time limit : 10 secMemory limit : 32 M

Submitted : 341, Accepted : 110

In the year 2051, several Mars expeditions have explored different areas of the red planet and produced maps of these areas. Now, the BaSA (Baltic Space Agency) has an ambitious plan: they would like to produce a map of the whole planet. In order to calculate the necessary effort, they need to know the total size of the area for which maps already exist. It is your task to write a program that calculates this area.

Task

Write a program that:

  • reads the description of map shapes from the input,
  • computes the total area covered by the maps,
  • writes the result to the output file.
Input

The input file mar.in starts with a line containing a single integer N (1<=N<=10 000), the number of available maps. Each of the following N lines describes a map. Each of these lines contains four integers x1y1x2 and y2 (0<=x1<x2<=30 0000<=y1<y2<=30 000). The values (x1,y1) and (x2,y2) are the coordinates of, respectively, the bottom-left and the top-right corner of the mapped area. Each map has rectangular shape, and its sides are parallel to the x- and y-axis of the coordinate system.

There are multiple test cases. Process to end of file.

Output

The output should contain one integer A, the total explored area (i.e. the area of the union of all rectangles).

Sample Input

2
10 10 20 20
15 15 25 30

Sample Output

225
(概念题)线段树 HOJ 1860 Mars Maps - 恶魔仁 - 恶魔仁


题意:求矩形的覆盖面积

错了一次,数组开小了,要MAX<<2
这题不用离散也行,坐标范围在0~30000 所以直接建树就行了

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

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

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

void PushUp(int l,int r,int rt)
{
if (cover[rt]) sum[rt] = r-l+1;
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(l,r,rt);
return;
}
int m = (l+r)>>1;
if (L<=m) Update(L,R,c,lson);
if (R>m) Update(L,R,c,rson);
PushUp(l,r,rt);
}

bool cmp(const Seg& s1,const Seg& s2)
{
return s1.h<s2.h;
}
int main()
{
//freopen("mar.in","r",stdin);
int n;
while (scanf("%d",&n)==1)
{
int a,b,c,d;
int m = 0;
int l = inf;
int r = 0;
for (int i = 0 ; i < n ; ++i)
{
scanf("%d%d%d%d",&a,&b,&c,&d);
if (a<l) l = a;
if (c>r) r = c;
seg[m].l = seg[m+1].l = a;
seg[m].r = seg[m+1].r = c;
seg[m].h = b;
seg[m+1].h = d;
seg[m++].cover = 1;
seg[m++].cover = -1;
}
sort(seg,seg+m,cmp);
memset(sum,0,sizeof(sum));
memset(cover,0,sizeof(cover));
int ans = 0;
for (int i = 0 ; i < m-1 ; ++i)
{
Update(seg[i].l,seg[i].r-1,seg[i].cover,l,r,1);
ans += sum[1]*(seg[i+1].h-seg[i].h);
}
printf("%d\n",ans);
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值