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

Sample Output

4

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

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

#include<iostream>
#include<cstdio>
#include<cstring>
#include <algorithm>
#define Max 10005
using namespace std;
struct node{
int l, r, m, s;
}Tree[Max<<3];
int li[Max], ri[Max];
int point[Max<<2];
int see[Max];
int ans;
int n, m, ni; void Complex(){
memset(li,0,sizeof(li));
memset(ri,0,sizeof(ri));
memset(point,0,sizeof(point));
memset(see,0,sizeof(see));
} void Build(int l,int r,int k)
{
Tree[k].l = l;
Tree[k].r = r;
Tree[k].s = -1;
Tree[k].m = (l+r)>>1;
if(l==r) return;
Build(Tree[k].m+1,r,k+k+1);
Build(l,Tree[k].m,k+k);
return;
} void update(int l, int r, int k)
{
if(Tree[k].l == l && Tree[k].r == r)
{
Tree[k].s = ni;
return;
}
else if(Tree[k].s != -1)
{
Tree[k+k].s = Tree[k+k+1].s = Tree[k].s;
Tree[k].s = -1;
} if(r <= Tree[k].m) update(l, r, k+k);
else if(l > Tree[k].m) update(l, r, k+k+1);
else
{
update(l, Tree[k].m, k+k);
update(Tree[k].m + 1, r, k+k+1);
}
return;
} void Query(int k) //统计
{
if(Tree[k].s>=0)
{
if(!see[Tree[k].s]++) ans++;
return;
}
Query(k+k);
Query(k+k+1);
return;
} int Bin(int xi) //二分查找特定点在关键点数字中的位置,为了更新线段树
{
int l = 1, r = m;
while(l<=r)
{
int mi = (l+r)>>1;
if(xi == point[mi]) return mi;
if(xi >= point[mi]) l = mi + 1;
else r = mi - 1;
}
return -1;
} void Myscanf() //统一输入,方便修改
{
ans = 0;
m = 1;
scanf("%d",&n);
for(int i=0; i<n; i++)
{
scanf("%d%d",&li[i],&ri[i]);
point[m++] = li[i];
point[m++] = ri[i];
}
} void Point_Do() //先排序在去重,达到离散的效果
{
int mi = 0;
sort(point+1,point+1+m);
for(int i=1; i<=m; i++)
{
if(point[i]!=point[mi]) point[++mi] = point[i]; //去重
}
m = mi;
} void Find_Point()
{
for(int i=0; i<n; i++)
{
ni = i;
update(Bin(li[i]),Bin(ri[i]),1);
}
} int main()
{
int num;
scanf("%d",&num);
while(num--)
{
Complex(); //初始化函数
Myscanf(); //输入函数
Point_Do(); //处理关键点函数
Build(1,m,1); //建树函数
Find_Point(); //对各个海报范围的处理函数
Query(1); //统计函数
printf("%d\n",ans); //输出结果
} return 0;
}

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

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. 基于HTML5 的人脸识别活体认证

    近几年,人脸识别技术在身份认证领域的应用已经有了较多应用,例如:支付宝.招行的取款.养老金领取等方面,但在杜绝假冒.认证安全性等方面,目前还是一个比较需要进一步解决的课题,特别是在移动端的活体认证技术 ...

  2. Linux rabbitmq的安装和安装amqp的php插件

    RabbitMQ是一个消息代理.它的核心原理非常简单:接收和发送消息.你可以把它想像成一个邮局:你把信件放入邮箱,邮递员就会把信件投递到你的收件人处.在这个比喻中,RabbitMQ是一个邮箱.邮局.邮 ...

  3. dede 单表模型动态浏览设置 arclistsg arc.sglistview.class.php

    比如,分类信息,内容页默认都是生成静态的,如果有需要设为动态访问的,可以参考如下方法: arclistsg 标签,单表独立模型的文档列表调用标记 方法一: 在arclist标签使用中,其对应主表ded ...

  4. hadoop2—namenode—HA原理详解

    在hadoop1中NameNode存在一个单点故障问题,也就是说如果NameNode所在的机器发生故障,那么整个集群就将不可用(hadoop1中有个SecorndaryNameNode,但是它并不是N ...

  5. 第二十章 数据访问(In .net4.5) 之 使用LINQ

    1. 概述 .net3.5中新添加给C#的LINQ查询,提供了直观便捷的数据查询方式.并且支持多种数据源的查询. 本章介绍标准的LINQ操作,如何用最优的方式使用LINQ 以及 LINQ to XML ...

  6. WPF实现3D翻转的动画效果

    1.前端代码实现 1.1 原理见代码注析 <Grid MouseDown="Grid_MouseDown"> <Viewport3D> <Viewpo ...

  7. chrome下的js调试

    console输出对象信息:console.log(object[,object,.....])

  8. 安装Mongodb3.0.6单实例

    [root@b28-17-51 ~]#mkdir -p /export/data [root@b28-17-51 ~]#mkdir -p /export/log [root@b28-17-51 exp ...

  9. Word2013死机的问题

    Word2013用公式编辑器经常死机,网上介绍方法.试试看https://zhidao.baidu.com/question/937023473910649252.html1.启动word2013 2 ...

  10. Swift TabeleViewCell dequeueReusableCellWithIdentifier 使用的新的细节,原来现在可以这样

    今天在看官方的TableView Guide,突然想起来最近写的一个代码中实现tableViewCell复用的时候有点问题: var cell = UITableViewCell(style: UIT ...