poj2528(离散化线段树区间更新)

这题的离散化有点不一样。要在第一次离散化之后把每个数字之间差不为一的地方在随便填进去一个数。然后就是线段树的区间更新加查询

#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn = 10005;
int n,num;
int li[maxn];
int ri[maxn];
int ne[maxn<<3];
int clo[maxn<<4];
bool hashs[maxn];
int ans = 0;
void PushDown(int root)
{
    clo[root<<1] = clo[root<<1|1] = clo[root];
    clo[root] = -1;
}
void Update(int L,int R,int c,int l,int r,int root)
{
    if(l>=L&&r<=R)
    {
        clo[root] = c;
        return ;
    }
    if(clo[root]!=-1) PushDown(root);
    int m = (l+r)>>1;
    if(m>=L) Update(L,R,c,l,m,root<<1);
    if(m<R) Update(L,R,c,m+1,r,root<<1|1);
}
void query(int l,int r,int root)
{
    if(l==r){
        if(!hashs[clo[root]]){
            ans++;
            hashs[clo[root]] = true;
        }
        return ;
    }
    if(clo[root]!=-1) PushDown(root);
    int m = (l+r)>>1;
    query(l,m,root<<1);
    query(m+1,r,root<<1|1);
}
inline int find(int a)
{
    return upper_bound(ne+1,ne+1+num,a)-ne-1;
}
int main()
{
    int cases; int tot;
    scanf("%d",&cases);
    while(cases--)
    {
        scanf("%d",&n);
        tot = 0;
        memset(hashs,false,sizeof(hashs));
        memset(clo,-1,sizeof(clo));
        for(int i=1;i<=n;i++)
        {
            scanf("%d%d",&li[i],&ri[i]);
            ne[++tot] = li[i];
            ne[++tot] = ri[i];
        }
        sort(ne+1,ne+tot+1);
        num = 1;
        for(int i=2;i<=tot;i++)
        {
            if(ne[i]!=ne[i-1])
                ne[++num] = ne[i];
        }
        for(int i=num;i>1;i--) if(ne[i]-ne[i-1]>1) ne[++num] = ne[i]-1;
        sort(ne+1,ne+num+1);
        for(int i=1;i<=n;i++)
        {
            int ll = find(li[i]);
            int rr = find(ri[i]);
            Update(ll,rr,i,1,num,1);
        }
        ans = 0;
        query(1,num,1);
        printf("%d\n",ans);
    }
    return 0;
}
/*10
3
1 10
1 4
6 10
*/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值