题目:
City Horizon
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 17855 | Accepted: 4896 |
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
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
思路:贪心处理,将所有building按照从低到高排序,依次区间更新,就不会存在被覆盖的问题(较高的一定覆盖较低的)。
由于宽度过大,在区间更新前先将其离散化,以方便处理
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <vector>
#include <algorithm>
#define MAXN 200001
long long lazy[2 * MAXN];
long long t[2 * MAXN], a[2 * MAXN];
struct data{
long long be;
long long ed;
long long hig;
}dat[MAXN];
long long table[MAXN];
//long long be[MAXN], ed[MAXN];
//long long hips[2 * MAXN];
//double endhips[4 * MAXN];
//long long maxn;
using namespace std;
void BuildTree(long long l, long long r, long long x){
if (l == r)
{
t[x] = a[l];
return;
}
long long m = (l + r) >> 1;
BuildTree(l, m, x << 1);
BuildTree(m + 1, r, x << 1 | 1);
t[x] = t[x << 1] + t[x << 1 | 1];
return;
}
void PushDown(long long l, long long r, long long x){
long long m = (l + r) >> 1;
if (lazy[x]){
t[x << 1] = lazy[x] * (m - l + 1);
t[x << 1 | 1] = lazy[x] * (r - m);
lazy[x << 1 | 1] = lazy[x];
lazy[x << 1] = lazy[x];
lazy[x] = 0;
}
return;
}
void Modify(long long pos, long long val, long long l, long long r, long long x){
if (pos == l && r == l)
{
t[x] = val;
return;
}
long long m = (l + r) >> 1;
if (pos <= m)
{
Modify(pos, val, l, m, x << 1);
}
else Modify(pos, val, m + 1, r, x << 1 | 1);
t[x] = t[x << 1] + t[x << 1 | 1];
return;
}
void SegModify(long long L, long long R, long long val, long long l, long long r, long long x){
if (l == L&&r == R){
t[x] = (R - L + 1)*val;
lazy[x] = val;
return;
}
PushDown(l, r, x);
long long m = (l + r) >> 1;
if (R <= m)SegModify(L, R, val, l, m, x << 1);
else if (L>m)SegModify(L, R, val, m + 1, r, x << 1 | 1);
else {
SegModify(L, m, val, l, m, x << 1);
SegModify(m + 1, R, val, m + 1, r, x << 1 | 1);
}
t[x] = t[x << 1] + t[x << 1 | 1];
return;
}
long long Query(long long L, long long R, long long l, long long r, long long x){
if (L == l && R == r)
return t[x];
PushDown(l, r, x);
long long m = (l + r) >> 1;
if (R <= m)return Query(L, R, l, m, x << 1);
else if (L>m)return Query(L, R, m + 1, r, x << 1 | 1);
else return Query(L, m, l, m, x << 1) + Query(m + 1, R, m + 1, r, x << 1 | 1);
}
bool cmp(data a, data b)
{
return a.hig < b.hig;
}
int main()
{
long long n;
while (cin >> n)
{
memset(dat, 0, sizeof(dat));
memset(lazy, 0, sizeof(lazy));
memset(table, 0, sizeof(table));
memset(a, 0, sizeof(a));
memset(t, 0, sizeof(t));
long long nn = 1;
long long ans = 0;
while (nn <= 4*n)
{
nn *= 2;
}
int s = 0;
for (size_t i = 0; i < n; i++)
{
scanf("%I64d%I64d%I64d", &dat[i].be, &dat[i].ed, &dat[i].hig);
//cin >> dat[i].be >> dat[i].ed >> dat[i].hig;
dat[i].ed--;
table[s] = dat[i].be;
s++;
table[s] = dat[i].ed;
s++;
table[s] = dat[i].be - 1;
s++;
table[s] = dat[i].ed + 1;
s++;
}
sort(table, table + 4 * n);
sort(dat, dat + n, cmp);
int end = unique(table, table + 4 * n) - table;
for (size_t i = 0; i < n; i++)
{
SegModify(lower_bound(table, table + end, dat[i].be) - table + 1, lower_bound(table, table + end, dat[i].ed) - table + 1, dat[i].hig, 1, nn, 1);
}
ans = Query(1, 1, 1, nn, 1);
for (size_t i = 2; i <= end; i++)
{
long long high = Query(i, i, 1, nn, 1);
ans += high*(table[i - 1] - table[i - 2]);
}
cout << ans << "\n";
}
return 0;
}