洛谷P2061 [USACO07OPEN]城市的地平线City Horizon

本文介绍了一种利用线段树数据结构解决特定几何问题的方法,即计算由多个矩形建筑组成的天际线轮廓的总面积。通过一系列操作更新线段树,最终求得所有元素之和。

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

题目描述

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.

有一个数列,初始值均为0,他进行N次操作,每次将数列[ai,bi)这个区间中所有比Hi小的数改为Hi,他想知道N次操作后数列中所有元素的和。

输入输出格式

输入格式:

第一行一个整数N,然后有N行,每行三个正整数ai、bi、Hi。

输出格式:

一个数,数列中所有元素的和。

输入输出样例

输入样例#1:
4
2 5 1
9 10 4
6 8 2
4 6 3
输出样例#1:
16

说明

N<=40000 , a、b、k<=10^9 。




线段树省选题。。。

附上丧心病狂的代码:

#include<iostream>
#include<algorithm>
#include<cstdio>
#define LSON rt<<1
#define RSON rt<<1|1
#define DATA(x) a[x].data
#define LSIDE(x) a[x].l
#define RSIDE(x) a[x].r
#define WIDTH(x) (lsh[RSIDE(x)]-lsh[LSIDE(x)])
#define MAXN 40010
using namespace std;
int n,c=1;
long long ans=0,lsh[MAXN<<1];
struct node1{
	long long data;
	int l,r;
}a[MAXN<<3];
struct node2{
	int left,right,h;
}b[MAXN];
inline int read(){
	int date=0,w=1;char c=0;
	while(c<'0'||c>'9'){if(c=='-')w=-1;c=getchar();}
	while(c>='0'&&c<='9'){date=date*10+c-'0';c=getchar();}
	return date*w;
}
bool cmp(const node2 &x,const node2 &y){
	return x.h<y.h;
}
int find(int x){
	int l=1,r=2*n,mid;
	while(l<=r){
		mid=l+r>>1;
		if(lsh[mid]==x)return mid;
		else if(lsh[mid]>x)r=mid-1;
		else l=mid+1;
	}
	return 0;
}
void pushdown(int rt){
	DATA(LSON)=DATA(rt);
	DATA(RSON)=DATA(rt);
	DATA(rt)=0;
}
void buildtree(int l,int r,int rt){
	int mid;
	LSIDE(rt)=l;
	RSIDE(rt)=r;
	DATA(rt)=0;
	if(l==r-1)return;
	mid=l+r>>1;
	buildtree(l,mid,LSON);
	buildtree(mid,r,RSON);
}
void update(int l,int r,long long c,int rt){
	int mid;
	if(l>RSIDE(rt)||LSIDE(rt)>r)return;
	if(l<=LSIDE(rt)&&RSIDE(rt)<=r){
		DATA(rt)=c;
		return;
	}
	if(DATA(rt))pushdown(rt);
	mid=LSIDE(rt)+RSIDE(rt)>>1;
	if(r<=mid)update(l,r,c,LSON);
	else if(mid<=l)update(l,r,c,RSON);
	else{
		update(l,r,c,LSON);
		update(l,r,c,RSON);
	}
}
void query(int rt){
	if(DATA(rt)){
		ans+=WIDTH(rt)*DATA(rt);
		return;
	}
	if(LSIDE(rt)==RSIDE(rt)-1)return;
	query(LSON);
	query(RSON);
}
int main(){
	n=read();
	buildtree(1,n*2,1);
	for(int i=1;i<=n;i++){
		b[i].left=read();b[i].right=read();b[i].h=read();
		lsh[c++]=b[i].left;
		lsh[c++]=b[i].right;
	}
	sort(lsh+1,lsh+2*n+1);
	sort(b+1,b+n+1,cmp);
	for(int i=1;i<=n;i++){
		int x=find(b[i].left),y=find(b[i].right);
		update(x,y,b[i].h,1);
	}
	query(1);
	printf("%lld\n",ans);
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值