线段树离散化

/*
 线段树离散化
 by sbn 
*/
#include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<vector>
using namespace std;
#define pb push_back
#define mp make_pair
typedef vector<int> vi;
typedef pair<int,int>   pii;
typedef long long ll;
pii b[10001],a[10001];
vi Hash,res;
int n,z,k,ans,t,hashsize;
struct node{
    ll l,r,col;
    node(){}
    node(ll nl,ll nr,int ncol){
        l=nl;r=nr;col=ncol;
    }
}   tree[400001];
inline void hash_init(){
    Hash.clear();
    for (int i=1;i<=n;i++)
    {
        Hash.pb(a[i].first);Hash.pb(a[i].first+1);
        Hash.pb(a[i].second);Hash.pb(a[i].second+1);
    }
    sort(Hash.begin(),Hash.end());
    Hash.erase(unique(Hash.begin(),Hash.end()),Hash.end());
}
inline void hash_main(){
    hash_init();
    for (int i=1;i<=n;i++)
    {
        b[i].first=lower_bound(Hash.begin(),Hash.end(),a[i].first)-Hash.begin()+1;
        b[i].second=lower_bound(Hash.begin(),Hash.end(),a[i].second)-Hash.begin()+1;
        if (b[i].first>hashsize)    hashsize=b[i].first;
        if (b[i].second>hashsize)   hashsize=b[i].second;
    }
}
inline void build(ll o,ll l,ll r){
    tree[o]=node(l,r,-1);
    if (l==r)   return;
    ll mid=(l+r)>>1;
    build(o<<1,l,mid);
    build(o<<1|1,mid+1,r);
}
inline void pushdown(ll o){
    if (tree[o].col!=-1){
        tree[o<<1|1].col=tree[o<<1].col=tree[o].col;
        tree[o].col=-1;
    }
    return;
}
inline void pushup(int o){
    if (tree[o<<1].col==-1||tree[o<<1|1].col==-1||tree[o<<1].col!=tree[o<<1|1].col)
        tree[o].col=-1;
    else    tree[o].col=tree[o<<1].col;
}
inline void rs(ll o,ll l,ll r,ll z){
    if (l<=tree[o].l&&tree[o].r<=r){
        tree[o].col=z;
        return;
    }
    pushdown(o);
    ll mid=(tree[o].l+tree[o].r)>>1;
    if (r<=mid) rs(o<<1,l,r,z);
    else
    if (l>mid)  rs(o<<1|1,l,r,z);
    else{
        rs(o<<1,l,mid,z);
        rs(o<<1|1,mid+1,r,z);
    }
    pushup(o);
}
inline void queue(ll o){
    if(tree[o].col!=-1){
        res.pb(tree[o].col);
        return;
    }
    if (tree[o].l==tree[o].r)   return;
    queue(o<<1);
    queue(o<<1|1);
    return;
}
int main(){
    //scanf("%d",&t);
    t=1;
    while (t--){
    hashsize=0;
    Hash.clear();
    res.clear();int zzz;
    scanf("%d%d",&n,&zzz);
    for (int i=1;i<=n;i++)
        scanf("%d%d",&a[i].first,&a[i].second);
    hash_main();
    build(1,1,hashsize);
    for (int i=1;i<=n;i++)
        rs(1,b[i].first,b[i].second,i);
    queue(1);
    sort(res.begin(),res.end());
    ans=0;

    for (int i=0;i<res.size();i++)
        if (res[i]!=res[i-1])   ans++;
    //cout<<"a:"<<endl;
    //for (int i=1;i<=n;i++)
    //  cout<<a[i].first<<" "<<a[i].second<<endl;
    //cout<<"b:"<<endl;
    //for (int i=1;i<=n;i++)
    //  cout<<b[i].first<<" "<<b[i].second<<endl;
    //cout<<"tree:"<<endl;
    //for (int o=1;o<=n*3;o++)
    //  cout<<tree[o].l<<" "<<tree[o].r<<" "<<tree[o].col<<endl;
    printf("%d\n",ans);
}
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值