POJ 2528

来源:互联网 发布:希捷备份软件 编辑:程序博客网 时间:2024/05/21 20:25
/*Mayor's posters线段树区间覆盖离散化特殊数据:131 101 46 10输出:3*/#include<cstdio>#include<cstring>#include<algorithm>using namespace std;#define lson (pos<<1)#define rson (pos<<1|1)const int maxn = 55555;int lx[maxn],rx[maxn],vis[maxn];int n,Hash[maxn * 2],cnt,ans;int cover[maxn << 2];void Hash_Init(){    sort(Hash,Hash + cnt);    cnt = unique(Hash,Hash + cnt) - Hash;    int m = cnt;    for(int i = 1; i < cnt; i++)        if(Hash[i] != Hash[i - 1] + 1){            Hash[m++] = Hash[i - 1] + 1;        }    cnt = m;    sort(Hash,Hash + cnt);    cnt = unique(Hash,Hash + cnt) - Hash;}int HASH(int t){    return lower_bound(Hash,Hash + cnt,t) - Hash;}void build(){    memset(cover,-1,sizeof(cover));    memset(vis,0,sizeof(vis));}void pushdown(int pos){    if(cover[pos] != -1){        cover[lson] = cover[rson] = cover[pos];        cover[pos] = -1;    }}void update(int L,int R,int l,int r,int pos,int d){    if(l <= L && R <= r){        cover[pos] = d;        return;    }    pushdown(pos);    int mid = (L + R) >> 1;    if(l <= mid)        update(L,mid,l,r,lson,d);    if(r  > mid)        update(mid + 1,R,l,r,rson,d);}void query(int l,int r,int pos){    if(cover[pos] != -1){        if(!vis[cover[pos]]){            vis[cover[pos]] = 1;            ans ++;        }        return;    }    if(l == r) return;    pushdown(pos);    int mid = (l + r) >> 1;    query(l,mid,lson);    query(mid + 1,r,rson);}int main(){    int T;    scanf("%d",&T);    while(T--){        scanf("%d",&n);        for(int i = 0; i < n; i++){            scanf("%d%d",&lx[i],&rx[i]);            Hash[cnt++] = lx[i];            Hash[cnt++] = rx[i];        }        Hash_Init();        build();        ans = 0;        int ret = 0;        for(int i = 0; i < n; i++){            int l = HASH(lx[i]),r = HASH(rx[i]);            update(0,cnt,l,r,1,ret++);        }        query(0,cnt,1);        printf("%d\n",ans);    }    return 0;}

0 0
原创粉丝点击