这个题目本来对大神来说可能是水题, 对我就不行了,昨晚非折腾到下半夜一点 搞定, 并且可以总结出 ,只有把问题想清楚,或着看人家解题报告自己把问题和代码思路
搞清楚,才能谈的上调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;elsedict[-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(线段树 离散化 区间更新 贴海报)相关推荐

  1. poj 2528 Mayor's posters (线段树+离散化)

    /*离散化+线段树由于 数据的输入最大是 10000000 ,直接用开数组肯点会超,所以要将起离散话,首先 ,我们存储输入的边,将其离散化,后面的就和一般的线段树一样可. */#include< ...

  2. POJ - 2528 Mayor's posters(线段数+离散化)

    题目链接:点击查看 题目大意:给定一个长度为1e7的墙,然后给出n张海报,每张海报都会占据墙上的一部分宽度,问按照给出的次序往墙上贴海报, 最后有几张海报能露出来(露出部分也算) 题目分析:线段树的区 ...

  3. POJ Mayor's posters——线段树+离散化

    原文:http://blog.163.com/cuiqiongjie@126/blog/static/85642734201261151553308/ 大致题意: 有一面墙,被等分为1QW份,一份的宽 ...

  4. POJ-2528 Mayor's posters 线段树+离散化 或 DFS

    题目大意 有 t 组数据,每组有 n 张(1<=n<=1e4)覆盖了 区间 [li,ri] 的海报(1<=i<=n,1<=li<=ri<=1e7),海报会由于 ...

  5. Find the median(线段树离散化+区间更新)

    题目链接:https://ac.nowcoder.com/acm/contest/887/E 链接:https://ac.nowcoder.com/acm/contest/887/E 来源:牛客网 F ...

  6. poj 2528 Mayor's posters(线段树+离散化)

    1 /* 2 poj 2528 Mayor's posters 3 线段树 + 离散化 4 5 离散化的理解: 6 给你一系列的正整数, 例如 1, 4 , 100, 1000000000, 如果利用 ...

  7. POJ 2528 Mayor's posters(线段树)

    题目大意 贴海报.每张海报的高度都是一样的,唯独宽度不一样.每张海报只能占用整数倍的单位线段长度,贴了 n(n<=10000) 张海报之后,有几张能够看见(有一个角能看见这张海报也算被看见了)? ...

  8. java-HDU1698(线段树的区间更新,和区间查询)

    HDU1698: 题目意思: Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Ja ...

  9. 离散化/线段树 (POJ - 2528 Mayor's posters)

    Mayor's posters https://vjudge.net/problem/POJ-2528#author=szdytom 线段树 + 离散化 讲解:https://blog.csdn.ne ...

最新文章

  1. 第十五周项目二-洗牌(范形程序设计)
  2. 示例Express中路由规则及获取请求参数
  3. lync 安装后相关防病毒软件的设置
  4. 数据结构——双向链表的实现
  5. 纪中B组模拟赛总结(2019.12.21)
  6. .sh文件是什么语言_关于Linux文件的一些基本命令和知识:
  7. iOS底层探索之LLVM(二)——自定义Clang插件(上)
  8. 如何优雅地给同事提“改进性建议”
  9. 技术与管理看看华为怎么说
  10. wps linux 字体目录在哪个文件夹,WPS OFFICE怎么添加字体?(我下载的字体文件应当放那个文件夹?)...
  11. 我的世界服务器如何开无限小号,实操神技能,微信能“无限”开小号?
  12. SEO页面优化以及如何对单页面应用进行SEO优化
  13. Smplayer命令行模式下的用法
  14. 怎么用浏览器访问计算机文件共享,如何用ES文件浏览器完全共享电脑里的文件流程...
  15. vs2017无法解析外部符号__imp__fprintf和__imp____iob_func
  16. 《联邦学习介绍》(科研汇报PPT,针对无机器学习基础的同学,浅显理解)
  17. 2022年旅游网站开发功能分析、开发意义在哪
  18. android 脸部识别之3D,这两款安卓手机也支持3D结构光人脸识别
  19. java-php-python-ssm校园疫情防控管理系统计算机毕业设计
  20. 一键式打造 DAO,M-DAO 或成 Web3 新宠儿

热门文章

  1. Google Earth Engine (GEE)——利用两种方式进行EVI指数(含函数的两种定义方式)
  2. PTA查找最后一个250 (20 分)
  3. 亚洲第一人,人肉扫码器,怎样提高记忆力
  4. 来上海的第五天,阿里巴巴前端实习生电话面试整理
  5. 金融知识小科普 - 央行逆回购
  6. 计算机和正餐的英语发音,晚饭的英文,早餐,午餐,晚餐英语怎么读!
  7. 龙珠 超宇宙 [Dragon Ball Xenoverse]
  8. 水利RTU遥测终端机厂家
  9. 【算法竞赛学习笔记】Link-Cut-Tree基础-超好懂的数据结构
  10. 超详细 某代刷网站js逆向