zoukankan      html  css  js  c++  java
  • poj2528贴海报,,

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

    离散后如何识别一段区间还是一段区间,而不是两个顶点,就是如果两个点的距离大于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;
        }
    }
  • 相关阅读:
    算法笔记--数据结构--并查集
    帮助
    八数码难题神奇!!!
    题解 P1197 【[JSOI2008]星球大战】
    线段树1对于Pushdown的理解
    最短路问题之SPFA
    并查集(路径压缩)
    并查集(KRUSKAL算法)
    初识单调栈
    初识单调队列
  • 原文地址:https://www.cnblogs.com/zsben991126/p/9911361.html
Copyright © 2011-2022 走看看