Gym - 102448F Finally, christmas! (扫描线)

Description
The christmas spirit is taking over the city of Arcoverde! In honor of this special day, the mayor of this renowned metropolis decided to decorate all of the city’s front view. To do this, he asked his engineer friends Icaro and Cortizo how much it would cost.

It is known that the mayor likes to save as much money as possible. Therefore, Icaro and Cortizo need to find out what is the minimum area necessary to comprise the entire city’s front view.

Arcoverde is made only of rectangular buildings, in two dimensions, and all buildings were built on the same base height, as shown on the picture below.
在这里插入图片描述

Icaro and Cortizo were very tired, since they were also responsible for building the biggest christmas tree ever seen on Pernambuco, so they asked for your help.

Input
The first input line contains an integer N(1≤N≤1e5), indicating the number of buildings in Arcoverde. Then, N lines follow, each one containing three integers Li (0≤Li<1e9), Ri (Li<Ri≤1e9) and Hi (1≤Hi≤1e6), which are, respectively, the left X-coordinate, the right X-coordinate and the height of the i-th building.

Output
The output consists in a single integer number, which is the minimum area required to decorate the entire front view of Arcoverde.

Examples

Input
6
2 6 9
9 14 11
12 20 6
17 25 20
23 31 14
29 36 18

Output
451

Main idea & Solution
给定平面上若干矩形,求矩形面积并

裸扫描线

Code

// #include <bits/stdc++.h>
#include <cstdio>
#include <iostream>
#include <vector>
#include <cstring>
#include <algorithm>
using namespace std;

const int MX = 4e5 + 7;
int n;
ll x1,y1,x2,y2,X[MX << 1];
struct Scanline{
    ll l,r,h;
    int mark;
    bool operator< (const Scanline&it) const {
        return h < it.h;
    }
} line[MX << 1];
struct SegTree{
    int l,r,sum;
    ll len;
} tree[MX << 2];
void build_tree(int rt,int l,int r){
    tree[rt].l = l,tree[rt].r = r;
    tree[rt].len = 0, tree[rt].sum = 0;
    if(l == r) return ;
    int mid = (l + r) >> 1;
    build_tree(ls,l,mid);build_tree(rs,mid+1,r);
    return;
}

void push_up(int rt){
    int l = tree[rt].l, r = tree[rt].r;
    if(tree[rt].sum) tree[rt].len = X[r + 1] - X[l];
    else tree[rt].len = tree[ls].len + tree[rs].len;
}

void update(int rt,ll L,ll R,int c){
    int l = tree[rt].l, r = tree[rt].r;
    if(X[r + 1] <= L || R <= X[l]) return ;
    if(L <= X[l] && X[r + 1] <= R){
        tree[rt].sum += c;
        push_up(rt);return ;
    }
    update(ls,L,R,c);update(rs,L,R,c);
    push_up(rt);
}
int main(){
    scanf("%d",&n);
    for(int i = 1;i <= n;++i){
        ll L,R,H;scanf("%lld %lld %lld",&L,&R,&H);
        x1 = L,x2 = R;
        y1 = 0,y2 = H;
        X[2 * i - 1] = x1, X[2 * i] = x2;
        line[2 * i - 1] = (Scanline){x1,x2,y1,1};
        line[2 * i] = (Scanline) {x1,x2,y2,-1};
    }
    n <<= 1;
    sort(line + 1,line + n + 1);
    sort(X + 1,X + n + 1);
    int tot = unique(X + 1,X + n + 1) - X - 1;
    build_tree(1,1,tot - 1);
    ll ans = 0;
    for(int i = 1;i < n;++i){
        update(1,line[i].l,line[i].r,line[i].mark);
        ans += tree[1].len * (line[i + 1].h - line[i].h);
    }
    printf("%lld\n", ans);
    return 0;
}

Hint
y 1 y1 y1 变量名的时候不要用 c m a t h / b i t s cmath / bits cmath/bits头文件

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值