POJ2528Mayor's posters[线段树 离散化]

Mayor‘s posters

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 59683   Accepted: 17296

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

Source

Alberta Collegiate Programming Contest 2003.10.18


notonlysuccess大神的题解

题意:在墙上贴海报,海报可以互相覆盖,问最后可以看见几张海报 思路:这题数据范围很大,直接搞超时+超内存,需要离散化:  离散化简单的来说就是只取我们需要的值来用,比如说区间[1000,2000],[1990,2012] 我们用不到[-∞,999][1001,1989][1991,1999][2001,2011][2013,+∞]这些值,所以我只需要1000,1990,2000,2012就够了,将其分别映射到0,1,2,3,在于复杂度就大大的降下来了 所以离散化要保存所有需要用到的值,排序后,分别映射到1~n,这样复杂度就会小很多很多 而这题的难点在于每个数字其实表示的是一个单位长度(并且一个点),这样普通的离散化会造成许多错误(包括我以前的代码,poj这题数据奇弱) 给出下面两个简单的例子应该能体现普通离散化的缺陷: 1-10 1-4 5-10 1-10 1-4 6-10  为了解决这种缺陷,我们可以在排序后的数组上加些处理,比如说[1,2,6,10]  如果相邻数字间距大于1的话,在其中加上任意一个数字,比如加成[1,2,3,6,7,10],然后再做线段树就好了.  线段树功能:update:成段替换 query:简单hash

说一下离散化,用map完美TLE,改成1e7大数组因为memset太耗时,再改成二分搜索用下标快好多

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
using namespace std;
const int N=1e4+10,TREE=4e4+5;
#define m (l+r)/2
#define lson o<<1,l,m
#define rson o<<1|1,m+1,r
#define lc o<<1
#define rc o<<1|1
inline int read(){
    char c=getchar();int x=0,f=1;
    while(c<‘0‘||c>‘9‘){if(c==‘-‘)f=-1;c=getchar();}
    while(c>=‘0‘&&c<=‘9‘){x=x*10+c-‘0‘;c=getchar();}
    return x*f;
}
//map<int,int> mp;
//int mp[10000009];
int mp[N<<2];
int t[TREE<<2];
int T,n,ll[N],rr[N],a[N<<1];
inline void paint(int o,int v){
    t[o]=v;
}
inline void pushDown(int o){
    if(t[o]!=0){
        paint(lc,t[o]);
        paint(rc,t[o]);
        t[o]=0;
    }
}
void draw(int o,int l,int r,int ql,int qr,int v){//printf("draw %d %d %d\n",o,l,r);
    if(ql<=l&&r<=qr) paint(o,v);
    else{
        pushDown(o);
        if(ql<=m) draw(lson,ql,qr,v);
        if(m<qr) draw(rson,ql,qr,v);
    }
}
int vis[N],ans=0;
void query(int o,int l,int r,int ql,int qr){//printf("quer %d %d %d\n",o,l,r);
    if(t[o]){
        if(!vis[t[o]]){ans++;vis[t[o]]=1;}
    }else{
        if(l==r) return;
        if(ql<=m) query(lson,ql,qr);
        if(m<qr) query(rson,ql,qr);
    }
}
int main(){
    T=read();
    while(T--){
        n=read();
        memset(t,0,sizeof(t));
        //mp.clear();
        //memset(mp,0,sizeof(mp));
        for(int i=1;i<=n;i++){ll[i]=read();rr[i]=read();a[2*i-1]=ll[i];a[2*i]=rr[i];}
        sort(a+1,a+1+2*n);
        int cnt=0;
//        mp[a[1]]=++cnt;
//        for(int i=2;i<=2*n;i++)
//            if(a[i]!=a[i-1]){
//                //if(!mp.count(a[i]-1)) mp[a[i]-1]=++cnt;
//                if(!mp[a[i]-1]) mp[a[i]-1]=++cnt;
//                mp[a[i]]=++cnt;
//            }
//        for(int i=1;i<=n;i++){
//            int ql=mp[ll[i]],qr=mp[rr[i]];//printf("hi %d %d\n",ql,qr);
//            draw(1,1,cnt,ql,qr,i);
//        }
//
        for(int i=1;i<=2*n;i++) if(a[i]!=a[i-1]) mp[++cnt]=a[i];
        for(int i=cnt;i>=1;i--) if(mp[i]!=mp[i-1]+1) mp[++cnt]=mp[i-1]+1;
        sort(mp+1,mp+1+cnt);
        for(int i=1;i<=n;i++){
            int ql=lower_bound(mp+1,mp+1+cnt,ll[i])-mp,qr=lower_bound(mp+1,mp+1+cnt,rr[i])-mp;
            draw(1,1,cnt,ql,qr,i);
        }
        ans=0;
        memset(vis,0,sizeof(vis));
        query(1,1,cnt,1,cnt);
        printf("%d\n",ans);

        //printf("\n\ncnt %d\n",cnt);
        //for(int i=1;i<=10;i++) printf("mp %d %d\n",i,mp[i]);
    }
}

POJ2528Mayor's posters[线段树 离散化]

时间: 2024-08-26 12:38:11

POJ2528Mayor's posters[线段树 离散化]的相关文章

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

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

POJ2528Mayor&#39;s posters 线段树,离散化技巧

题意:一个坐标轴从1~1e7,每次覆盖一个区间(li,ri),问最后可见区间有多少个(没有被其他区间挡住的) 线段树,按倒序考虑,贴上的地方记为1,每次看(li,ri)这个区间是否全是1,全是1就说明在它后面贴的把它给挡住了,否则该海报可见. 然后就愉快的MLE了.... 再看看数据范围,离散化如下,比如如果海报的左右端点如下 那图中橙色的一块的大小其实对结果没有影响,可以把他们都缩为1 最后离散化结果如下图: 代码: 1 #include <algorithm> 2 #include <

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

题目连接: http://poj.org/problem?id=2528 题目大意: 有10000000块瓷砖,n张海报需要贴在墙上,每张海报所占的宽度和瓷砖宽度一样,长度是瓷砖长度的整数倍,问按照所给海报顺序向瓷砖上贴海报,最后有几张海报是可见的? 解题思路: 因为瓷砖块数和海报张数多,首选线段树,如果按照常规的建树方式,把瓷砖当做数的节点,肯定会MTL......... 所以我们可以用海报的起点和终点当做树的节点,这样树的节点才有20000个,但是这样建树的话,求海报覆盖了那些节点会很复杂,

Poj2528Mayor&#39;s posters线段树

先贴,明天再补. #include <cstdio> #include <cstring> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #include <cstdlib> #include <list> #include <set> #inclu

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

题意:在一块木板上贴海报,每次贴海报给一个横坐标范围,在这个范围内贴,按照它给的顺序,海报可以被覆盖,问最后还能看见几张海报. 都说这是线段树入门题....结果我还是出翔了,不是在线段树部分,是在离散化部分. 我之前看到一个很飘逸的离散化写法,可惜找不到了,这回是这么写的:去重之后再把每个点的后一个值也加入离散化后的数组(如果这个值之前没有的话),这样避免了漏掉中间没被覆盖的情况. 然后样例都没调通,后来发现lowwer_bound返回值有可能是0,而我的线段树是以1为左边界的,每次这么更新难免

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 (线段树区间更新+离散化)

题目链接:http://poj.org/problem?id=2528 给你n块木板,每块木板有起始和终点,按顺序放置,问最终能看到几块木板. 很明显的线段树区间更新问题,每次放置木板就更新区间里的值.由于l和r范围比较大,内存就不够了,所以就用离散化的技巧 比如将1 4化为1 2,范围缩小,但是不影响答案. 写了这题之后对区间更新的理解有点加深了,重点在覆盖的理解(更新左右两个孩子节点,然后值清空),还是要多做做题目. 1 #include <iostream> 2 #include <

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

因为将每个单位都作为一个最小单元的话会爆内存的 所以,将海报的每个端点进行排序,将这些端点最为最小的区间. 毕竟是刚刚接触线段树,理解起来还有些吃力,还是那句话,题做多了慢慢就好了. 萌萌的AC代码君贴上. 1 //#define LOCAL 2 #include <iostream> 3 #include <algorithm> 4 #include <cmath> 5 using namespace std; 6 7 int n; 8 struct CPost 9

【线段树+离散化】POJ2528 Mayor&#39;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 post