(中等) POJ 2528 Mayor'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 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.

  题目大致就是说给一个线段覆盖线段,问最后没有被完全覆盖的线段的个数。

  这是第一次写离散,也是第三次写线段树的区间更新,想了好久,发现是个空线段树,只需要COL,即标记,不需要BIT来保存啥数据。

  离散的话是因为数据范围太大,只取用到的,排序后分别编号为1...N ,再用。

  (注:这个题要注意,离散的话比如3和7分别标号为4和5的话,那么如果第一个覆盖1-20,第二个覆盖1-3 , 另一个覆盖 7-10 的话,那么编号4和5都被覆盖,结果将为2,而不是正确的3 。 所以可以按照杭电那位大神的处理方法,只要3和7之间超过1,那么再加一个数4,这样的话分别编号为4,5,6就好了。)

  标记记录每次更新的为编号几。

代码如下:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>

#define lson L,M,po*2
#define rson M+1,R,po*2+1

using namespace std;

int COL[40004*4];
int num[20004];
int num1[20004];
int rnum[40004];
int cou;
bool vis[10004];

int findH(int x,int L,int R)
{
    int M=(L+R)/2;

    if(rnum[M]==x)
        return M;

    if(rnum[M]<x)
        return findH(x,M+1,R);
    else
        return findH(x,L,M);
}

void pushDown(int po)
{
    if(COL[po])
    {
        COL[po*2]=COL[po];
        COL[po*2+1]=COL[po];

        COL[po]=0;
    }
}

void update(int ul,int ur,int cha,int L,int R,int po)
{

    if(ul<=L&&ur>=R)
    {
        COL[po]=cha;

        return;
    }

    pushDown(po);

    int M=(L+R)/2;

    if(ul<=M)
        update(ul,ur,cha,lson);
    if(ur>M)
        update(ul,ur,cha,rson);

}

void query(int L,int R,int po)
{
    if(L==R)
    {
        if(COL[po])
            vis[COL[po]]=1;

        return;
    }

    pushDown(po);

    int M=(L+R)/2;

    query(lson);
    query(rson);
}

int main()
{
    int T,N;
    int a,b;
    int ans=0;
    cin>>T;

    while(T--)
    {
        memset(COL,0,sizeof(COL));
        memset(vis,0,sizeof(vis));
        scanf("%d",&N);

        ans=0;

        for(int i=0;i<N;++i)
        {
            scanf("%d %d",&num[i*2],&num[i*2+1]);
            num1[i*2]=num[i*2];
            num1[i*2+1]=num[i*2+1];
        }

        sort(num,num+2*N);

        rnum[0]=num[0];
        cou=1;
        for(int i=1;i<N*2;++i)
        {
            if(num[i]==num[i-1])
                continue;

            if(num[i]-num[i-1]>1)
                rnum[cou++]=num[i-1]+1;

            rnum[cou++]=num[i];
        }

        for(int i=0;i<N;++i)
        {
            a=findH(num1[i*2],0,cou-1);
            b=findH(num1[i*2+1],0,cou-1);

            update(a+1,b+1,i+1,1,cou,1);
        }

        query(1,cou,1);

        for(int i=1;i<=N;++i)
            if(vis[i])
                ++ans;

        printf("%d\n",ans);
    }

    return 0;
}

(中等) POJ 2528 Mayor's posters , 离散+线段树。

时间: 2024-08-05 20:56:22

(中等) POJ 2528 Mayor's posters , 离散+线段树。的相关文章

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

Mayor's posters Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 45031   Accepted: 13080 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 (hash+线段树成段更新)

题意:有一面墙,被等分为1QW份,一份的宽度为一个单位宽度.现在往墙上贴N张海报,每张海报的宽度是任意的,但是必定是单位宽度的整数倍,且<=1QW.后贴的海报若与先贴的海报有交集,后贴的海报必定会全部或局部覆盖先贴的海报.现在给出每张海报所贴的位置(左端位置和右端位置),问张贴完N张海报后,还能看见多少张海报?(PS:看见一部分也算看到.) 思路:简单的成段更新,但是数据量是1千万,会MT,所以要区间压缩(离散化),保证覆盖的关系不变,离散化的时候有个易错的细节,poj数据水了,这个易错点引用h

POJ 2528——Mayor&#39;s posters——————【线段树区间替换、找存在的不同区间】

Mayor's posters Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2528 Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign have been plac

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

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

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(离散化线段树)

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

题目链接: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 所以,离散化处理时要处理边界,