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对应区间是 ...
随机推荐
- go基础_接口断言
// interface package main import ( "fmt" ) //定义一个接口,接口名字Inter,接口的方法集有2个方法 type Inter inter ...
- SniperOj-compare_flag-Writeup
SniperOj-compare_flag-Writeup 题干如上,只给了一个nc命令,那么连接到服务器如下 有如下的python代码 #!/usr/bin/env python from time ...
- 一年读100本书---HHR,NZJ---19年最后4个月
那些自律到极致的人,都拥有了开挂的人生.生物钟,绝对一致之后,一切都会很高效. 19年最后一个季度的HHR计划:还剩下3个月的时间,主要搞定几件事情:创业(以太一堂,混沌大学),工作能力(推荐算法工程 ...
- ubuntu 先安装php再安装apache后,php不解析
本来服务器是nginx ,把他改成了apache, 安装apache, sudo apt-get updatesudo apt-get install apache2 然后访问ip,不成功 vim / ...
- 吴裕雄 python 机器学习——模型选择数据集切分
import numpy as np from sklearn.model_selection import train_test_split,KFold,StratifiedKFold,LeaveO ...
- tensorflow按需分配GPU问题
使用tensorflow,如果不加设置,即使是很小的模型也会占用整块GPU,造成资源浪费. 所以我们需要设置,使程序按需使用GPU. 具体设置方法: gpu_options = tf.GPUOptio ...
- Hadoop重新格式namenode后无法启动datanode的问题
这个很简单的哇~ 格式化namenode之后就会给namenode的ClusterId重新生成,导致与datanode中的ClusterId不一致而无法启动datanode 解决方法: 进入hadoo ...
- js中this,箭头函数和普通函数
四种基本用法 1. 一般方法中,this代指全局对象 window 2. 作为对象方法调用,this代指当前对象 3. 作为构造函数调用,this 指代new 出的对象 function test() ...
- IDEA中使用Springboot+SSM的踩坑记(一)
今天由于电脑无限蓝屏,不知怎么把我IDEA里面破解过的一些东西给搞没了,包括IDEA本体和JRebel,照着原来的方法破解连本体都开不起来了(哭死),索性下了个最新版来用,结果JRebel还是破解不得 ...
- ubuntu 虚拟机添加多个站点
我们安装好lamp环境,然后开始操作,比如一个站点叫test.ubuntu1.com,一个叫test.ubuntu2.com 1.修改hosts文件,路径/etc/hosts sudo vim /et ...