[POJ3277]City Horizon

试题描述

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.

输入

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

输出

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

输入示例

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

输出示例

16

数据规模及约定
见“试题描述

题解

上一题的代码改一改就好了。

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <algorithm>
using namespace std;

int read() {
	int x = 0, f = 1; char c = getchar();
	while(!isdigit(c)){ if(c == ‘-‘) f = -1; c = getchar(); }
	while(isdigit(c)){ x = x * 10 + c - ‘0‘; c = getchar(); }
	return x * f;
}

#define maxn 40010
#define LL long long
int n, cnt, ca, cd;
struct Rec {
	LL x1, y1, x2, y2;
	Rec() {}
	Rec(LL _1, LL _2, LL _3, LL _4): x1(_1), y1(_2), x2(_3), y2(_4) {}
} rs[maxn];
struct Rec_int { int x1, y1, x2, y2; } rsi[maxn];
LL num[maxn<<1], A[maxn<<1], B[maxn<<1];
struct Line {
	int l, r, x;
	Line() {}
	Line(int _1, int _2, int _3): l(_1), r(_2), x(_3) {}
	bool operator < (const Line& t) const { return x < t.x; }
} ad[maxn], de[maxn];

LL sumv[maxn<<3];
int addv[maxn<<3];
void build(int L, int R, int o) {
	if(L == R) {
		sumv[o] = 0;
		addv[o] = 0;
		return ;
	}
	int M = L + R >> 1, lc = o << 1, rc = lc | 1;
	build(L, M, lc); build(M+1, R, rc);
	sumv[o] = 0; addv[o] = 0;
	return ;
}
void update(int L, int R, int o, int ql, int qr, int v) {
	int M = L + R >> 1, lc = o << 1, rc = lc | 1;
	if(ql <= L && R <= qr) {
		addv[o] += v;
		if(addv[o]) sumv[o] = A[R] - A[L-1];
		else if(L == R) sumv[o] = 0;
		else sumv[o] = sumv[lc] + sumv[rc];
		return ;
	}
	if(ql <= M) update(L, M, lc, ql, qr, v);
	if(qr > M) update(M+1, R, rc, ql, qr, v);
	sumv[o] = addv[o] ? A[R] - A[L-1] : sumv[lc] + sumv[rc];
	return ;
}

int main() {
	int kase = 0;
	n = read();
	cnt = 0;
	for(int i = 1; i <= n; i++) {
		int l = read(), r = read(), h = read();
		rs[i] = Rec(l, 0, r, h);
		num[++cnt] = l; num[++cnt] = r;
	}
	sort(num + 1, num + cnt + 1);
	cnt = unique(num + 1, num + cnt + 1) - num - 1;
	int tcnt = cnt;
	for(int i = 1; i < cnt; i++) A[i] = num[i+1] - num[1];
	for(int i = 1; i <= n; i++) {
		rsi[i].x1 = lower_bound(num + 1, num + cnt + 1, rs[i].x1) - num;
		rsi[i].x2 = lower_bound(num + 1, num + cnt + 1, rs[i].x2) - num - 1;
	}
	cnt = 0;
	for(int i = 1; i <= n; i++)
		num[++cnt] = rs[i].y1, num[++cnt] = rs[i].y2;
	sort(num + 1, num + cnt + 1);
	cnt = unique(num + 1, num + cnt + 1) - num - 1;
	for(int i = 1; i < cnt; i++) B[i] = num[i+1] - num[i];
	ca = cd = 0;
	for(int i = 1; i <= n; i++) {
		rsi[i].y1 = lower_bound(num + 1, num + cnt + 1, rs[i].y1) - num;
		rsi[i].y2 = lower_bound(num + 1, num + cnt + 1, rs[i].y2) - num;
		ad[++ca] = Line(rsi[i].x1, rsi[i].x2, rsi[i].y1);
		de[++cd] = Line(rsi[i].x1, rsi[i].x2, rsi[i].y2);
	}

	sort(ad + 1, ad + ca + 1);
	sort(de + 1, de + cd + 1);
	LL ans = 0;
	int ka = 1, kd = 1;
	build(1, tcnt, 1);
	for(int i = 1; i <= cnt; i++) {
		while(ka <= ca && ad[ka].x == i)
			update(1, tcnt, 1, ad[ka].l, ad[ka].r, 1), ka++;
		while(kd <= cd && de[kd].x == i)
			update(1, tcnt, 1, de[kd].l, de[kd].r, -1), kd++;
		if(i < cnt) ans += sumv[1] * B[i];
	}
	printf("%lld\n", ans);

	return 0;
}
时间: 2024-08-25 03:23:55

[POJ3277]City Horizon的相关文章

POJ 3277 City Horizon

City Horizon Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 18555   Accepted: 5114 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 silhouette

xtu数据结构 H. City Horizon

H. City Horizon Time Limit: 2000ms Memory Limit: 65536KB 64-bit integer IO format: %lld      Java class name: Main 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 silh

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

题目地址:POJ 3277 水题..稍微处理一下然后用求面积并的方法求即可. 代码如下: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #include <math.h> #include <ctype.h> #include <queue> #include <

离散化+线段树 POJ 3277 City Horizon

POJ 3277 City Horizon Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 18466 Accepted: 5077 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 silho

poj 3277 City Horizon (线段树 扫描线 矩形面积并)

题目链接 题意: 给一些矩形,给出长和高,其中长是用区间的形式给出的,有些区间有重叠,最后求所有矩形的面积. 分析: 给的区间的范围很大,所以需要离散化,还需要把y坐标去重,不过我试了一下不去重 也不会出错, 所有的区间都能列出来,只是在查找的时候费点事. 给的矩形相当于在同一水平线上的,也就是y1坐标相当于为0,其他的就和 poj 1151 Atlantis 差不多了. 我写的思路是按照矩形面积并的思路写的: 但是还有另一种方法也是挺简单的,就是把给的矩形按照高从小到大排序,然后依次插入线段树

1645: [Usaco2007 Open]City Horizon 城市地平线

1645: [Usaco2007 Open]City Horizon 城市地平线 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 315  Solved: 157[Submit][Status] 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 t

bzoj1645 / P2061 [USACO07OPEN]城市的地平线City Horizon(扫描线)

P2061 [USACO07OPEN]城市的地平线City Horizon 扫描线 扫描线简化版 流程(本题为例): 把一个矩形用两条线段(底端点的坐标,向上长度,添加$or$删除)表示,按横坐标排序 $upd:$本题的底端点坐标简化为$(x,0)$ 蓝后对纵坐标建一棵线段树(本题需要对高度进行离散化). 每次对线段树进行覆盖$or$删除区间操作,顺便统计一下$k=$有多少点被覆盖到 而两次(线段)操作之间的长度为$r=x_{i}-x_{i-1}$ 于是两条线段之间被覆盖的面积即为$k*r$ (

BZOJ1645 [Usaco2007 Open]City Horizon 城市地平线

本来想打线段树的说... 就是把坐标离散化了,然后区间最大求和即可... 后来觉得有点烦的说(silver题就要线段树...),于是看了下usaco的题解,发现了个高端的东西:善用STL里的容器和迭代器就可以了. 以下就是高端程序: 1 /************************************************************** 2 Problem: 1645 3 User: rausen 4 Language: C++ 5 Result: Accepted 6

BZOJ_1654_[Usaco2007 Open]City Horizon 城市地平线_扫描线

Description 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