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

     这个题目本来对大神来说可能是水题, 对我就不行了,昨晚非折腾到下半夜一点 搞定, 并且可以总结出 ,只有把问题想清楚,或着看人家解题报告自己把问题和代码思路

搞清楚,才能谈的上调bug,否则根本就不知道错在哪儿。说说这个题目的理解,他是如何转化为线段树问题的呢?我们知道线段树有一个区间更新的东西,每张海报的宽度不就是一个区间么?那么我们可以用一棵树中的部分结点

来表示整张海报的可视部分,也就是说每个结点只允许表示一张完整的或着不完整的海报(好几个结点才可以表示成完整的一张海报),那么如何表示这个节点只被一张海报覆盖呢? 那么添加一个结点属性c,c > 0,表示一种海报覆盖,c == 0,表示多张或着没有任何一种海报覆盖,从根节点往下看,最上层的那一层 c > 0,的结点组合起来恰好就是整个墙面的可视化部分吧 ? 这是线段树所能表达出来的内容。

细节上:

是不是发现给的墙面宽度特别宽?所以我学会了离散化思想,把离散的点先按照大小关系排序,同时有必要记住每个点对(左右端点)的所属第几张海报,然后按照大小关系依次编号,这样确实能够维系每个点对之间的原来的关系

但是存在一个问题,人家的博客中给出了这样的样例

如三张海报为:1~10 1~4 6~10

离散化时 X[ 1 ] = 1, X[ 2 ] = 4, X[ 3 ] = 6, X[ 4 ] = 10

第一张海报时:墙的1~4被染为1;

第二张海报时:墙的1~2被染为2,3~4仍为1;

第三张海报时:墙的3~4被染为3,1~2仍为2。

最终,第一张海报就显示被完全覆盖了,于是输出2,但实际上明显不是这样,正确输出为3。

那么处理方法是在每个排序好的点之间如果发现某两点作差 > 1那么在之间添加新的结点,这样就可以避免上述错误,并且即使在一张海报点对之间多加了一个中间数也并不会影响结果,顶多离散化后这张海报宽度增加 1。

数据结构上用了一个dict二维数组 如dict【i】【j】表示第 i 副图画  第 j  个端点值, 用一个结构体Node表示每个端点值和所属第几个海报的信息。通过Node中的海报位置信息 我们还能把端点值还原到dict数组,此时dict数组就是离散化后的各张海报信息。如dict【node【i】.num】【0】 = node 【i】.val 就是第node【i】.num 张海报,值为此张海报左端点值。

另外值得一提的是不考虑上例提到的那种情况竟然也可以ac,那样就简单多了,但是我们要有一中求真务实的态度。

Mayor‘s posters

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 50053   Accepted: 14536

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

代码:

/*=============================================================================
#
#      Author: liangshu - cbam
#
#      QQ : 756029571
#
#      School : 哈尔滨理工大学
#
#      Last modified: 2015-08-10 12:59
#
#     Filename: B.cpp
#
#     Description:
#        The people who are crazy enough to think they can change the world, are the ones who do !
=============================================================================*/

#include<iostream>
#include<sstream>
#include<algorithm>
#include<cstdio>
#include<string.h>
#include<cctype>
#include<string>
#include<cmath>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
using namespace std;
const int INF = 10010;
struct Tree
{
    int l, r, c;
} tree[INF * 14];

struct Node
{
    int val,num;
} node[INF<<2];

set<int>cnt;

int cmp(Node a, Node b)
{
    return a.val < b.val;
}

void create(int t, int l, int r)
{
    tree[t].l = l;
    tree[t].r = r;
    tree[t].c = 0;
    if(l == r)
        return ;
    int a, b, mid = (l + r)>>1;
    create(t<<1, l, mid);
    create(t<<1|1, mid + 1, r);
}

void update(int t, int val, int l, int r)
{

    if(tree[t].l >= l && tree[t].r <= r)
    {
        tree[t].c = val;

        return ;
    }

    if(tree[t].c > 0)
    {
        tree[t<<1].c = tree[t].c;
        tree[t<<1|1].c =tree[t].c;
        tree[t].c = 0;

    }
    if(tree[t].l == tree[t].r)
        return ;
    int mid = (tree[t].l + tree[t].r) >>1;
//    if(l <= mid)
//    {
//        update(t<<1, val, l, r);
//    }
//    if(mid < r)
//    {
//        update(t<<1|1, val, l, r);
//    }

    if( l > mid)update(t<<1 | 1, val, l, r);
    else if(r <= mid)
        update(t<<1, val, l, r);
    else
    {
        update(t<<1,val, l, mid);
        update(t<<1|1,val, mid + 1, r);
    }
}

int flag[INF<<2];
int coun = 0;
void cal(int t)
{
    if(tree[t].c > 0)
    {
        if(!flag[tree[t].c])
        {
            coun++;
            flag[tree[t].c] = 1;
        }
        return ;
    }
    if(tree[t].l == tree[t].r)
        return ;
    cal(t<<1);
    cal(t<<1|1);
}

int main()
{
    int dict[INF][3];
    int t;
    cin>>t;
    int n,tx;
    while(t--)
    {
        memset(flag, 0, sizeof(flag));
        memset(dict, 0 ,sizeof(dict));
        coun = 0;
        tx = 1;
        scanf("%d", &n);
        for(int i = 1; i <= n; i++)
        {
            scanf("%d%d", &dict[i][0], &dict[i][1]);
            node[2 * i - 1]. val = dict[i][0];
            node[2 * i - 1].num = i;
            node[2 * i].val = dict[i][1];
            node[2 * i].num = -1 * i;
        }
        sort(node + 1, node + 2 * n  + 1 , cmp);
        if(n >= 2)
        {
            for(int i = 2; i <= 2 * n ; i += 1)
            {
                if(node[i].val - node[i-1].val > 1)
                {
                    node[2 * n  + tx].val = node[i].val - 1;
                    node[2 * n + 1 + tx].num = INF<<3;
                    tx++;
                }
            }
        }
        sort(node + 1, node + 2 * n  + tx  , cmp);

        int x = 1;
        dict[abs(node[1].num)][node[1].num > 0 ? 0 : 1] = x;
        for(int i = 2; i <= 2 *n +tx -1 ; i++)
        {
            if(node[i].val != node[i-1].val)
            {
                if(node[i].num == INF<<3)
                {
                    x++;
                    continue;
                }
                x++;
            }
            if(node[i].num > 0)
                dict[node[i].num][0] = x;
            else
                dict[-1 * node[i].num][1] = x;
        }
        create(1, 1, x);
        for(int i = 1; i<= n; i++)
        {
            update(1, i, dict[i][0], dict[i][1]);
        }
        cal(1);
        printf("%d\n",coun );

    }
    return 0;

}
4

版权声明:本文为博主原创文章,未经博主允许不得转载。

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

时间: 2024-10-28 09:58:39

poj 2528 Mayor's posters(线段树 离散化 区间更新 贴海报)的相关文章

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

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

POJ 2528 Mayor&#39;s posters 线段树成段更新+离散化

题目来源:POJ 2528 Mayor's posters 题意:很多张海报贴在墙上 求可以看到几张海报 看那两张图就行了 第一张俯视图 思路:最多2W个不同的数 离散化一下 然后成段更新 a[rt] = i代表这个区间是第i张报纸 更新玩之后一次query cover[i]=1代表可以看到第i张报纸 #include <cstdio> #include <algorithm> #include <cstring> using namespace std; const

POJ 2528 Mayor&#39;s posters(线段树,区间覆盖,单点查询)

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

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

poj 2528 Mayor&#39;s posters(线段树)

题目链接:http://poj.org/problem?id=2528 思路分析:线段树处理区间覆盖问题,也可以看做每次给一段区间染不同的颜色,最后求在整段区间上含有的所有颜色种类数: 注意由于区间太大,所以需要离散化: 区间更新:对于线段树的每个结点,标记颜色,初始时没有颜色,标记为0:当更新时,使用延迟标记,需要标记传递到子节点: 区间查询:使用深度优先查询线段树,当某个子节点的颜色不为0时,即停止深度优先搜索,并在map中查询是否已经记录该段区间的颜色: 代码如下: #include <i

poj 2528 Mayor&#39;s posters 线段树区间更新

Mayor's posters Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://poj.org/problem?id=2528 Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign have been placing their electoral posters at al

POJ训练计划2528_Mayor&#39;s posters(线段树/成段更新+离散化)

解题报告 地址传送门 题意: 一些海报,覆盖上去后还能看到几张. 思路: 第一道离散化的题. 离散化的意思就是区间压缩然后映射. 给你这么几个区间[1,300000],[3,5],[6,10],[4,9] 区间左右坐标排序完就是 1,3,4,5,6,9,10,300000; 1,2,3,4,5,6, 7 ,8; 我们可以把上面的区间映射成[1,8],[2,4],[5,7],[3,6]; 这样就节省了很多空间. 给线段染色, lz标记颜色. #include <map> #include <

poj 3225 Help with Intervals(线段树,区间更新)

Help with Intervals Time Limit: 6000MS   Memory Limit: 131072K Total Submissions: 12474   Accepted: 3140 Case Time Limit: 2000MS Description LogLoader, Inc. is a company specialized in providing products for analyzing logs. While Ikki is working on g

POJ 题目3667 Hotel(线段树,区间更新查询,求连续区间)

Hotel Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 13805   Accepted: 5996 Description The cows are journeying north to Thunder Bay in Canada to gain cultural enrichment and enjoy a vacation on the sunny shores of Lake Superior. Bessie