题目链接:http://poj.org/problem?id=2528

题意:有n张海报要贴,每张需要用的区间为L到R,后面的可以贴在之前的上面,就是吧之前的挡住,求最后我们能看到几张海报;

我们可以倒着处理,因为最后贴的我们是能看到的;如果区间被贴过了result不加,没有贴过就+1并标记一下;

由于数据范围太大所以用线段树

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<algorithm>
using namespace std; #define N 10100
#define Lson r<<1
#define Rson r<<1|1 struct Post
{
int L,R;
}p[N]; struct SegTree
{
int L, R, isCover;
int Mid()
{
return (L+R)/;
}
}a[N*]; int Hash[*N]; void Up(int r)
{
if(a[r].L!=a[r].R)
if(a[Lson].isCover== && a[Rson].isCover==)//向上更新,如果左右子树都被覆盖,那么他也会被覆盖
a[r].isCover = ;
} void BuildSegTree(int r, int L, int R)
{
a[r].isCover = ;
a[r].L = L; a[r].R = R;
if(L == R)
return ;
BuildSegTree(Lson, L, a[r].Mid());
BuildSegTree(Rson, a[r].Mid()+, R);
}
int judge(int r, int L, int R)
{
if(a[r].isCover==)
return ;//被覆盖返回0;
if(a[r].R == R && a[r].L == L)
{
a[r].isCover = ;
return ;
}
int ans;
if(L>a[r].Mid())
ans = judge(Rson, L, R);
else if(R<=a[r].Mid())
ans = judge(Lson, L, R);
else
{
int ans1 = judge(Lson, L, a[r].Mid());
int ans2 = judge(Rson, a[r].Mid()+, R);
ans = ans1 | ans2;//只要有一个没被覆盖就说明能贴;
}
Up(r);
return ans;
}
int main()
{
int T, n, k;
scanf("%d", &T);
while(T--)
{
scanf("%d", &n);
k=;
for(int i=; i<=n; i++)
{
scanf("%d%d", &p[i].L, &p[i].R);
Hash[k++]=p[i].L-;//防止出现离散化后原本不相邻的数变成相邻的了
Hash[k++]=p[i].L;
Hash[k++]=p[i].R+;
Hash[k++]=p[i].R;
}
sort(Hash, Hash+k);
int len = unique(Hash, Hash+k) - Hash;
BuildSegTree(, , len);
int result = ;
for(int i=n; i>; i--)
{
int L = lower_bound(Hash, Hash+len, p[i].L) - Hash;
int R = lower_bound(Hash, Hash+len, p[i].R) - Hash; int ans=judge(, L, R);
if(ans == )
result++;
}
printf("%d\n", result);
}
return ;
}

 过了那么久再写一遍

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<stack>
using namespace std; #define INF 0xfffffff
#define N 100050
#define Lson r<<1
#define Rson r<<1|1 struct SegmentTree
{
int L, R;
bool IsCover;///判断此区间是否被覆盖; int Mid() { return (L+R)>>;}
int len() { return R-L+; } } a[N<<]; void Build(int r, int L, int R)
{
a[r].L = L, a[r].R = R;
a[r].IsCover = false; if(L == R) return; Build(Lson, L, a[r].Mid());
Build(Rson, a[r].Mid()+, R); a[r].IsCover = a[Lson].IsCover && a[Rson].IsCover;
} int Query(int r, int L, int R)
{
int ans;
if(a[r].IsCover == true)
a[Lson].IsCover = a[Rson].IsCover = a[r].IsCover; if(a[r].IsCover == true)///被覆盖了,返回0;
return ;
if(a[r].L == L && a[r].R == R)///没被覆盖,把它覆盖,并返回1;
{
a[r].IsCover = true;
return ;
} if(R<=a[r].Mid())
ans = Query(Lson, L, R);
else if(L>a[r].Mid())
ans = Query(Rson, L, R);
else
{
int ans1 = Query(Lson, L, a[r].Mid());
int ans2 = Query(Rson, a[r].Mid()+, R);
ans = (ans1 || ans2);///只要有一边没被覆盖就说明能贴;
}
a[r].IsCover = a[Lson].IsCover && a[Rson].IsCover;///向上更新,因为有了这个更新所以不能在上面直接返回结果;
return ans;
} struct node
{
int L, R;
}p[N]; int Hash[N*], cnt; int main()
{
int T, n, L, R;
scanf("%d", &T);
while(T--)
{
cnt = ;
scanf("%d", &n);
for(int i=; i<=n; i++)
{
scanf("%d %d", &p[i].L, &p[i].R);
Hash[cnt++] = p[i].L; Hash[cnt++] = p[i].R;
Hash[cnt++] = p[i].L-; Hash[cnt++] = p[i].R+;
///加上左右边界是为了防止出现本身没有相连的,而离散化之后相连的情况;
}
sort(Hash, Hash+cnt);
cnt = unique(Hash, Hash+cnt)-Hash; Build(, , cnt); int ans = ; for(int i=n; i>=; i--)
{
L = lower_bound(Hash, Hash+cnt, p[i].L) - Hash;
R = lower_bound(Hash, Hash+cnt, p[i].R) - Hash;
ans += Query(, L, R);
}
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. 执行RF测试只生成output.xml文件,不生成log和report文件

    命令格式: -l --log file HTML log file. Can be disabled by giving a special value `NONE`. Default: log.ht ...

  2. Ansible 如何查看模块文档

    [root@localhost ~]$ ansible-doc -l # 列出所有模块 [root@localhost ~]$ ansible-doc cron # 查看指定模块的文档

  3. javaWeb项目重命名的问题

    tomcat项目名称修改 步骤: 1.对工程重命名(选择工程,右键Refactor->Rename)      2.修改Web路径(选择工程,右键Properties->MyEclipse ...

  4. Matlab 图像处理入门

    一些(形态学)图像处理方法如下: I = imread('pic.jpg'); I2 = rgb2gray(I); figure,imshow(I); title('原图'); figure,imsh ...

  5. 【docker】 VI/VIM 无法使用系统剪贴板(clipboard)

    docker 容器里边操作系统是ubuntu .默认是没有vim 的,需要自己安装一下 1 更新源 apt-get update 2 安装 vim apt-get install vim 此时.系统不 ...

  6. android第三方---->android智能机器人的使用

    在网上找了个第三方智能机器人,可以实现聊天语音等功能,比较不错的.今天我们就开始智能机器人聊天的学习,例子中涉及的handler的有关知识和json数据的解析,请参见我的博客:android基础--- ...

  7. 原生js--编码请求主体(异步请求)

    1.表单编码请求 需要对每个表单元素进行普通的URL编码,使用“=”把编码后的名字和值分开,并使用“&”分开名值对. 例如:a=b&c=d 表单数据编码的MIME类型:applicat ...

  8. mac Intellij Idea Tmocat 启动报 Error running Tomcat: /conf/Catalina

    原因:主要是tomcat下Catalina目录没有权限导致,将其设置读写权限即可 如果在刚刚启动tomcat时出现以下问题:Error running Tomcat 8.5.31: Error cop ...

  9. sencha touch Container

    Container控件是我们在实际开发中最常用的控件,大部分视图控件都是继承于Container控件,了解此控件能帮我们更好的了解sencha touch. layout是一个很重要的属性,能够帮助你 ...

  10. nginx 日志文件

    默认日志格式 log_format access '$remote_addr - $remote_user [$time_local] "$request" ' '$status ...