POJ 2528 Mayor's posters (线段树+区间覆盖+离散化)
题意: 一共有n张海报, 按次序贴在墙上, 后贴的海报可以覆盖先贴的海报, 问一共有多少种海报出现过。
题解: 因为长度最大可以达到1e7, 但是最多只有2e4的区间个数,并且最后只是统计能看见的不同海报的数目,所以可以先对区间进行离散化再进行区间覆盖的操作。
由于墙上不贴东西的时候对后面没有影响, 所以可以不建树, 直接memset一下就好了。
因为是区域覆盖的问题, 树上原来的点并不会对后面的结果产生影响, 所以可以只修改lazy标记而不对树进行修改。
最后再用建树的操作访问一下lazy标记 并记录答案就好了。
代码
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
const int N = +;
int tree[N<<], lazy[N<<];
int a[N];
bool vis[N];
int ans, n, t;
pair<int,int> P[N]; void Pushdown(int rt)
{
if(lazy[rt])
{
lazy[rt<<|] = lazy[rt<<] = lazy[rt];
lazy[rt] = ;
}
}
void Revise(int L, int R, int C ,int l, int r, int rt)
{
if(L <= l && r <= R)
{
lazy[rt] = C;
return ;
}
Pushdown(rt);
int m = l+r >> ;
if(L <= m) Revise(L,R,C,lson);
if(m < R) Revise(L,R,C,rson);
}
void build(int l, int r, int rt)
{
if(lazy[rt])
{
if(!vis[lazy[rt]])
{
vis[lazy[rt]] = ;
ans++;
}
return ;
}
int m = l+r >> ;
build(lson);
build(rson);
}
int main()
{
ios::sync_with_stdio(false);
cin.tie();
cout.tie();
cin >> t;
while(t--)
{
ans = ;
cin >> n;
for(int i = ; i <= *n; i++)
{
cin >> a[i];
P[i].fi = a[i], P[i].se = i;
}
sort(P+,P+*n+);
int last = , cnt = ;
for(int i = ; i <= *n; i++) //离散化操作
{
if(P[i].fi == last)
a[P[i].se] = cnt;
else a[P[i].se] = ++cnt, last = P[i].fi;
}
memset(lazy, , sizeof(lazy));
for(int i = ; i <= *n; i+=)
Revise(a[i],a[i+],(i+)/,,cnt,);//由于每次的海报不同所以直接给海报一个编号
memset(vis, , sizeof(vis));
build(,cnt,);
cout << ans << endl;
}
return ;
}
POJ 2528 Mayor's posters (线段树+区间覆盖+离散化)的更多相关文章
- POJ 2528 Mayor's posters (线段树区间更新+离散化)
题目链接:http://poj.org/problem?id=2528 给你n块木板,每块木板有起始和终点,按顺序放置,问最终能看到几块木板. 很明显的线段树区间更新问题,每次放置木板就更新区间里的值 ...
- POJ 2528 Mayor's posters(线段树,区间覆盖,单点查询)
Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 45703 Accepted: 13239 ...
- POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化)
POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化) 题意分析 贴海报,新的海报能覆盖在旧的海报上面,最后贴完了,求问能看见几张海报. 最多有10000张海报,海报 ...
- poj 2528 Mayor's posters 线段树区间更新
Mayor's posters Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://poj.org/problem?id=2528 Descript ...
- POJ 2528 Mayor’s posters (线段树段替换 && 离散化)
题意 : 在墙上贴海报, n(n<=10000)个人依次贴海报,给出每张海报所贴的范围li,ri(1<=li<=ri<=10000000).求出最后还能看见多少张海报. 分析 ...
- poj 2528 Mayor's posters 线段树+离散化技巧
poj 2528 Mayor's posters 题目链接: http://poj.org/problem?id=2528 思路: 线段树+离散化技巧(这里的离散化需要注意一下啊,题目数据弱看不出来) ...
- POJ2528:Mayor's posters(线段树区间更新+离散化)
Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral electio ...
- POJ 2528 Mayor's posters(线段树+离散化)
Mayor's posters 转载自:http://blog.csdn.net/winddreams/article/details/38443761 [题目链接]Mayor's posters [ ...
- poj 2528 Mayor's posters 线段树+离散化 || hihocode #1079 离散化
Mayor's posters Description The citizens of Bytetown, AB, could not stand that the candidates in the ...
随机推荐
- Cell Phone Networ (树形dp-最小支配集)
目录 Cell Phone Networ (树形dp-最小支配集) 题意 思路 题解 Cell Phone Networ (树形dp-最小支配集) Farmer John has decided to ...
- 记一次python时间格式转换遇到的坑
需求:拿到指定格式的时间的前一天的时间,如果今天是月初,年初,自动转换,比如:输入时间是:2019-06-27 23:59:59输出时间是:2019-06-26 23:59:59 之前用datetim ...
- SpringMVC学习笔记之---深入使用
SpringMVC深入使用 (一)基于XML配置的使用 (1)配置 1.SpringMVC基础配置 2.XML配置Controller,HandlerMapping组件映射 3.XML配置ViewRe ...
- 我的第一个py爬虫-小白(beatifulsoup)
一.基本上所有的python第一步都是安装.安装 我用到的第三方安装包(beatifulsoup4.re.requests).还要安装lxml 二.找个http开头的网址我找的是url="h ...
- 【Java例题】1.4圆类
4.定义一个圆类,包括半径.构造方法.计算周长方法, 计算面积方法和显示半径方法. 然后编写一个主类,在其主方法中通过定义一个圆对象来 显示圆的半径.周长和面积. package study; imp ...
- hadoop学习(六)----HDFS的shell操作
HDFS所有命令: [uploaduser@rickiyang ~]$ hadoop fs Usage: hadoop fs [generic options] [-appendToFile < ...
- Pipeline 模型
解决的问题 解决并发效率问题,将任务拆分成流水线,然后多线程并发执行,比之单线程执行快. 案例 CPU 流水线 Tomcat 容器 Structs
- python面试总结1(基础章节)
python语言基础 语言特点 python是静态还是动态类型?是强类型还是弱类型 动态强类型语言 动态还是静态指的是编译期还是运作期确定类型 强类型指的是不会发生隐式类型转换 python作为后端语 ...
- C#之BackgroundWorker从简单入门到深入精通的用法总结
需求分析 经常用到的耗时操作,例如: 1.文件下载和上载(包括点对点应用程序传输文件,从网络下载文件.图像等)2.数据库事务(从数据库读到大量的数据到WinForm界面中的DataGridview里呈 ...
- 【KakaJSON手册】03_JSON转Model_03_key处理
有时候,服务器返回的JSON数据的key跟客户端模型的属性名可能不一致,比如客户端遵守驼峰规范叫做nickName,而服务器端返回的JSON可能叫做nick_name.这时候为了保证数据转换成功,就需 ...