CodeForces 35E Parade(线段树区间更新+离散化)

题意:

在二维平面上,给你每个建筑的左右坐标以及高度,问你建筑外部轮廓的转折点。

解析:

这题可以用线段树的区间更新来做,线段树的每个节点,用于维护X轴区间最高的点,而不是X点上最高的点。
由于数据范围比较大 [1e9,1e9] ,可以先将X轴坐标,离散化。然后每次更新的区间是 [L,R1] ,这样就可以把坐标的更新,更新到区间上。
至于最后如何,输出拐点,可以用一个pre来表示之前的区间的高度,cur表示的是当前区间的高度,如果 cur!=pre 则表示出现了拐点。
那么就保存下该拐点,最后一起输出。

注意:

由于离散化,所以数组要开到平常的两倍大小,不然会WA。
因为这个检查了好久。
还有就是必须加上文件输入输出才行,不然会WA在第一个用例。

my code

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#define ls (o<<1)
#define rs (o<<1|1)
#define lson ls, L, M
#define rson rs, M+1, R
#define pb push_back
#define mp make_pair
using namespace std;
typedef long long ll;
const int N = (int)2e5 + 10;
typedef pair<ll, ll> pii;

struct Oper {
    int h, l, r;
} oper[N];
int X[N];
int n, tot;
vector<pii> ans;

void discrete() {
    sort(X, X+tot);
    tot = unique(X, X+tot) - X;
}

int maxv[N<<2];
void pushDown(int o) {
    if(maxv[o]) {
        maxv[ls] = max(maxv[ls], maxv[o]);
        maxv[rs] = max(maxv[rs], maxv[o]);
        maxv[o] = 0;
    }
}

void build(int o, int L, int R) {
    maxv[o] = 0;
    if(L == R) return ;
    int M = (L + R)/2;
    build(lson);
    build(rson);
}

void insert(int o, int L, int R, int ql, int qr, int val) {
    if(ql <= L && R <= qr) {
        maxv[o] = max(maxv[o], val);
        return ;
    }
    int M = (L + R)/2;
    pushDown(o);
    if(ql <= M) insert(lson, ql, qr, val);
    if(qr > M) insert(rson, ql, qr, val);
}

int query(int o, int L, int R, int pos) {
    if(L == R)
        return maxv[o];
    int M = (L + R)/2;
    pushDown(o);
    if(pos <= M) return query(lson, pos);
    else return query(rson, pos);
}

void getHeight() {
    ll pre = 0, cur;
    for(int i = 0; i < tot-1; i++) {
        cur = query(1, 0, tot-2, i);
        if(pre != cur) {
            ans.pb(mp(X[i], pre));
            ans.pb(mp(X[i], cur));
            pre = cur;
        }
    }
    if(cur != 0) {
        ans.pb(mp(X[tot-1], cur));
        ans.pb(mp(X[tot-1], 0));
    }
}

int main() {
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);

    ll h, l, r;
    while(~scanf("%d", &n)) {
        tot = 0;
        ans.clear();
        for(int i = 0; i < n; i++) {
            scanf("%lld%lld%lld", &h, &l, &r);
            oper[i] = (Oper){h, l, r};
            X[tot++] = l;
            X[tot++] = r;
        }

        discrete();
        build(1, 0, tot-2);
        int ql, qr;
        for(int i = 0; i < n; i++) {
            ql = lower_bound(X, X+tot, oper[i].l) - X;
            qr = lower_bound(X, X+tot, oper[i].r) - X - 1;
            insert(1, 0, tot-2, ql, qr, oper[i].h);
        }

        getHeight();
        printf("%d\n", (int)ans.size());
        for(int i = 0; i < ans.size(); i++) {
            printf("%lld %lld\n", ans[i].first, ans[i].second);
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值