POJ 2528 Mayor's posters (线段树区间更新+离散化)
题目链接:http://poj.org/problem?id=2528
给你n块木板,每块木板有起始和终点,按顺序放置,问最终能看到几块木板。
很明显的线段树区间更新问题,每次放置木板就更新区间里的值。由于l和r范围比较大,内存就不够了,所以就用离散化的技巧 比如将1 4化为1 2,范围缩小,但是不影响答案。
写了这题之后对区间更新的理解有点加深了,重点在覆盖的理解(更新左右两个孩子节点,然后值清空),还是要多做做题目。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
using namespace std;
const int MAXN = 2e4 + ;
struct data {
int l , r , sum;
}T[MAXN << ];
map <int , int> mp;
int x[MAXN] , y[MAXN] , cont , id[MAXN]; void init(int p , int l , int r) {
int mid = (l + r) >> ;
T[p].l = l , T[p].r = r;
if(l == r) {
T[p].sum = ;
return ;
}
init(p << , l , mid);
init((p << )| , mid + , r);
T[p].sum = T[p << ].sum + T[(p << )|].sum;
} int query(int index , int p) {
int mid = (T[p].l + T[p].r) >> ;
if(index == T[p].l && T[p].r == index) {
return T[p].sum;
}
if(T[p].sum) {
T[p << ].sum = T[(p << )|].sum = T[p].sum;
T[p].sum = ;
}
if(index <= mid) {
query(index , p << );
}
else {
query(index , (p << )|);
}
} void updata(int p , int l , int r , int val) {
int mid = (T[p].l + T[p].r) >> ;
if(T[p].l >= l && T[p].r <= r) { //找到了区间,更新这个区间
T[p].sum = val;
return ;
}
if(T[p].sum) { //重点注意,如果这个区间被访问了,并且这个区间要更新,就要将这个区间的值更新到其左右孩子的节点,并且要将这个区间的值清空,这样才能算是覆盖
T[p << ].sum = T[(p << )|].sum = T[p].sum;
T[p].sum = ;
}
if(r <= mid) {
updata(p << , l , r , val);
}
else if(l > mid) {
updata((p << )| , l , r , val);
}
else {
updata(p << , l , mid , val);
updata((p << )| , mid + , r , val);
}
} int main()
{
int t , n;
scanf("%d" , &t);
while(t--) {
scanf("%d" , &n);
mp.clear();
cont = ;
for(int i = ; i <= n ; i++) {
scanf("%d %d" , x + i , y + i);
if(!mp[x[i]]) {
id[++cont] = x[i];
mp[x[i]]++;
}
if(!mp[y[i]]) {
id[++cont] = y[i];
mp[y[i]]++;
}
}
sort(id + , id + cont + );
int len = -; //离散化之后的最大的数
for(int i = ; i <= n ; i++) { //离散化
x[i] = lower_bound(id + , id + cont + , x[i]) - id;
len = max(x[i] , len);
y[i] = lower_bound(id + , id + cont + , y[i]) - id;
len = max(y[i] , len);
}
init( , , len);
for(int i = ; i <= n ; i++) {
updata( , x[i] , y[i] , i);
}
mp.clear();
int res = ;
for(int i = ; i <= len ; i++) {
int temp = query(i , );
if(!mp[temp] && temp) {
res++;
mp[temp]++;
}
}
printf("%d\n" , res);
}
}
POJ 2528 Mayor's posters (线段树区间更新+离散化)的更多相关文章
- 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张海报, 按次序贴在墙上, 后贴的海报可以覆盖先贴的海报, 问一共有多少种海报出现过. 题解: 因为长度最大可以达到1e7, 但是最多只有2e4的区间个数,并且最后只是统计能看见的不同 ...
- 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 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 45703 Accepted: 13239 ...
- 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 思路: 线段树+离散化技巧(这里的离散化需要注意一下啊,题目数据弱看不出来) ...
- 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 ...
随机推荐
- org.hibernate.HibernateException: Could not parse configuration: /hibernate.cfg.xml
org.hibernate.HibernateException: Could not parse configuration: /hibernate.cfg.xml at org.hibernate ...
- Akka的Actor模型及使用实例
本文的绝大部分内容转载自rerun.me这一blog,老外写的东西就是好啊. ACTORS介绍 Anyone who has done multithreading in the past won't ...
- 出现错误ActivityManager: Warning: Activity not started, its current task has been
1.在学习两个Activity的切换时,重新把新的工程部署上模拟器时候出现错误:ActivityManager: Warning: Activity not started, its current ...
- Struts2配置
1. 设定server a) window– preferences – myeclipse – servers – tomcat – 6.x b) 选择tomcat h ...
- php linux部署相关
http://www.itbulu.com/wdcp-php55.html http://www.wdlinux.cn/wdcp/install.html http://www.yiichina.co ...
- 对于GLM的理解,与方差分析的对比
最近遇到一个问题,如果因变量为一个连续变量(如胰岛素水平),主要考察的变量为分组变量(如正常血糖组,前糖尿病组,糖尿病组三组),现在的目的是想看调整多种变量(包括多个连续性变量和分类变量)后,胰岛素水 ...
- temp - Linux administration handbook 答案
我开始做第一章后的练习题,发觉不是很容易随意地回答,就像是C++ primer之后的练习题的感觉. 自己有这么多不会的,让我感觉很不爽啊- -! 先不要要求自己一下子都明了,一口吃不成胖子,先找一份工 ...
- dpkg-query
1.功能作用 查看软件包信息 2.位置 /usr/bin 3.格式用法 dpkg-query [<选项> ...] <命令> 4.主要参数 Commands: -s|--sta ...
- 使用mp4v2将H264+AAC合成mp4文件
录制程序要添加新功能:录制CMMB电视节目,我们的板卡发送出来的是RTP流(H264视频和AAC音频),录制程序要做的工作是: (1)接收并解析RTP包,分离出H264和AAC数据流: (2)将H26 ...
- 《C++ Primer 4th》读书笔记 第5章-表达式
原创文章,转载请注明出处: http://www.cnblogs.com/DayByDay/p/3912114.html