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

强烈不推荐在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 (离散化 + 线段树)

时间: 2024-08-05 21:35:21

POJ 2528 Mayor's posters (离散化 + 线段树)的相关文章

POJ 2528 Mayor&#39;s posters(离散化线段树)

Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign have been placing their electoral posters at all places at their whim. The city council has finally decided to build an electoral wall for

POJ 2528 Mayor&#39;s posters(离散化 线段树)

题意  在墙上贴n张海报  输入每张海报的的左右端点坐标  问最后可以看到多少张海报  能看到一点也是能看到 先把线段树初始化为0 输入一张海报  就把那个区间变成这张海报的序号  最后判断墙上有多少个不同的序号就行了 但是海报坐标的端点值高达10000000  直接用线段树会超时   但是注意到海报最多只有10000张  也就是最多有20000个不同的坐标  于是可以利用离散化的知识   把所有坐标排序 注意所有右端点坐标+1也要加入排序(注意1,10 ; 1,3;  7,10这种情况  如果

POJ - 2528 - Mayor&#39;s posters 【线段树+离散化+补点】

http://poj.org/problem?id=2528 #include <cstdio> #include <iostream> #include <set> #include <cstring> #include <string> #define left rt<<1 #define right rt<<1|1 using namespace std; const int MAXN = 32768 + 5; in

POJ 2528 Mayor&#39;s posters(线段树+离散化)

题目链接:Mayor's posters 题意:按顺序往墙上贴海报,可以重叠,问最后可以看到多少海报.(被覆盖的海报是看不到的) 注意: 因为数据比较大,所以不离散化,肯定爆内存. 还有就是,不能只是单纯的离散化,还要处理好点的边界 举个例子 4 2  10. 2  8 3  6 6  8 8  10 离散化后 2 3 6 8 10 1 2 3 4 5 覆盖掉了 1 5   和  1 4俩段 只剩下 2  3  .3  4. 4  5 答案是 3 但是正确答案是4 所以,离散化处理时要处理边界,

【POJ】2528 Mayor&#39;s posters ——离散化+线段树

Mayor's posters Time Limit: 1000MS    Memory Limit: 65536K Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign have been placing their electoral posters at all places at their whim. The city

POJ 2528 Mayor&#39;s posters(线段树区间染色+离散化或倒序更新)

Mayor's posters Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 59239   Accepted: 17157 Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign have been placing their electoral post

poj(2528)——Mayor&#39;s posters(线段树+离散化)

这道题目让我又重新认识了一下离散化: 首先总结一下离散化的特点: 1)有时区间的端点并不是整数,或者区间太大导致建树内存开销过大而MLE,那么就需要进行离散化后再建树. 2)意思是将区间范围很大的数据集映射到较小的数据集,这样建树更加有效,或者说我们只取需要的值来用. 这个意思说到底就是进行映射,把原来很大的映射到一个较小的空间中去. 题意: 给定一些海报,它们可能相互重叠,告诉你每个海报的宽度(它们的高度都是一样的)和先后的叠放次序,问没有被完全盖住的海报有多少张? 这里我们注意到了数据的范围

poj 2528 Mayor&#39;s posters 【线段树 + 离散化】

Mayor's posters Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 50643   Accepted: 14675 Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign have been placing their electoral post

POJ - 2528 Mayor&#39;s posters (线段树+离散化)

Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign have been placing their electoral posters at all places at their whim. The city council has finally decided to build an electoral wall for

poj 2528 Mayor&#39;s posters(线段树 离散化 区间更新 贴海报)

     这个题目本来对大神来说可能是水题, 对我就不行了,昨晚非折腾到下半夜一点 搞定, 并且可以总结出 ,只有把问题想清楚,或着看人家解题报告自己把问题和代码思路 搞清楚,才能谈的上调bug,否则根本就不知道错在哪儿.说说这个题目的理解,他是如何转化为线段树问题的呢?我们知道线段树有一个区间更新的东西,每张海报的宽度不就是一个区间么?那么我们可以用一棵树中的部分结点 来表示整张海报的可视部分,也就是说每个结点只允许表示一张完整的或着不完整的海报(好几个结点才可以表示成完整的一张海报),那么如