[USACO 2007 Open Silver] City Horizon - 离散化+线段树

本文介绍了一种利用离散化结合线段树解决区间查询问题的方法。通过实例详细展示了如何对输入数据进行预处理并构建线段树,以高效查询区间内的最大值。适用于需要快速处理区间更新及查询的应用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


题目描述


输入格式

这里写图片描述


输出格式


样例数据

样例输入

4
2 5 1
9 10 4
6 8 2
4 6 3

样例输出

16

样例说明

这里写图片描述


题目分析

离散化+线段树(维护最大值)
事先拍一遍序,以免出蜜汁错误


源代码

#include<algorithm>
#include<iostream>
#include<iomanip>
#include<cstring>
#include<cstdlib>
#include<vector>
#include<cstdio>
#include<cmath>
#include<queue>
using namespace std;
typedef long long LL;
inline const LL Get_Int() {
    LL num=0,bj=1;
    char x=getchar();
    while(x<'0'||x>'9') {
        if(x=='-')bj=-1;
        x=getchar();
    }
    while(x>='0'&&x<='9') {
        num=num*10+x-'0';
        x=getchar();
    }
    return num*bj;
}
const int maxn=100000;
struct Tree { //修改区间 查询点 
    LL left,right,height;
};
struct Segment_Tree { //统计标记次数(颜色有用么) 
    Tree tree[maxn*4];
    LL sum;
    void build(int index,int Left,int Right) {
        tree[index].left=Left;
        tree[index].right=Right;
        tree[index].height=0;
        if(Left==Right)return;
        int mid=(Left+Right)/2;
        build(2*index,Left,mid);
        build(2*index+1,mid+1,Right);
    }
    void push_down(int index) {
        if(tree[index].height<=0)return;
        tree[index*2].height=tree[index*2+1].height=tree[index].height;
        tree[index].height=-1;
    }
    void push_up(int index) {
        if(tree[index*2].height==tree[index*2+1].height)tree[index].height=tree[index*2].height;
    }
    void modify(int index,int Left,int Right,int height) {
        if(Right<tree[index].left||Left>tree[index].right)return; //不相交
        if(Left<=tree[index].left&&Right>=tree[index].right) { //完全包含
            tree[index].height=height;
            return;
        }
        push_down(index);
        modify(index*2,Left,Right,height);
        modify(index*2+1,Left,Right,height);
        push_up(index);
    }
    void init() {
        sum=0;
    }
    void Query(int index,int Left,int Right,int* b) {
        if(Right<tree[index].left||Left>tree[index].right)return; //不相交
        if(tree[index].height>0) { //完全包含
            sum+=tree[index].height*(long long)(b[tree[index].right+1]-b[tree[index].left]);
            return;
        }
        Query(index*2,Left,Right,b);
        Query(index*2+1,Left,Right,b);
    }
};
Segment_Tree st;
struct Building {
    int start,end,height;
    bool operator < (const Building& b) const {
        return height<b.height;
    }
} b[40005];
int n,a[100005];
int Discretization(int* a,int n) {
    sort(a+1,a+n+1);
    int cnt=unique(a+1,a+n+1)-a-1;
    for(int i=1; i<=n; i++) {
        b[i].start=lower_bound(a+1,a+cnt+1,b[i].start)-a;
        b[i].end=lower_bound(a+1,a+cnt+1,b[i].end)-a;
    }
    return cnt;
}
int main() {
    n=Get_Int();
    for(int i=1; i<=n; i++) {
        b[i].start=Get_Int();
        b[i].end=Get_Int();
        b[i].height=Get_Int();
        a[i]=b[i].start;
        a[i+n]=b[i].end;
    }
    int cnt=Discretization(a,n*2);
    sort(b+1,b+n+1);
    st.build(1,1,cnt);
    for(int i=1; i<=n; i++)st.modify(1,b[i].start,b[i].end-1,b[i].height);
    st.init();
    st.Query(1,1,cnt,a);
    printf("%lld\n",st.sum);
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值