• POJ2528——Mayor's posters (线段树区间更新查询+离散化)


    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 placing the posters and introduce the following rules:
    • Every candidate can place exactly one poster on the wall.
    • All posters are of the same height equal to the height of the wall; the width of a poster can be any integer number of bytes (byte is the unit of length in Bytetown).
    • The wall is divided into segments and the width of each segment is one byte.
    • Each poster must completely cover a contiguous number of wall segments.

    They have built a wall 10000000 bytes long (such that there is enough place for all candidates). When the electoral campaign was restarted, the candidates were placing their posters on the wall and their posters differed widely in width. Moreover, the candidates started placing their posters on wall segments already occupied by other posters. Everyone in Bytetown was curious whose posters will be visible (entirely or in part) on the last day before elections.

    Your task is to find the number of visible posters when all the posters are placed given the information about posters' size, their place and order of placement on the electoral wall.

    Input

    The first line of input contains a number c giving the number of cases that follow. The first line of data for a single case contains number 1 <= n <= 10000. The subsequent n lines describe the posters in the order in which they were placed. The i-th line among the n lines contains two integer numbers l i and ri which are the number of the wall segment occupied by the left end and the right end of the i-th poster, respectively. We know that for each 1 <= i <= n, 1 <= l i <= ri <= 10000000. After the i-th poster is placed, it entirely covers all wall segments numbered l i, l i+1 ,... , ri.

    Output

    For each input data set print the number of visible posters after all the posters are placed.
    The picture below illustrates the case of the sample input.

    Sample Input

    1
    5
    1 4
    2 6
    8 10
    3 4
    7 10

    Sample Output

    4

    题解:

    基础的线段树区间更新查询就不说了,主要考点是数据离散化。

    离散化,把无限空间中有限的个体映射到有限的空间中去,以此提高算法的时空效率。

    通俗的说,离散化是在不改变数据相对大小的条件下,对数据进行相应的缩小。

    主要用于所给数据的上限太大无法开数组但只需要相对大小关系就可以做题的情况。

    也就是说当数据只与它们之间的相对大小有关,而与具体是多少无关时,可以进行离散化。

    一般离散化前都需要排序(sort)和去重(unique)两步。(如果不知道unique函数的请进海克斯传送门

    代码:

    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    #include <cstring>
    
    using namespace std;
    
    const int MAXN = 10005;
    
    bool Tree[MAXN*4];
    bool Change[MAXN*4];
    struct D{
    	int l,r;
    }board[MAXN];
    int post[MAXN*2];
    int top;
    int hash[10000005];
    
    void Build(int temp,int left ,int right){
    	Tree[temp] = false;
    	if(left == right)return ;
    	int mid = left + (right-left)/2;
    	Build(temp<<1,left,mid);
    	Build(temp<<1|1,mid+1,right);
    }
    
    bool Updata(int temp,int left,int right,int ql,int qr){//更新加查询 
    	if(Tree[temp])return false;
    	if(ql>right || qr<left)return false;
    	if(left == right){
    		Tree[temp] = true;
    		return true;
    	}
    	int mid = left + (right-left)/2;
    	bool re1,re2;
    	re1 = Updata(temp<<1,left,mid,ql,qr);
    	re2 = Updata(temp<<1|1,mid+1,right,ql,qr);
    	if(Tree[temp<<1] && Tree[temp<<1|1])Tree[temp] = true;
    	return re1||re2;
    }
    
    int main(){
    	
    	int T,N,sum,len;
    	scanf("%d",&T);
    	while(T--){
    		scanf("%d",&N);
    		sum = top = 0;
    		memset(Change,false,sizeof Change);
    		for(int _=0 ; _<N ; _++){
    			scanf("%d %d",&board[_].l,&board[_].r);
    			post[top++] = board[_].l;
    			post[top++] = board[_].r;
    		}
    		sort(post,post+top);//排序 
    		len = unique(post,post+top) - post;//去重 
    		Build(1,0,len-1);
    		for(int _=0 ; _<len ; _++)hash[post[_]] = _;//离散化 
    		for(int _=N-1 ; _>=0 ; _--){
    			if(Updata(1,0,len-1,hash[board[_].l],hash[board[_].r]))++sum;
    		}
    		printf("%d
    ",sum);
    	}
    	
    	return 0;
    } 



  • 相关阅读:
    [POJ]poj2632(模拟)
    [EOJ]2019 ECNU XCPC March Selection #2
    [POJ]POJ1328(trie)
    卡特兰数相关总结
    2019海亮夏令营随笔
    树上数数 题解
    护卫小队 题解
    洛谷 P2966 [USACO09DEC]牛收费路径Cow Toll Paths 题解
    洛谷 P4735 最大异或和
    登峰造极 题解
  • 原文地址:https://www.cnblogs.com/vocaloid01/p/9514108.html
Copyright © 2020-2023  润新知