【poj 3277 bzoj 1645 luogu 2061】[Usaco2007 Open Silver]City Horizon 城市地平线 (线段树)...

[Usaco2007 Open]City Horizon 城市地平线

Time Limit: 5 Sec  Memory Limit: 64 MB

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 A_i through B_i along the horizon (1 <= A_i < B_i <= 1,000,000,000) and has height H_i (1 <= H_i <= 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: A_i, B_i, and H_i

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

OUTPUT DETAILS:

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

Silver

 

【题目大意】

如下 1 所示,在一条水平线上有 N 个建筑物(1 <= N <= 40000),建筑物都是长方形的,且可以互相遮盖。给出每个建筑物的左右坐标值 Ai、Bi (1 <= Ai,Bi <= 109)以及每个建筑物的高度 Hi(1 <= Hi <= 109),需要计算出这些建筑物总共覆盖的面积。

【分析】

只能做做这类SB题了。。。

先按高度排序,然后跑区间替换+区间最大值模板裸的线段树,然后累加计算,AC!!!

用到了之前只听说但不会的unique函数,涨姿势了~

强行学习unique,删除某一段区间内相邻的重复元素,没有这个的话还要多写一些代码,很吼啊~

详情见很挫的代码。

#include<iostream>
#include<fstream>
#include<cstdio>
#include<algorithm>
#include<string>
#include<vector>
#include<queue>
#include<deque>
#include<utility>
#include<map>
#include<set>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<functional>
#include<sstream>
#include<cstring>
#include<bitset>
#include<stack>
using namespace std;

int n,cnt;
struct sdt
{
	int lef,rig,h;
}a[3200005];
int pos[800005],old[800005];
int lef[800005],rig[800005];
long long data[800005],delta[800005];

bool cmp(sdt x,sdt y)
{
	return x.h<y.h;
}

void build(int root,int l,int r)
{
	lef[root]=l;
	rig[root]=r;
	if(l==r)
	{
		return ;
	}
	int mid=(l+r)/2;
	build(root*2,l,mid);
	build(root*2+1,mid+1,r);
}

void check(int root)
{
	if(!delta[root])return ;
	data[root]=delta[root];
	if(lef[root]==rig[root])
	{
		delta[root]=0;
		return ;
	}
	if(delta[root*2]<delta[root])
		delta[root*2]=delta[root];
	if(delta[root*2+1]<delta[root])
		delta[root*2+1]=delta[root];
	delta[root]=0;
}

void add(int root,int l,int r,int x)
{
	check(root);
	if(lef[root]==l && rig[root]==r)
	{
		delta[root]=x;
		return ;
	}
	int mid=(lef[root]+rig[root])/2;
	if(l>mid)add(root*2+1,l,r,x);
	else if(r<=mid)add(root*2,l,r,x);
	else
	{
		add(root*2,l,mid,x);
		add(root*2+1,mid+1,r,x);
	}
	check(root*2);
	check(root*2+1);
	data[root]=max(data[root*2],data[root*2+1]);
}

long long query(int root,int l,int r)
{
	check(root);
	if(lef[root]==l && rig[root]==r)
	{
		return data[root];
	}
	int mid=(lef[root]+rig[root])/2;
	if(l>mid)return query(root*2+1,l,r);
	else if(r<=mid)return query(root*2,l,r);
	else return max(query(root*2,l,mid),query(root*2+1,mid+1,r));
}

int binarysearch(int x)
{
    int L=1,R=cnt;
    while(L<=R)
	{
        int mid=(L+R)>>1;
        if(pos[mid]==x)return mid;
        if(pos[mid]>x)R=mid-1;
		else if(pos[mid]<x)L=mid+1;
    }
}

int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	{
		scanf("%d%d%d",&a[i].lef,&a[i].rig,&a[i].h);
		pos[++cnt]=a[i].lef;
		pos[++cnt]=a[i].rig;
	}
	sort(pos+1,pos+1+cnt);
	cnt=unique(pos+1,pos+cnt+1)-(pos+1);
	//for(int i=1;i<=cnt;i++)
	//cout<<pos[i]<<" ";
	//cout<<endl;
	sort(a+1,a+n+1,cmp);
	build(1,1,cnt);
	for(int i=1;i<=n;i++)
	{
		int xx=binarysearch(a[i].lef);
		int yy=binarysearch(a[i].rig);
		//cout<<xx<<" "<<yy<<endl;
		old[xx]=a[i].lef;
		old[yy]=a[i].rig;
		add(1,xx,yy-1,a[i].h);
	}
	long long ans=0;
	for(int i=1;i<cnt;i++)
	{
		//cout<<i<<" "<<old[i]<<" "<<old[i+1]<<" "<<query(1,i,i)<<endl;
		ans+=query(1,i,i)*(long long)(old[i+1]-old[i]);
	}
	printf("%lld\n",ans);
	return 0;
}

  

转载于:https://www.cnblogs.com/winmt/p/6629098.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值