N 棟矩形建築 (N ≤ 40000),第 i 棟橫跨 [A_i, B_i],高度 H_i。建築可重疊。求天際線剪影總面積。坐標 ≤ 1e9,高度 ≤ 1e9。
struct Node { int l, r; int h; bool cover; } tree[MAXN * 4];
int xs[MAXN * 2]; // 離散化坐標
void insert(int L, int R, int h, int rt) {
if (tree[rt].l == L && tree[rt].r == R) {
if (tree[rt].h < h) tree[rt].h = h;
tree[rt].cover = true;
return;
}
if (tree[rt].cover) return; // 已被更高建築完全覆蓋
int mid = (tree[rt].l + tree[rt].r) >> 1;
if (R <= mid) insert(L, R, h, rt<<1);
else if (L >= mid) insert(L, R, h, rt<<1|1);
else { insert(L, mid, h, rt<<1); insert(mid, R, h, rt<<1|1); }
}
long long query(int rt) {
if (tree[rt].l + 1 == tree[rt].r) { // 葉子:基本區間
return 1LL * tree[rt].h * (xs[tree[rt].r] - xs[tree[rt].l]);
}
if (tree[rt].cover) { // 整段同高
return 1LL * tree[rt].h * (xs[tree[rt].r] - xs[tree[rt].l]);
}
return query(rt<<1) + query(rt<<1|1);
}