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 ...
随机推荐
- 【iOS】Masonry 自动布局 MASViewConstraint.m:207 错误
问题详情: Assertion failure 报错原因: make.right.equalTo([_imageView superview]).right.with.offset(-); make. ...
- abp(net core)+easyui+efcore实现仓储管理系统目录
abp(net core)+easyui+efcore实现仓储管理系统目录 abp(net core)+easyui+efcore实现仓储管理系统——ABP总体介绍(一) abp(net core)+ ...
- DesignPattern系列__06迪米特原则
迪米特原则定义 迪米特原则,也叫最少知道原则,即一个类应该对自己依赖的类知道的越少越好,而你被依赖的类多么复杂,对我都没有关系.也就是说,对于别依赖的类来说,不管业务逻辑多么复杂,都应该尽量封装在类的 ...
- UVA11388 GCD LCM
(链接点这儿) 题目: The GCD of two positive integers is the largest integer that divides both the integers w ...
- mongoDB的CRUD的总结
今天开始接触非关系型数据库的mongoDB,现在将自己做的笔记发出来,供大家参考,也便于自己以后忘记了可以查看. 首先,mongoDB,是一种数据库,但是又区别与mysql,sqlserver.orc ...
- C++实现多组数据合并输出
思路 假设有多组数据,每一组都是按从小到大的顺序输入的,设计如下数据结构 前面一列是每一组数据的首部,后面是真正的数据,首部的定义为: struct head { Node* next; head* ...
- 学习Vuex 个人的一些拙见。
首先说下什么是vuex?这个是对vue的状态的管理,这样说可能有点大,其实就是vue 里面 data 的管理,或者说是多个vue 组件共有的data 的一种管理, 在任何一个组件里面,都可以修改,访 ...
- C# 复制Excel单元格格式
本文将介绍通过C# 复制Excel单元格格式的方法,包括复制单元格中的字体.字号.字体加粗.倾斜.单元格背景色.字体颜色.单元格数字格式.单元格文字方向.文字旋转.下划线.单元格对齐方式.单元格边框等 ...
- git码云的使用基础(为了以后更好的协同操作)
git手册 安装教程 windows 不需要什么操作点点点就好 设置一个文件夹当成本地仓库 第一次上传 git init# 创建一个本地的仓库 git add. 当前文件夹下所有内容# 添加到暂存区 ...
- 【原】iOS查找私有API
喜接新项目往往预示的会出一堆问题.解决问题的同时往往也就是学到更多东西的时候,这也许就是学习到新东西最直接最快速的方法吧! 小编经过努力,新项目终于过测试了,可是被苹果大大给拒了,好苦啊,最近的审核真 ...