Mayor's posters
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 40570   Accepted: 11798

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. 1
  2. 5
  3. 1 4
  4. 2 6
  5. 8 10
  6. 3 4
  7. 7 10

Sample Output

  1. 4

题意:给n张海报能贴瓷砖的范围,问,最终可以看见多少张海报

心得:线段树的离散化的第一步,以后写线段树绝对要多建几个函数,好修改,看代码↓

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include <algorithm>
  5. #define Max 10005
  6. using namespace std;
  7. struct node{
  8. int l, r, m, s;
  9. }Tree[Max<<3];
  10. int li[Max], ri[Max];
  11. int point[Max<<2];
  12. int see[Max];
  13. int ans;
  14. int n, m, ni;
  15.  
  16. void Complex(){
  17. memset(li,0,sizeof(li));
  18. memset(ri,0,sizeof(ri));
  19. memset(point,0,sizeof(point));
  20. memset(see,0,sizeof(see));
  21. }
  22.  
  23. void Build(int l,int r,int k)
  24. {
  25. Tree[k].l = l;
  26. Tree[k].r = r;
  27. Tree[k].s = -1;
  28. Tree[k].m = (l+r)>>1;
  29. if(l==r) return;
  30. Build(Tree[k].m+1,r,k+k+1);
  31. Build(l,Tree[k].m,k+k);
  32. return;
  33. }
  34.  
  35. void update(int l, int r, int k)
  36. {
  37. if(Tree[k].l == l && Tree[k].r == r)
  38. {
  39. Tree[k].s = ni;
  40. return;
  41. }
  42. else if(Tree[k].s != -1)
  43. {
  44. Tree[k+k].s = Tree[k+k+1].s = Tree[k].s;
  45. Tree[k].s = -1;
  46. }
  47.  
  48. if(r <= Tree[k].m) update(l, r, k+k);
  49. else if(l > Tree[k].m) update(l, r, k+k+1);
  50. else
  51. {
  52. update(l, Tree[k].m, k+k);
  53. update(Tree[k].m + 1, r, k+k+1);
  54. }
  55. return;
  56. }
  57.  
  58. void Query(int k) //统计
  59. {
  60. if(Tree[k].s>=0)
  61. {
  62. if(!see[Tree[k].s]++) ans++;
  63. return;
  64. }
  65. Query(k+k);
  66. Query(k+k+1);
  67. return;
  68. }
  69.  
  70. int Bin(int xi) //二分查找特定点在关键点数字中的位置,为了更新线段树
  71. {
  72. int l = 1, r = m;
  73. while(l<=r)
  74. {
  75. int mi = (l+r)>>1;
  76. if(xi == point[mi]) return mi;
  77. if(xi >= point[mi]) l = mi + 1;
  78. else r = mi - 1;
  79. }
  80. return -1;
  81. }
  82.  
  83. void Myscanf() //统一输入,方便修改
  84. {
  85. ans = 0;
  86. m = 1;
  87. scanf("%d",&n);
  88. for(int i=0; i<n; i++)
  89. {
  90. scanf("%d%d",&li[i],&ri[i]);
  91. point[m++] = li[i];
  92. point[m++] = ri[i];
  93. }
  94. }
  95.  
  96. void Point_Do() //先排序在去重,达到离散的效果
  97. {
  98. int mi = 0;
  99. sort(point+1,point+1+m);
  100. for(int i=1; i<=m; i++)
  101. {
  102. if(point[i]!=point[mi]) point[++mi] = point[i]; //去重
  103. }
  104. m = mi;
  105. }
  106.  
  107. void Find_Point()
  108. {
  109. for(int i=0; i<n; i++)
  110. {
  111. ni = i;
  112. update(Bin(li[i]),Bin(ri[i]),1);
  113. }
  114. }
  115.  
  116. int main()
  117. {
  118. int num;
  119. scanf("%d",&num);
  120. while(num--)
  121. {
  122. Complex(); //初始化函数
  123. Myscanf(); //输入函数
  124. Point_Do(); //处理关键点函数
  125. Build(1,m,1); //建树函数
  126. Find_Point(); //对各个海报范围的处理函数
  127. Query(1); //统计函数
  128. printf("%d\n",ans); //输出结果
  129. }
  130.  
  131. return 0;
  132. }

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

Poj 2528 Mayor's posters 分类: Brush Mode 2014-07-23 09:12 84人阅读 评论(0) 收藏的更多相关文章

  1. MS SQLServer 批量附加数据库 分类: SQL Server 数据库 2015-07-13 11:12 30人阅读 评论(0) 收藏

    ************************************************************ * 标题:MS SQLServer 批量附加数据库 * 说明:请根据下面的注释 ...

  2. 修改MS SQL忽略大小写 分类: SQL Server 数据库 2015-06-19 09:18 34人阅读 评论(0) 收藏

    第一步:数据库->属性->选项->限制访问:SINGLE_USER 第二步:ALTER DATABASE [数据库名称] collate Chinese_PRC_CI_AI 第三步: ...

  3. *** glibc detected *** malloc(): memory corruption 分类: C/C++ Linux 2015-05-14 09:22 37人阅读 评论(0) 收藏

    *** glibc detected *** malloc(): memory corruption: 0x09eab988 *** 发现是由于memset越界写引起的. 在Linux Server上 ...

  4. Improving the GPA 分类: 贪心 HDU 比赛 2015-08-08 16:12 11人阅读 评论(0) 收藏

    Improving the GPA Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) ...

  5. vs2008 多人同时开发项目时的代码注释规范格式 分类: C#小技巧 2014-04-23 14:12 297人阅读 评论(0) 收藏

    多人同时开发一个项目,区分项目的那个窗体是谁开发的,例:下面的格式 /************************************************       模块:服务器设置   ...

  6. OC基础:继承.初始化方法,便利构造器 分类: ios学习 OC 2015-06-16 19:27 84人阅读 评论(0) 收藏

    继承: 1.单向继承,一个类只能有一个父类,一个父类可以有多个子类. 2.单向继承,基类(根类)是OSObject 3.子类可以继承父类的属性和方法 当父类的方法不满足子类的需求时,子类可以重写父类的 ...

  7. sscanf 函数 分类: POJ 2015-08-04 09:19 4人阅读 评论(0) 收藏

    sscanf 其实很强大 分类: 纯C技术 技术笔记 2010-03-05 16:00 12133人阅读 评论(4) 收藏 举报 正则表达式stringbuffercurlgoogle 最近在做日志分 ...

  8. Mahout快速入门教程 分类: B10_计算机基础 2015-03-07 16:20 508人阅读 评论(0) 收藏

    Mahout 是一个很强大的数据挖掘工具,是一个分布式机器学习算法的集合,包括:被称为Taste的分布式协同过滤的实现.分类.聚类等.Mahout最大的优点就是基于hadoop实现,把很多以前运行于单 ...

  9. Poj 2349 Arctic Network 分类: Brush Mode 2014-07-20 09:31 93人阅读 评论(0) 收藏

    Arctic Network Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9557   Accepted: 3187 De ...

随机推荐

  1. firefox 中碰到的一个小坑

    情况描述: 在一个处于正常文档流的div中,里面有一部分文字,还有个有浮动的块, 上代码 HTML: <div class="container">   this is ...

  2. Mongodb添加地理位置索引

    1.同步站点信息到mongo中(支持mysql.sqlserver数据同步) 2.在Collections文件夹下所在文档右键,在菜单中选择Add Index, 3.然后进行数据查询{ "m ...

  3. Java实现九九乘法表的输出

    九九乘法表一般为三角形,每个数分别和从1到自身的数相乘然后把结果列出来,即要用到两层循环,外层是从1到9for(i=1;i<=9;i++),内层是当前数和从1到自身相乘for(j=1;j< ...

  4. for循环绑定事件,闭包思想!

    1.选项卡问题 总结:用alert()测试,是否得到对象. 2.闭包,解决作用域. <script> window.onload=function(){ var li=document.g ...

  5. ok6410的madplay配置

    二.移植嵌入式播放器 madplay madplay 播放器程序主要依赖于如下库: zlib   zlib-1.1.4.tar.gz 提供数据压缩用的函式库 libid3tag  libid3tag- ...

  6. java遍历Map的几种方式

    1.遍历map的几种方式:private Hashtable<String, String> emails = new Hashtable<String, String>(); ...

  7. EMVTag系列15《选择应用响应数据》

    1. 接触交易选择应用响应数据 标签 长度 数据域 9102 A5 变长 FCI专用模板 强制 50 1–16 应用标签 纯电子现金:PBOC DEBIT 借记卡:PBOC DEBIT 贷记卡:PBO ...

  8. ORACLE 分区表简介

    参考链接:http://blog.csdn.net/ziwen00/article/details/9158725ORACLE-分区表 此文从以下几个方面来整理关于分区表的概念及操作: 1.表空间及分 ...

  9. 获得N位数字字母随机组合

    import string import random def get_rand(n): allw = string.letters+string.digits r = [] for i in ran ...

  10. mysql substring_index

    select * from tablename where substring_index(field1,'_',-1)=‘abc' #表中field1的值结构为123_abc