题意:

在一面长度为10000000 的墙上贴广告,告诉你每张海报的l,r(1 <= li <= ri <= 10000000.),让你求最后有几张海报露出来

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

思路:

由于数据较大,直接开数组不现实,所以我们考虑将每个点离散化,由于这里可能存在原本不相邻的点在离散化后变成相邻

例如三张海报分别为1-5,1-2,4-5,将数据离散化后1,2,4,5分别对应1,2,3,4,此时我们发现用离散化之后的数据得出来的结果

是2,但是实际上可以看到的海报为3,所以当a[i]-a[i-1]>1时,我们要再加入a[i-1]+1,这样就能避免上述情况。

所以,这道题就变成了离散化+线段树区间染色

代码:

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <vector>
#include <math.h>
#include <string.h>
#include <map>
using namespace std;
const int MAXN=5e4+;
typedef long long ll;
int l_[MAXN],r_[MAXN],lsh[MAXN<<],visit[MAXN],lazy[MAXN<<];
void push_down(int node)
{
lazy[node<<]=lazy[node];
lazy[node<<|]=lazy[node];
lazy[node]=;
}
void update(int node,int l,int r,int x,int y,int k)
{
if(x<=l&&y>=r)
{
lazy[node]=k;
return;
}
if(lazy[node])
push_down(node);
int mid=(l+r)>>;
if(x<=mid)
update(node<<,l,mid,x,y,k);
if(y>mid)
update(node<<|,mid+,r,x,y,k);
}
int ans=;
void query(int node,int l,int r,int x,int y)
{
if(lazy[node])
{
if(!visit[lazy[node]])
ans++,visit[lazy[node]]=;
return;
}
if(l==r)
return;
int mid=(l+r)>>;
if(x<=mid)
query(node<<,l,mid,x,y);
if(y>mid)
query(node<<|,mid+,r,x,y);
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
memset(lazy,,sizeof(lazy));
memset(visit,,sizeof(visit));
ans=;
int n;
scanf("%d",&n);
int index=;
for(int i=; i<=n; i++)
{
scanf("%d%d",&l_[i],&r_[i]);
lsh[index++]=l_[i],lsh[index++]=r_[i];
}
sort(lsh,lsh+index);
int temp=index;
for(int i=; i<temp; i++)
if(lsh[i]-lsh[i-]>)
lsh[index++]=lsh[i-]+;
int cnt=unique(lsh,lsh+index)-lsh;
for(int i=; i<=n; i++)
{
l_[i]=lower_bound(lsh,lsh+cnt,l_[i])-lsh+;
r_[i]=lower_bound(lsh,lsh+cnt,r_[i])-lsh+;
update(,,cnt,l_[i],r_[i],i);
}
query(,,cnt,,cnt);
printf("%d\n",ans);
}
return ;
}

Mayor's posters-POJ2528 区间染色+离散化的更多相关文章

  1. POJ2528 Mayor's posters —— 线段树染色 + 离散化

    题目链接:https://vjudge.net/problem/POJ-2528 The citizens of Bytetown, AB, could not stand that the cand ...

  2. POJ 2528 Mayor's posters(线段树区间染色+离散化或倒序更新)

    Mayor's posters Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 59239   Accepted: 17157 ...

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

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

  4. POJ 2528 Mayor's posters 【区间离散化+线段树区间更新&&查询变形】

    任意门:http://poj.org/problem?id=2528 Mayor's posters Time Limit: 1000MS   Memory Limit: 65536K Total S ...

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. 记一道简单的re--BUUctf reverse1

    1.首先拖进ida里,看到了左面一百多function...还是shift+f12 查看敏感字符串吧 2.发现了这两个比较可疑的字符串,然后双击this is the right flag 进入到了他 ...

  2. 修改环境变量后,导致一些常用命令失效,如ll,ls,vi不能用

    因为一不小心将linux的环境变量修改错误,导致ll之类的常用命令都不能用,很是苦恼,通过百度查询,原来在敲命令时,敲完整的命令路径,还是可以的: 原文地址:http://www.cnblogs.co ...

  3. Jmeter中cookie自动存储

    1,新建一个测试计划,然后添加一个"HTTP Cookie 管理器"(用来存储cookie)2,新建一个线程组,添加一个Sampler-->“HTTP 请求”(用来登录用的) ...

  4. collections模块、时间模块、random模块、os模块、sys模块、序列化模块、subprocess模块

    一.collections模块 1.其他数据类型 在内置数据类型(str.dict.list.tuple.set)的基础上,collections模块还提供了了几个额外的数据类型:Counter.de ...

  5. 类型不匹配 java.lang.IllegalArgumentException : argument type mismatch

    异常: 解决: money的类型是 float类型(把0.8改成 0.8f 即可)

  6. Fluent_Python_Part4面向对象,11-iface-abc,协议(接口),抽象基类

    第四部分第11章,接口:从协议到抽象基类(重点讲抽象基类) 接口就是实现特定角色的方法集合. 严格来说,协议是非正式的接口(只由文档约束),正式接口会施加限制(抽象基类对接口一致性的强制). 在Pyt ...

  7. beyond compare 用法

    1.过滤器用法:点击小眼睛(回话-->回话设置)打开过滤器界面----设置多个过滤文件或目录直接使用回车键 2.比较时最好先设置编码以防出现乱码问题 工具-->文件格式. 3.比较时出现乱 ...

  8. Pychram 运行程序在 run 窗口和 python console 窗口之间切换

    有图有真相 第一步: 第二步:

  9. Sqlmap 工具用法详解

    Sqlmap 工具用法详解                                 sqlmap是一款自动化的sql注入工具.     1.主要功能:扫描.发现.利用给定的url的sql注入漏 ...

  10. Unity2018编辑器脚本趟坑记录

    解除预制体问题:(这个例子是解除游戏中的Canvas与Asset中的预制体的关系) if( PrefabUtility.IsAnyPrefabInstanceRoot(GameObject.Find( ...