zoukankan      html  css  js  c++  java
  • UVA 1232

    UVA 1232 - SKYLINE

    题目链接

    题意:按顺序建房。在一条线段上,每一个房子一个高度。要求出每间房子建上去后的轮廓线

    思路:线段树延迟更新。一个setv作为高度的懒标记,此外还要在开一个cover表示当前结点一下是否都为同一高度

    代码:

    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    
    #define lson(x) ((x<<1)+1)
    #define rson(x) ((x<<1)+2)
    
    const int N = 100005;
    
    int t, n;
    
    struct Node {
        int l, r, h, setv;
        bool cover;
        Node() {}
        Node(int l, int r) {
    	this->l = l; this->r = r;
    	h = 0; setv = 0; cover = true;
        }
    } node[4 * N];
    
    void pushup(int x) {
        node[x].cover = ((node[lson(x)].h == node[rson(x)].h) && node[lson(x)].cover && node[rson(x)].cover);
        node[x].h = node[lson(x)].h;
    }
    
    void pushdown(int x) {
        node[lson(x)].setv = max(node[lson(x)].setv, node[x].setv);
        node[rson(x)].setv = max(node[rson(x)].setv, node[x].setv);
        node[lson(x)].h = max(node[lson(x)].setv, node[lson(x)].h);
        node[rson(x)].h = max(node[rson(x)].setv, node[rson(x)].h);
    }
    
    void build(int l, int r, int x = 0) {
        node[x] = Node(l, r);
        if (l == r) return;
        int mid = (l + r) / 2;
        build(l, mid, lson(x));
        build(mid + 1, r, rson(x));
    }
    
    int query(int l, int r, int v, int x = 0) {
        if (node[x].cover && node[x].h > v) return 0;
        if (node[x].l >= l && node[x].r <= r && node[x].cover) {
    	node[x].setv = v;
    	node[x].h = v;
    	return node[x].r - node[x].l + 1;
        }
        int mid = (node[x].l + node[x].r) / 2;
        int ans = 0;
        pushdown(x);
        if (l <= mid) ans += query(l, r, v, lson(x));
        if (r > mid) ans += query(l, r, v, rson(x));
        pushup(x);
        return ans;
    }
    
    int main() {
        scanf("%d", &t);
        while (t--) {
    	scanf("%d", &n);
    	build(1, N - 1);
    	int l, r, h;
    	int ans = 0;
    	while (n--) {
    	    scanf("%d%d%d", &l, &r, &h);
    	    ans += query(l, r - 1, h);
    	}
    	printf("%d
    ", ans);
        }
        return 0;
    }


  • 相关阅读:
    Acwing 284.金字塔 (区间DP)
    Acwing 283.多边形 (区间DP)
    Acwing 277.饼干 (DP+排序不等式)
    Acwing 274.移动服务 (DP)
    Acwing 273.分级 (DP)
    Acwing 271.杨老师的照相排序 (DP)
    Acwing 272.最长公共上升子序列 (DP)
    Mybatis-缓存
    mybatis与spring整合
    mybatis-sql映射文件
  • 原文地址:https://www.cnblogs.com/blfbuaa/p/7389320.html
Copyright © 2011-2022 走看看