【线段树+离散化】POJ2528 Mayor's posters

Mayor‘s posters

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 64939   Accepted: 18770

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 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 li 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 <= li <= ri <= 10000000. After the i-th poster is placed, it entirely covers all wall segments numbered li, li+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

题意:N个人(1<=N<=10000)依次贴N张等高的海报,给出每张海报的左端点,右端点li,ri(1<=li<=ri<=10000000)。后面的海报可能会把前面的海报盖住。问最后能看见几张海报。

题解:一个比较明显的区间染色问题。暴力肯定会TLE+爆内存,没有问题吧。对我这种蒟蒻来说好像只会用刚学的线段树。但是端点范围太大,直接开数组明显不可行。所以还要离散化一下。所谓的离散化,在我理解看来就是将一个很大的区间映射成一个很小的区间,而不改变原有的覆盖关系。但是对于这道题而言,简单的离散化可能会出现错误。比如说:例子一:[1,10][1,4][5,10]例子二:[1,10][1,4][6,10]它们普通离散化后都变成了[1,4][1,2][3,4]。线段2覆盖了[1,2],线段3覆盖了[3,4],那么线段1是否被覆盖掉了呢?例子一是被覆盖掉了,而例子二没有被覆盖。解决的办法,就是在距离大于1的两个相邻节点间插入一个点,保证准确性。

代码:
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
struct node{int l,r;}a[100001]; //节点信息
int N,ans;
int c[400001]; //区间每个点的颜色
bool vis[100001]; //记录颜色访问信息
int x[100001]; //离散化后的区间
inline int read(){
    int x=0,f=1;
    char c=getchar();
    for(;!isdigit(c);c=getchar())
        if(c==‘-‘)
            f=-1;
    for(;isdigit(c);c=getchar())
        x=x*10+c-‘0‘;
    return x*f;
}
void cover(int num){
    if(c[num]!=-1){
        c[num<<1]=c[num<<1|1]=c[num];
        c[num]=-1;
    }
}
void update(int L,int R,int col,int l,int r,int num){
    //如果这个点在需要染色的区间内,直接更新并且返回
    if(l>=L && r<=R){
        c[num]=col;
        return;
    }
    cover(num); //更新
    int mid=(l+r)>>1;
    if(L<=mid) update(L,R,col,l,mid,num<<1); //更新左儿子
    if(R>mid) update(L,R,col,mid+1,r,num<<1|1); //更新右儿子
}
void query(int l,int r,int num){
    //累加未出现的颜色
    if(c[num]!=-1){
        if(!vis[c[num]]) ans++;
        vis[c[num]]=1;
        return;
    }
    if(l==r) return; //如果到了叶子则返回
    int mid=(l+r)>>1;
    query(l,mid,num<<1); //统计左儿子
    query(mid+1,r,num<<1|1); //统计右儿子
}
int main(){
    int T=read();
    while(T--){
        memset(c,-1,sizeof(c));
        memset(vis,0,sizeof(vis));
        N=read();ans=0;
        int p=0; //输入点数
        int q=1; //离散化后点数
        for(int i=0;i<N;i++){
            a[i].l=read();a[i].r=read();
            x[p++]=a[i].l;x[p++]=a[i].r;
        }
        sort(x,x+p);
        //去重操作
        for(int i=1;i<p;i++)
            if(x[i]!=x[i-1])
                x[q++]=x[i];
        //离散化操作
        for(int i=q-1;i>=1;i--)
            if(x[i]>x[i-1]+1)
                x[q++]=x[i-1]+1;
        sort(x,x+q);
        //染色操作
        for(int i=0;i<N;i++){
            int l=lower_bound(x,x+q,a[i].l)-x;
            int r=lower_bound(x,x+q,a[i].r)-x;
            update(l,r,i,0,q,1);
        }
        //统计操作
        query(0,q,1);
        printf("%d\n",ans);
    }
    return 0;
}

【线段树+离散化】POJ2528 Mayor's posters

时间: 2024-10-25 19:05:41

【线段树+离散化】POJ2528 Mayor's posters的相关文章

poj2528 Mayor&#39;s posters(线段树,离散化)

离散化的思想: 对于这样的数据 (3,10000), (9,1000000), (5,100000), (1,1000), (7,1000000) 我们可以将其处理为 (2,7), (5,9), (3,8), (1,6), (4,9) 我们再对离散化之后的数据进行处理就行了. 题目意思: n(n<=10000)个人依次贴海报,给出每张海报所贴的范围li,ri(1<=li<=ri<=10000000). 求出最后还能看见多少张海报. 参考代码: #include <iostre

POJ2528 Mayor&#39;s posters(线段树染色问题+离散化)

题目大意:有t组数据,每组数据给你n张海报(1<=n<=10000),下面n组数据分别给出每张海报的左右范围(1 <= l <= r <= 10000000),下一张海报会覆盖前一张海报,求最后可见(包括完全和不完全可见)的海报有几张. 例如: 1 5 1 4 2 6 8 10 3 4 7 10 如上图所示,答案为4. 解题思路:其实这是一道区间染色问题,但是由于查找区间太大,显然直接建树会导致MLE,所以这里通过使用对区间的离散化来缩小查找范围.参考了一些大牛博客,简单说一

POJ2528 Mayor&#39;s posters 【线段树】+【成段更新】+【离散化】

Mayor's posters Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 39795   Accepted: 11552 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(线段树区间更新及区间统计+离散化)

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

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

2016-08-15 题意:一面墙,往上面贴海报,后面贴的可以覆盖前面贴的.问最后能看见几种海报. 思路:可以理解成往墙上涂颜色,最后能看见几种颜色(下面就是以涂色来讲的).这面墙长度为1~1000 0000,一千万,确实很大.暴力的话肯定不行,除非..( you know). 正确的解法是用线段树,不过还得加上离散化,因为数据太大10000000啊. 先说一下离散化,这个其实就是压缩,把范围压缩,举个例子: 输入 : 1 3000    //涂第一种颜色 范围从1~10000      下面同

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

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

POJ2528 Mayor&#39;s posters(线段树成段替换,区间查询,离散化简单hash)

题意:在墙上贴海报,海报可以互相覆盖,问最后可以看见几张海报 思路:这题数据范围很大,直接搞超时+超内存,需要离散化: 离散化简单的来说就是只取我们需要的值来 用,比如说区间[1000,2000],[1990,2012] 我们用不到[-∞,999][1001,1989][1991,1999][2001,2011][2013,+∞]这些值,所以我只需要 1000,1990,2000,2012就够了,将其分别映射到0,1,2,3,在于复杂度就大大的降下来了 所以离散化要保存所有需要用到的值,排序后,

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

恩,这区间范围挺大的,需要离散化.如果TLE,还需要优化一下常数. AC代码 #include <stdio.h> #include <string.h> #include <map> #include <set> #include <algorithm> using namespace std; const int maxn = 40000+5; typedef pair<int, int> Pii; Pii a[10000 + 5

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