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对应区间是 ...
随机推荐
- Vue.js 学习入门:介绍及安装
Vue.js 是什么? Vue (读音 /vjuː/,类似于 view) 是一套用于构建用户界面的渐进式框架.与其它大型框架不同的是,Vue 被设计为可以自底向上逐层应用.Vue 的核心库只关注视图层 ...
- 清华大学慕课 (mooc) 数据结构-邓俊辉-讲义-合并版
邓公的数据结构一直好评如潮,可惜我如今才开始学习它.QAQ 昨天,<数据结构 (2020 春)>的讲义已经推到清华大学云盘上了.苦于 10 拼页的打印版不易在 PC 上阅读(手机上更是如此 ...
- Python学习之Craps赌博游戏篇
在此先安利一波大佬的Python学习项目地址:https://github.com/jackfrued/Python-100-Days 这些天一直在看着大佬的项目学习Python,这是第五天循环学习完 ...
- Vue - 实现双击显示编辑框;自动聚焦点击的显示框;点击编辑框外的地方,隐藏编辑框
实现这三个功能的踩坑记录. 1. 需求 在Vue中,有一个input, 双击时编辑文本,点击该input节点外的其他地方,则取消编辑. 那么这里有三个要实现的地方 第一是双击显示编辑框. 第二是自动聚 ...
- Springmvc-crud-05(路径错误)
错误: 原因:Tomcat8之后的一些高版本,使用restful风格访问然后转发到jsp页面,进行业务操作时会报路径错误 解决方案①:修改jsp页面中的page指令isErrorPage=" ...
- 在IDEA离线安装lombok插件
1.打开,找到自己IDEA版本,idea http://plugins.jetbrains.com/plugin/6317-lombok/versions 2.下载,导入安装
- oracle 12cR2 RAC deconfig CRS过程记录
已经停了该节点的crs所有服务: [root@jhjzksrv02 install]# ./rootcrs.sh -force -deconfig -verboseUsing configuratio ...
- mui下拉刷新 ios click事件无法响应问题
使用mui的事件监听事件 tap mui("#pullrefresh").on('tap', '.ulDiv', function (event) {this.click();}) ...
- pdf .js和tableexport.js使用时遇到的2问题。
pdf .js 问题一:报错 network.js:71 The provided value 'moz-chunked-arraybuffer' is not a valid enum value ...
- python写入文件中遇到 UnicodeEncodeError: ‘gbk’ codec can’t encode character 错误的解决办法
在写入TXT文件时,某些页面总是报UnicodeEncodeError: ‘gbk’ codec can’t encode character错误,网上找了半天也没找到解决办法. 后来终于找到了解决办 ...