Mayor's posters-POJ2528 区间染色+离散化
题意:
在一面长度为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 区间染色+离散化的更多相关文章
- POJ2528 Mayor's posters —— 线段树染色 + 离散化
题目链接:https://vjudge.net/problem/POJ-2528 The citizens of Bytetown, AB, could not stand that the cand ...
- POJ 2528 Mayor's posters(线段树区间染色+离散化或倒序更新)
Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 59239 Accepted: 17157 ...
- Mayor's posters(线段树+离散化+区间染色)
题目链接:http://poj.org/problem?id=2528 题目: 题意:将n个区间进行染色(对于同一个区间,后一次染色会覆盖上一次的染色),问最后可见的颜色有多少种. 思路:由于区间长度 ...
- POJ 2528 Mayor's posters 【区间离散化+线段树区间更新&&查询变形】
任意门:http://poj.org/problem?id=2528 Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Total S ...
- poj 2528 Mayor's posters(线段树+离散化)
/* poj 2528 Mayor's posters 线段树 + 离散化 离散化的理解: 给你一系列的正整数, 例如 1, 4 , 100, 1000000000, 如果利用线段树求解的话,很明显 ...
- poj2528 Mayor's posters (线段树+离散化)
恩,这区间范围挺大的,需要离散化.如果TLE,还需要优化一下常数. AC代码 #include <stdio.h> #include <string.h> #include & ...
- POJ2528 Mayor's posters(线段树+离散化)
题意 : 在墙上贴海报, n(n<=10000)个人依次贴海报,给出每张海报所贴的范围li,ri(1<=li<=ri<=10000000).求出最后还能看见多少张海报. 分析 ...
- D - Mayor's posters(线段树+离散化)
题目: The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campai ...
- 【POJ 2528】Mayor’s posters(线段树+离散化)
题目 给定每张海报的覆盖区间,按顺序覆盖后,最后有几张海报没有被其他海报完全覆盖.离散化处理完区间端点,排序后再给相差大于1的相邻端点之间再加一个点,再排序.线段树,tree[i]表示节点i对应区间是 ...
随机推荐
- Codeforces Round #624 (Div. 3) F
题意: 给出n的质点,带着初位置和速度: 如果中途两点可以相遇dis(i,j)=0: 如果不可以相遇,mindis(i,j): 求n个点的两两质点最小dis(i,j)之和 思路: 因为当初位置x和速度 ...
- 【ES6新增语法详述】
目录 1. 变量的定义 let const 2. 模版字符串 3. 数据解构 4. 函数扩展 设置默认值 箭头函数 5. 类的定义 class 6. 对象的单体模式 "@ ES6新增了关于变 ...
- Azure虚拟机网站部署 防火墙设置
唯一需要注意的是当你的网站设置的端口不是默认的80的时候,需要在防火墙那里将你新设置的端口设置为allow 先要到云的后台设置 “入站安全规则”--> 将你的网站端口设置为Allow 1.通过 ...
- JAVA基础学习(4)之循环控制
4循环控制 4.1 for循环 4.1.1 for循环 固定次数for循环 先执行一次do-while循环 其他while循环 Scanner in = new Scanner(System.in); ...
- android nfc功能开发
链接:Android NFC开发详细总结 https://blog.csdn.net/zhwadezh/article/details/79111348 链接2:Android NFC功能 简单实 ...
- 原生js登录创建cookie
原生js创建cookie,功能:点击登录按钮时,将用户名.密码存为cookie:页面再次加载时,自动读取cookie中的用户名.密码. <html><head><titl ...
- Go_排序
package main import ( "fmt" "sort" "math/rand" ) //1.声明Hero结构体 type He ...
- vue项目打包后运行报错400如何解决
昨天一个Vue项目打包后,今天测试,发现无论localhost还是服务器上都运行不了,报错如下: Failed to load resource: the server responded with ...
- cnpm - 解决 " cnpm : 无法加载文件 C:\Users\93457\AppData\Roaming\npm\cnpm.ps1,因为在此系统上禁止运行脚本。有关详细信息 。。。 "
1.在win10 系统中搜索框 输入 Windos PowerShell选择 管理员身份运行 2,打开了powershell命令行之后,输入 set-ExecutionPolicy RemoteSig ...
- Centos610安装Archiva
安装说明: https://www.cwiki.us/display/ArchivaZH/Linux+Installing+Standalone 1.下载地址 https://archiva.apac ...