洛谷·bzoj·City Horizon 城市的地平线

初见安~这里是两个传送门:洛谷P2061 & bzoj P1645

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.

N个矩形块,交求面积并.

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

Sol

这个题的翻译好奇怪啊……嘛,说白了其实应该是一条数轴上有n条线段,每一条线段会将区间 [l,r)【注意这里是左开右闭!!!】内所有小于h[i] 的点的值改为h[i]。很明显的是,如果我们把修改的值从小到大排个序,就可以直接进行覆盖并且不用考虑对应区间内是否存在值大于h[i]的点了。

那么问题就转化为了:如何完成区间修改和区间查询【或者单点查询】的操作,并且这里的区间修改还不是非常单纯的可以用延迟标记的区间加减。

就算不单纯,也要用线段树!!【大雾】

区间修改的话,乍一看就和区间查询长得差不多就行了。然而这样写的话很容易就挂掉了。

因为有可能你一开始给区间[3,4] 赋值为2,后来又给[4,4] 赋值为4,并且前面的操作还摆在那里,对于后期的查询就很不友好了。所以我们可以这样整——只留下有效操作的值。什么意思?就比如有上面这两次操作需求,我们要给[4,4] 赋值的时候,就提前[3,4] 的值给[3.3] 和 [4,4],再把[3,4] 的值清零即可。这样的话不仅可以保证留下来的值都是正确的,还可以让最后累加答案的时候做到留下来的不重不漏加起来就是所求答案

然后在考虑开线段树的时候还会注意到一个问题——1e9的范围???开4倍可不行。但是n的范围很小,所以还要离散化一下。

上代码【主要看注释部分的核心操作】

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<queue>
#include<set>
#include<map>
#define maxn 40004
using namespace std;
typedef long long ll;
int read() {
	int x = 0, f = 1, ch = getchar();
	while(!isdigit(ch)) {if(ch == '-') f = -1; ch = getchar();}
	while(isdigit(ch)) x = (x << 1) + (x << 3) + ch - '0', ch = getchar();
	return x * f;
}

int n, tot = 0;
struct node {
	int l, r, x;
	bool operator < (const node &a) const {return x < a.x;}
}q[maxn];

set<int> st;
map<int, int> dfn, raw;

int tree[maxn << 4];//这里可能不用开这么大……但是开8倍爆掉了 
bool flag[maxn << 4];
void change(int p, int l, int r, int ls, int rs, int x) {
	if(tree[p]) tree[p << 1] = tree[p << 1 | 1] = tree[p];//如果有值,就传下去 
	
	if(ls <= l && r < rs) {tree[p] = x; return;}//类似于区间查询。直接区间赋值。 
	if(l == r) return;//因为本题是左闭右开所以会有l==r死循环的情况…… 
	int mid = l + r >> 1;
	if(ls <= mid) change(p << 1, l, mid, ls, rs, x);
	if(rs > mid) change(p << 1 | 1, mid + 1, r, ls, rs, x);
	tree[p] = 0;//下传后清空 
}

ll ans = 0;
void ask(int p, int l, int r) {
	if(tree[p]) {ans += (raw[r + 1] - raw[l]) * 1ll * tree[p]; return;} 
	//找到值了,ans累加。为什么是r+1的位置减去l的位置, 读者可以画个数轴思考一下 。 
	if(!tree[p] && l == r) return;//防止意外死循环 
	int mid = l + r >> 1;
	ask(p << 1, l, mid);
	ask(p << 1 | 1, mid + 1, r);
}

signed main() {
	n = read();
	for(int i = 1; i <= n; i++) q[i].l = read(), q[i].r = read(), q[i].x = read(), st.insert(q[i].l), st.insert(q[i].r);
	
	sort(q + 1, q + 1 + n);
	
	for(set<int>::iterator it = st.begin(); it != st.end(); it++) {
		dfn[*it] = ++tot; raw[tot] = (*it);//这里是离散化 
	}
	raw[tot + 1] = raw[tot] + 1;//最后最好加一个点 
	
	for(int i = 1; i <= n; i++) {
		change(1, 1, tot, dfn[q[i].l], dfn[q[i].r], q[i].x);//操作 
	}
	
	ask(1, 1, tot);
	printf("%lld\n", ans);
	return 0;
}

迎评:)
——End——

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值