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. 
输入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.输出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.
样例输入

1
5
1 4
2 6
8 10
3 4
7 10

样例输出

4

离散化 的大概思路 :   比如说给你一组 数据 1 4 1000 100000,  如果直接
                         开线段, 显然是浪费, 那么我们只要 进行 映射 :
                                1    1  
                                4    2
                             1000    3
                           100000    4
                         接下来 我们只要对 1 2 3 4 建立线段树就行了 只需要
                         [1,4]的区间     
离散化就相当于是先做映射,然后再建树。

本题大意:给定一些海报,可能相互重叠,告诉你每个海报的宽度(高度都一样的)和先后叠放顺序,问没有被完全盖住的有多少张?

海报最多10000张,但是墙有10000000块瓷砖长,海报不会落在瓷砖中间。

如果直接建树,就算不TLE,也会MLE。即单位区间长度太多。

其实10000张海报,有20000个点,最多有19999个区间。对各个区间编号,就是离散化。然后建数。

其实浮点数也是一样离散化的。

还有好多需要注意的地方。这题的线段树要四倍的,普通的三倍不行了。

细节决定成败:


#include<stdio.h>
#include<math.h>
#include<algorithm>
#include<iostream>
#include<string.h>
#include<stdlib.h>
using namespace std;
#define N 40005
#define Lson r<<1
#define Rson r<<1|1
int aa[N];
struct node
{
int L,R;
bool is;
int mid()
{
return (L+R)/;
}
}a[N*];
struct Node{int x,y;}p[N]; void BuildTree(int r,int L,int R)
{
a[r].L=L;
a[r].R=R;
a[r].is=false;
if(L==R)
return;
BuildTree(Lson,L,a[r].mid());
BuildTree(Rson,a[r].mid()+,R);
} void Up(int r)
{
if(a[r].L!=a[r].R)
{
if(a[Lson].is && a[Rson].is)
a[r].is=true;
}
}
bool Update(int r,int L,int R)
{
if(a[r].is==true)
return false;
if(a[r].L==L && a[r].R==R)
{
a[r].is=true;
return true;
}
bool sum; if(R<=a[r].mid())
sum=Update(Lson,L,R);
else if(L>a[r].mid())
sum=Update(Rson,L,R);
else
{
bool a1=Update(Lson,L,a[r].mid());
bool a2=Update(Rson,a[r].mid()+,R);
sum=a2+a1;
}
Up(r);
return sum;
}
int main()
{
int T,n,k=,i;
scanf("%d",&T);
while(T--)
{
int ans=;
k=;
scanf("%d",&n);
for(i=;i<=n;i++)
{
scanf("%d %d",&p[i].x,&p[i].y);
aa[k++]=p[i].x;
aa[k++]=p[i].x-;
aa[k++]=p[i].y;
aa[k++]=p[i].y+;
}
sort(aa,aa+k);
int len=unique(aa,aa+k)-aa;
BuildTree(,,len);
for(i=n;i>;i--)
{
int l=lower_bound(aa,aa+len,p[i].x)-aa;
int r=lower_bound(aa,aa+len,p[i].y)-aa;
if(Update(,l,r)==true)
ans++;
}
printf("%d\n",ans);
}
return ;
}

Mayor's posters-POJ2528(线段树+离散化)的更多相关文章

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

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

  2. D - Mayor's posters(线段树+离散化)

    题目: The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campai ...

  3. POJ2528 Mayor's posters(线段树+离散化)

    题意 : 在墙上贴海报, n(n<=10000)个人依次贴海报,给出每张海报所贴的范围li,ri(1<=li<=ri<=10000000).求出最后还能看见多少张海报. 分析 ...

  4. 【POJ 2528】Mayor’s posters(线段树+离散化)

    题目 给定每张海报的覆盖区间,按顺序覆盖后,最后有几张海报没有被其他海报完全覆盖.离散化处理完区间端点,排序后再给相差大于1的相邻端点之间再加一个点,再排序.线段树,tree[i]表示节点i对应区间是 ...

  5. poj2528 Mayor's posters (线段树+离散化)

    恩,这区间范围挺大的,需要离散化.如果TLE,还需要优化一下常数. AC代码 #include <stdio.h> #include <string.h> #include & ...

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

    这道题最关键的点就在离散化吧. 假如有三张海报[1, 10] [10, 13][15,  20] 仅仅三个区间就得占用到20了. 但是离散化后就可以是[1, 2] [2, 3] [4, 5] n到1e ...

  7. Mayor's posters(线段树+离散化+区间染色)

    题目链接:http://poj.org/problem?id=2528 题目: 题意:将n个区间进行染色(对于同一个区间,后一次染色会覆盖上一次的染色),问最后可见的颜色有多少种. 思路:由于区间长度 ...

  8. POJ-2528 Mayor's posters(线段树区间更新+离散化)

    http://poj.org/problem?id=2528 https://www.luogu.org/problem/UVA10587 Description The citizens of By ...

  9. POJ-2528 Mayor's posters (线段树区间更新+离散化)

    题目分析:线段树区间更新+离散化 代码如下: # include<iostream> # include<cstdio> # include<queue> # in ...

  10. poj2528(线段树+离散化)Mayor's posters

    2016-08-15 题意:一面墙,往上面贴海报,后面贴的可以覆盖前面贴的.问最后能看见几种海报. 思路:可以理解成往墙上涂颜色,最后能看见几种颜色(下面就是以涂色来讲的).这面墙长度为1~1000 ...

随机推荐

  1. Oracle 表-初步涉水不深(第一天)

    oracle 关系型数据库 注释:面对大型数据处理,市场占有率40%多(但是目前正往分布式转换) 故事:本来一台大型计算机才能处理的数据,美国科学家用100台家用电脑连接,成功处理了数据.. tabl ...

  2. CSS综合用法

    div 居中 {position: absolute; top: 50%; left: 50%; margin-top: -180px; margin-left: -160px;}

  3. Linux下Eclipse连接小米手机真机调试

    以前用Ubuntu 12.04可以真机调试,连上手机就可以了,上次看pear os 好看于是下了个玩玩(界面风格像mac 买不起,仿得起),这次想开发安卓发现真机调试不了了...于是乎各种找资料,各种 ...

  4. vscode显示php函数列表

    1.安装插件支持 https://marketplace.visualstudio.com/items?itemName=linyang95.php-symbols 2.ctrt+shift+o 即可 ...

  5. iOS----时间日期处理

    时间日期处理 1.NSDateFormatter 日期格式化 ①可以把NSString 类型转为 NSDate类型 举例 把 "2015-08-23 19:46:14" 转为NSD ...

  6. 到T-SQL DML 三级的阶梯:在SQL server中实现关系模型

    作者: Gregory Larsen, 2017/08/02 (第一次出版: 2011/11/09) 翻译:谢雪妮,许雅莉,赖慧芳,刘琼滨 译文: 系列 该文章是阶梯系列的一部分:T-SQL DML的 ...

  7. Farseer.net轻量级ORM开源框架 V1.x 入门篇:存储过程数据操作

    导航 目   录:Farseer.net轻量级ORM开源框架 目录 上一篇:Farseer.net轻量级ORM开源框架 V1.x 入门篇:存储过程实体类映射 下一篇:Farseer.net轻量级ORM ...

  8. genymotion 双击打开后 图标只显示在任务栏 无法在电脑上显示

    解决办法 删除 c:/users/user/AppData/local/Genymobile  例如:C:\Users\lenovo\AppData\Local\Genymobile 删除注册表:HK ...

  9. 阿里云报错Redirecting to /bin/systemctl restart sshd.service

    转:http://blog.csdn.net/caijunfen/article/details/70599138 云服务器 ECS Linux CentOS 7 下重启服务不再通过 service  ...

  10. Linux下启动tomcat报java.lang.OutOfMemoryError: PermGen space

    一.错误信息 java.lang.reflect.InvocationTargetException    at sun.reflect.NativeMethodAccessorImpl.invoke ...