POJ 题目3277 City Horizon(线段树+离散化+扫描线)

本文介绍了一个计算城市建筑轮廓总面积的问题,通过输入每栋建筑物的位置和高度,利用离散化和线段树等数据结构和算法,高效求解重叠部分并最终得出所有建筑物轮廓所覆盖的总面积。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

City Horizon
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 17146 Accepted: 4668

Description

Farmer John has taken his cows on a trip to the city! As the sun sets, the cows gaze at the city horizon and observe the beautiful silhouettes formed by the rectangular buildings.

The entire horizon is represented by a number line with N (1 ≤ N ≤ 40,000) buildings. Building i's silhouette has a base that spans locations Ai through Bi along the horizon (1 ≤ Ai < Bi ≤ 1,000,000,000) and has height Hi (1 ≤ Hi ≤ 1,000,000,000). Determine the area, in square units, of the aggregate silhouette formed by all N buildings.

Input

Line 1: A single integer: N
Lines 2.. N+1: Input line i+1 describes building i with three space-separated integers: Ai, Bi, and Hi

Output

Line 1: The total area, in square units, of the silhouettes formed by all N buildings

Sample Input

4
2 5 1
9 10 4
6 8 2
4 6 3

Sample Output

16

Hint

The first building overlaps with the fourth building for an area of 1 square unit, so the total area is just 3*1 + 1*4 + 2*2 + 2*3 - 1 = 16.

Source

USACO 2007 Open Silver

数组开小wa了,几次,n=4000,离散化8000,线段树最少3倍啊

ac代码

Problem: 3277  User: kxh1995 
Memory: 7748K  Time: 735MS 
Language: C++  Result: Accepted 

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<algorithm>
using namespace std;
struct s
{
	__int64 x1,x2,y;
	int flag;
}a[380080];
__int64 hash[380050],sum[380050];
int col[380050];
int cmp(s a,s b)
{
	return a.y<b.y;
}
__int64 bseach(__int64 key,__int64 n)
{
	__int64 l=1,r=n;
	while(l<=r)
	{
		int mid=(l+r)>>1;
		if(hash[mid]==key)
			return mid;
		if(hash[mid]<key)
			l=mid+1;
		else
			r=mid-1;
	}
	return l;
}
void pushup(__int64 tr,__int64 l,__int64 r)
{
	if(col[tr])
		sum[tr]=hash[r+1]-hash[l];
	else
		if(l==r)
			sum[tr]=0;
		else
			sum[tr]=sum[tr<<1]+sum[tr<<1|1];
}
void update(__int64 L,__int64 R,__int64 l,__int64 r,__int64 tr,__int64 flag)
{
	if(L<=l&&r<=R)
	{
		col[tr]+=flag;
		pushup(tr,l,r);
		return;
	}
	int mid=(l+r)>>1;
	if(L<=mid)
		update(L,R,l,mid,tr<<1,flag);
	if(R>mid)
		update(L,R,mid+1,r,tr<<1|1,flag);
	pushup(tr,l,r);
}
int main()
{
	__int64 n;
	while(scanf("%I64d",&n)!=EOF)
	{
		memset(col,0,sizeof(col));
		memset(sum,0,sizeof(sum));
		__int64 x1,x2,y;
		__int64 i,k=1;
		for(i=1;i<=n;i++)
		{
			scanf("%I64d%I64d%I64d",&x1,&x2,&y);
			a[k].x1=x1;
			a[k].x2=x2;
			a[k].y=1;
			a[k].flag=1;
			hash[k++]=x1;
			a[k].x1=x1;
			a[k].x2=x2;
			a[k].y=y+1;
			a[k].flag=-1;
			hash[k++]=x2;
		}
		sort(a+1,a+k,cmp);
		sort(hash+1,hash+k);
		__int64 ans=0;
		for(i=1;i<k-1;i++)
		{
			__int64 x,y;
			x=bseach(a[i].x1,k-1);
			y=bseach(a[i].x2,k-1)-1;
			if(x<=y)
				update(x,y,1,k-1,1,a[i].flag);
			ans+=sum[1]*(a[i+1].y-a[i].y);
		}
		printf("%I64d\n",ans);
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值