码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 2528 Mayor's posters (离散化 + 线段树)

时间:2015-08-20 06:49:55      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:线段树   离散化   数据结构   

强烈不推荐在POJ做这道题!!!
强烈不推荐在POJ做这道题!!!
强烈不推荐在POJ做这道题!!!
推荐去UVA 10587 或 SCU 2249
POJ的数据比较水且可能有错,一些本来错误的数据但可以水过,以及在UVA与SCU同样题目都能AC的程序在POJ莫名WA了。
建议写完程序后跑下这组数据:
1
3
1 10
1 3
6 10
好多题解的答案是2,但答案明显是3,这是因为每个数字其实表示的是一个单位长度,并非一个点 , 这就会导致像这样的区间:
1-10 1-4 5-10
1-10 1-4 6-10
普通离散化后都变成了[1,4], [1,2], [3,4],但实际上前者是2后者是3,所以要在非相邻区间之间插入一个点,例如上例即变成:
[1,4], [1,2], [3,4]
[1,5], [1,2], [3,3] ,[4,5]
这样才能得到正确的解。

代码:
吐槽一个地方。。你们把hash随便替换一个名字试试看

/*
* @author FreeWifi_novicer
* language : C++/C
*/
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<string>
#include<map>
#include<set>
#include<vector>
#include<queue>

using namespace std;

#define clr( x , y ) memset(x,y,sizeof(x))
#define cls( x ) memset(x,0,sizeof(x))
#define mp make_pair
#define pb push_back
#define lson l , mid , rt << 1
#define rson mid + 1 , r , rt << 1|1
typedef long long lint;
typedef long long ll;
typedef long long LL;

const int maxn = 11111;  
bool hash[maxn];  
int l[maxn] , r[maxn];  
int X[maxn*3];  
int col[maxn<<4];  
int cnt;
void push_down( int rt ){
    if( col[rt] != -1){
        col[ rt << 1 ] = col[ rt << 1|1 ] = col[rt] ;
        col[rt] = -1 ;
    }
}

void update( int L , int R , int c , int l , int r , int rt ){
    if( L <= l && r <= R ){
        col[rt] = c ;
        return ;
    }
    push_down( rt ) ;
    int mid = ( l + r ) >> 1 ;
    if( L <= mid )
        update( L , R , c , lson ) ;
    if( R > mid )
        update( L , R , c , rson ) ;
}

void query( int  l , int r , int rt ){
    if( col[rt] != -1 ){
        if( !hash[col[rt]] ) cnt++;
        hash[col[rt]] = true ;
        return ;
    }
    if( l == r ) return ;
    int mid = ( l + r ) >> 1 ;
    query( lson ) ;
    query( rson ) ;

}
int bs( int key , int len ){
    int L = 0 , R = len - 1 ;
    while( L <= R ){
        int M = ( L + R ) >> 1 ;
        if( X[M] == key ) return M ;
        else if( X[M] > key ) R = M - 1 ;
        else L = M + 1 ;
    }
    return -1 ;
}

int main(){
//  freopen("input.txt","r",stdin);
    int t ; cin >> t ;
    while( t-- ){

        int n ;
        cin >> n ;
        int p = 0 ;
        for( int  i = 0 ; i < n ; i++ ){
            scanf( "%d%d" , &l[i] , &r[i] ) ;
            X[p++] = l[i] ;
            X[p++] = r[i] ;
        }

        sort( X , X+p ) ;
        int s = 1 ;
        for( int i = 1 ; i < p ; i++ ){
            if( X[i] != X[i-1] )
                X[s++] = X[i] ;
        }
        for( int i = s - 1 ; i > 0 ; i-- ){
            if( X[i] != X[i-1] + 1 ) 
                X[s++] = X[i-1] + 1 ;
        }

        sort( X , X+s ) ;
        clr( col , -1 ) ;
        for( int i = 0 ; i < n ; i++ ){
            int  le = bs( l[i] , s ) ;
//            cout << le << endl;
            int  ri = bs( r[i] , s )  ;
            update( le , ri , i , 0 , s , 1 ) ;
        }
        cnt = 0 ;
        clr( hash , false ) ;

        query( 0 , s , 1 ) ;

        cout << cnt << endl;
    }
    return 0;
}

版权声明:博主表示授权一切转载啦:)

POJ 2528 Mayor's posters (离散化 + 线段树)

标签:线段树   离散化   数据结构   

原文地址:http://blog.csdn.net/qq_15714857/article/details/47797535

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!