poj2528贴海报,,

本文详细介绍了区间离散化的过程及其在解决区间更新问题中的应用,通过使用线段树数据结构,实现对区间颜色的快速更新与查询。文章提供了完整的代码示例,包括离散化处理、区间更新及查询的实现细节。

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

对于区间段的离散化需要注意一下,和点离散化不同

离散后如何识别一段区间还是一段区间,而不是两个顶点,就是如果两个点的距离大于1,就往离散的数据里插入一个中间值,即用三个点来表示一段区间

/*
离散化长度
区间更新,线段树每个结点保存当前该区间颜色,查询也是如此
*/
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define maxn 100005
int color[maxn<<2];//-1表示无色
struct poster{
    int l,r;
}p[maxn];
int data[maxn<<2],cnt,m;
void pushdown(int rt){
    if(color[rt]!=-1){
        color[rt<<1]=color[rt<<1|1]=color[rt];
        color[rt]=-1;
    }
}
void update(int L,int R,int c,int l,int r,int rt){
    if(L<=l && R>=r){
        color[rt]=c;
        return;
    }
    pushdown(rt);
    int m=l+r>>1;
    if(L<=m) update(L,R,c,lson);
    if(R>m) update(L,R,c,rson);
}

int vis[maxn],ans;
void query(int L,int R,int l,int r,int rt){
    if(color[rt]!=-1 && !vis[color[rt]]){
        ans++;
        vis[color[rt]]=1;
        return;
    }
    if(l==r) return;
    pushdown(rt);
    int m=l+r>>1;
    if(L<=m) query(L,R,lson);
    if(R>m) query(L,R,rson);
}
int main(){
    int n,T;
    scanf("%d",&T);
    while(T--){
        cin >> n;
        cnt=ans=0;
        memset(vis,0,sizeof vis);
        memset(color,-1,sizeof color);
        for(int i=1;i<=n;i++){
            scanf("%d%d",&p[i].l,&p[i].r);
            data[cnt++]=p[i].l;
            data[cnt++]=p[i].r;
        }
        //离散化
        sort(data,data+cnt);
        m=unique(data,data+cnt)-data;
        int tmp=m;
        for(int i=1;i<m;i++)
            if(data[i]-data[i-1]>1) data[tmp++]=data[i-1]+1;
        m=tmp;
        sort(data,data+m);


        for(int i=1;i<=n;i++){
            int posl=lower_bound(data,data+m,p[i].l)-data+1;
            int posr=lower_bound(data,data+m,p[i].r)-data+1;
            update(posl,posr,i,1,m,1);
        }

        query(1,m,1,m,1);
        cout << ans << endl;
    }
}

 

转载于:https://www.cnblogs.com/zsben991126/p/9911361.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值