POJ 2528(线段树+离散化+特殊离散化)网上博客很少有人真正写对!!! 是POJ数据太水...
Description
The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign have been placing their electoral posters at all places at their whim. The city council has finally decided to build an electoral wall for placing the posters and introduce the following rules:
- Every candidate can place exactly one poster on the wall.
- All posters are of the same height equal to the height of the wall; the width of a poster can be any integer number of bytes (byte is the unit of length in Bytetown).
- The wall is divided into segments and the width of each segment is one byte.
- Each poster must completely cover a contiguous number of wall segments.
They have built a wall 10000000 bytes long (such that there is enough place for
all candidates). When the electoral campaign was restarted, the candidates were
placing their posters on the wall and their posters differed widely in width.
Moreover, the candidates started placing their posters on wall segments already
occupied by other posters. Everyone in Bytetown was curious whose posters will
be visible (entirely or in part) on the last day before elections.
Your task is to find the number of visible posters when all the posters are
placed given the information about posters' size, their place and order of
placement on the electoral wall.
Input
The first line of input contains a number c giving the
number of cases that follow. The first line of data for a single case contains
number 1 <= n <= 10000. The subsequent n lines describe the posters in the
order in which they were placed. The i-th line among the n lines contains two
integer numbers l i and ri which are the number of the wall segment
occupied by the left end and the right end of the i-th poster, respectively. We
know that for each 1 <= i <= n, 1 <= l i <= ri <=
10000000. After the i-th poster is placed, it entirely covers all wall segments
numbered l i, l i+1 ,... , ri.
Output
For each input data set print the number of visible
posters after all the posters are placed.
The picture below illustrates the case of the sample input.
Sample Input
1
5
1 4
2 6
8 10
3 4
7 10
Sample Output
4
题意就是一块公告牌,长度为n。大家向上面依次贴广告,广告的范围是[L,R],问你最后你在公告牌上能看见几个广告。
这个题分为3个阶段解!
显然是线段树啊!对!这是第一阶段!但如果硬写这个数据量不是TLE就是MLE。所以必须加上离散化处理。离散化处理就是这个题的第二阶段!
什么是离散化呢?就是建树时没有必要每个区间长度为1的区间都要占一个节点,这样无论在空间还是在以后的操作上面都是吃亏的。我们只需要存所有出现的区间的端点就可以了。
比如这个例子
3
1 10
1 4
6 10
我们把出现的端点排序然后去重以后得到的1,4,6,10。
1 4
6 10
↓ ↓ ↓ ↓
1 2
3 4
经过这样的一映射,我们就只处理一个长度为4的线段树就行了
结果显然是不对的,这个测试用例的结果是3,而如果向上面(普通离散化)这样分析的话结果就是2了,不信看下图。
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAVIAAAD6CAYAAADz7c/YAAAeQklEQVR4nO3d32tUd/7H8ZMvve26sV4VEan2QuziorFdRAsKMa7CsgtKajYsgkWNW4TaVk1qKUutNqV6sbQ1SoXebLKh9q5u1YJCs8hao0QweGEtQcSrbF27f8B88/osn/Rkcs7MOfM5v+bM8wHjxDkz53xmzpn3fH5/2iozPABAw/4v7wQAQLMjkAKAIwIpADgikAKAIwIpADgikAKAIwIpADgikAKAIwIpADgikAKAIwIpADgikAKAIwIpADgikCKyf/7zn15bW9u8W5Dbt297a9euNdv1urz98MMP3sKFC+el98MPP5z3frZs2ZJTKtGsnso7AWg+fX19Xk9PT+C2f//7396pU6e848ePZ5yq2vbv3+89fvx43uPbt2/31q1bN/v/119/PctkoSQIpIht6dKl3vr16+c9rpznrl27vB9//NEbHBz0njx5UoiAeubMGe/SpUvesmXLvPv378/Z9txzz5mb9cwzz2SdPJQARXskZnJy0tuzZ493794979ChQ96CBQvyTpLJIff395vAvnz58ryTg5IiR4rE7N27N+8kzPPnP//Z5EQV2K9cuZJ3clBSBFKU1j/+8Q9vdHTUm5iYyDspKDmK9iglFel7e3tNkX7VqlV5JwclRyBFKalIr+5OKtIDaaNoj9KxRfqxsbG8k4IWQY4UpaIi/YEDB7yBgYHALlpAGgikKBUNBpCDBw/mnBK0Eor2KA0NCNAAABXp6ViPLJEjRWkcO3bM3G/YsCFwTgCNbhL7f42zB5JAjhSloeGpmzZtCt1+9uxZM0RUXaLEP8YecEEgRWm88sorNbdrZJMCKV2ikDSK9gDgiBwpElNd52jHtg8PD3vXrl2bfTxqjlCNR6rvVFGcXCSKjECKxBw+fDjw8dOnT8/5P0ERZUMgRWIqlUqi+1OHetswlISLFy8mti/AjzpSFNqtW7e8JUuW5J0MoC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DBw/Mvb7g9SYm0Q9Mve48diRM2GdjKXA3ayDVZ6AZv/TDo76eCnaiDvkaSqthkHHqT9Wt6be//a338ccfm2tHDTSiAKprUTlbcqPNpdSBNMpMO0HUEBA0g3i9WYCaQaOfiQJFUGNCGT6TKBTY4n52YdeRRKmfzFut9xt2PdRS5muFOtIA6soTdfajVqKiZ5lncUpa2a8jroefEUgDaAKHJCa1LRO1MmsoZGdnZ95JaRplvo64HuYqTdHe37lX/dNcxncnNcmGP02aXT9r6kbjT4NLUVKfpyaicKUv4KNHj2b/r5brItUHlvk64npIUZ7rnCTBrrXjv0VdmyZt1enyMl6zqfpWBHZNIv+tCGv0lPk64npIX5v+STdUA0C5UUcKAI4IpADgiEAKAI4IpADgiEAKAI4IpADgiEAKAI4IpADgiEAKAI4IpADgiEAKAI4IpADgiEAKAI4IpADgiEAKAI4IpADgiEAKAI4IpADgiEAKAI4IpADgiEAKAI4IpADgiEAKAI4IpADgiEAKAI7+H6+pcaPrBkKjAAAAAElFTkSuQmCC" alt="" />
是不是少了[4,5]这个区间?现在就要来到这个题的第三点!!!我们在普通离散化后,如果两个相邻的区间端点之差大于1,我们就再这两个端点间加一个端点。
像上面的用例就应该变成
1 4 6 10 我们都加上了一个右端点-1的三个点,在建树的时候考虑7个点
↓ ↓ ↓ ↓ ↓ ↓ ↓
1 2 3 4 5 6 7
这样建树就是对的!!!!!
代码如下:
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
#define M 10010
bool hash[M];
int col[M<<];//用来保存颜色(海报)
struct segTree
{
int l,r;
}tree[M];
int ans,x[M<<],n,t;
void PushDown (int now)//如果当前节点有海报,向下更新
{
if (col[now]!=-)
{
col[now<<]=col[now<<|]=col[now];
col[now]=-;
}
}
void Update (int L,int R,int c,int l,int r,int now)
{
if (L<=l&&r<=R)//如果正好找到区间,修改区间
{
col[now]=c;
return ;
}
PushDown(now);//延迟后的更新(这时候该用子节点了,要向下更新)
int m=(l+r)>>;
if (L<=m)
Update(L,R,c,l,m,now<<);
if (R>m)
Update(L,R,c,m+,r,now<<|);
}
void query (int l,int r,int now)
{
if (col[now]!=-)
{
if (!hash[col[now]])
ans++;
hash[col[now]]=true;
return ;
}
if (l==r)//更新到底一定要return,要不会炸
return ;
int m=(l+r)>>;
query(l,m,now<<);
query(m+,r,now<<|);
}
int main()
{
//freopen("de.txt","r",stdin);
scanf("%d",&t);
while (t--)
{
int cnt=;
scanf("%d",&n);
for (int i=;i<n;++i)
{
scanf("%d%d",&tree[i].l,&tree[i].r);
x[cnt++]=tree[i].l,x[cnt++]=tree[i].r;
}
sort(x,x+cnt);
int m=;
for (int i=;i<cnt;++i)
if (x[i]!=x[i-]) x[m++]=x[i];//记录不重复的节点
for (int i=m-;i>=;--i)
if (x[i]!=x[i-]+) x[m++]=x[i-]+;//把相差大于1的两个节点之间再加上一个节点(右节点-1)
sort(x,x+m);
memset(col,-,sizeof col);
for (int i=;i<n;++i)
{
int l=lower_bound(x,x+m,tree[i].l)-x;
int r=lower_bound(x,x+m,tree[i].r)-x;
Update(l,r,i,,m,);
}
memset(hash,false,sizeof hash);
ans=;
query(,m,);
printf("%d\n",ans);
}
return ;
}
这个题去重的时候办法可能老土,但是别用set!!!不信看时间!不用set 79ms 用了set 375ms
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAcIAAABICAIAAADqJmIYAAASXklEQVR4Ae1dTUwb1xa+pvBUWgJKVcWESin0xUAJBZZpI0WNHg8i0R9KWKVKqizIwpGyiTAbpKoSG7C6QcKLsIgalK4SF9oihYgqT5H6miXwgAJOG8pTKURVIwit+0SL37l/M3fGHofaY3vGPiOE7++5d77je+aec67neGKxGMELEUAEEAFEIFUEilLtiP0QAUQAEUAEKAIoRvF7gAggAohAWgigGE0LPuyMCCACiEDxxaE5DYWrgSY1q5VjwvkIIO+cz6MkM0T2JQHH+VW4G3U+j3CGiAAi4GgEUIw6mj04OUQAEXA+AihGnc8jnCEigAg4GoFi582usj9w6Aif1uaji59siBn6qoPvlVew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fEXs/6+rqiXIWghG3Dm8H1XDcIlEAOwxvZy9fUqKGIe04bOCmpxfxgFLvGvtoXEg7qxB1VuXJvzJaADWCIe2SI5VCbST04ZIhIB3QsAhvx6hHQu+zUNjaUBp3MLydhknOEhjSjkPvWfpvFMRocfHf/twjvsOeyE85Y8n+Bk4Q0o51BA+GKUQdp2dVLkajkdTCZyH+HYa02x/+qbRSY8/JyHQKHcvwdrSN3j6vw9u5+0V5GNJu5cf/wZe1qKh4L+ZxgxhVlh8mFQTcvQ6VGynMJLLP1XyHc6PgXAI/0x+uvg2cPCKACCACuUKgCC5Q6rm/PleTwHERAUQAEXAvAkUEflEPf64/N+peFuDMEQFEwN0IYEg7d/MPZ48IIAI5RwBC2kVhK1rkKdmLlaCLKef8SHkC6KNIGTondET2OYELKc+haO/PWAykqMc1b8pL+VaxIyKACCACmUCguMjzLLiYYnvu/jFoJqBBmogAIoAI7AeBoljMQ2IgRvfTGNsgAogAIoAImBEQL8ozF2MeEUAEEAFEYH8I/B8MOgE0rf0HigAAAABJRU5ErkJggg==" alt="" />
POJ 2528(线段树+离散化+特殊离散化)网上博客很少有人真正写对!!! 是POJ数据太水...的更多相关文章
- poj 2528 线段树区间修改+离散化
Mayor's posters POJ 2528 传送门 线段树区间修改加离散化 #include <cstdio> #include <iostream> #include ...
- poj 2528(线段树+离散化) 市长的海报
http://poj.org/problem?id=2528 题目大意是市长竞选要贴海报,给出墙的长度和依次张贴的海报的长度区间(参考题目给的图),问最后你能看见的海报有几张 就是有的先贴的海报可能会 ...
- poj 2528 线段树+离散化
题意:在墙上贴一堆海报(只看横坐标,可以抽象成一线段),新海报可以覆盖旧海报.求最后能看到多少张海报 sol:线段树成段更新.铺第i张海报的时候更新sg[i].x~sg[i].y这一段为i. 然而坐标 ...
- POJ 2528 (线段树 离散化) Mayor's posters
离散化其实就是把所有端点放在一起,然后排序去个重就好了. 比如说去重以后的端点个数为m,那这m个点就构成m-1个小区间.然后给这m-1个小区间编号1~m-1,再用线段树来做就行了. 具体思路是,从最后 ...
- poj 2528 线段树 离散化的小技巧
题意:在墙上贴海报,海报可以互相覆盖,问最后可以看见几张海报思路:直接搞超时+超内存,需要离散化.离散化简单的来说就是只取我们需要的值来 用,比如说区间[1000,2000],[1990,2012] ...
- Mayor's posters POJ - 2528(线段树 + 离散化)
Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 74745 Accepted: 21574 ...
- Mayor's posters POJ - 2528 线段树(离散化处理大数?)
题意:输入t组数据,输入n代表有n块广告牌,按照顺序贴上去,输入左边和右边到达的地方,问贴完以后还有多少块广告牌可以看到(因为有的被完全覆盖了). 思路:很明显就是线段树更改区间,不过这个区间的跨度有 ...
- Mayor's posters POJ - 2528 线段树区间覆盖
//线段树区间覆盖 #include<cstdio> #include<cstring> #include<iostream> #include<algori ...
- POJ 2528 线段树
坑: 这道题的坐标轴跟普通的坐标轴是不一样的-- 此题的坐标轴 标号是在中间的-- 线段树建树的时候就不用[l,mid][mid,r]了(这样是错的) 直接[l,mid][mid+1,r]就OK了 D ...
随机推荐
- 透明的UITableView
// // ViewController.m // 透明table // // Created by LiuWei on 2018/4/23. // Copyright © 2018年 xxx. Al ...
- @RequestParam和@PathVariable的区别
一:@RequestParam @RequestParam是传递参数的. @RequestParam用于将请求参数区数据映射到功能处理方法的参数上. public Object Login(@Requ ...
- click和blur冲突的问题
昨天在前端群里讨论到一个问题,大家平时做表单验证的时候一般都有个input框和删除按钮,然后习惯性在失去焦点的时候去验证输入的内容是否正确,做验证,发请求等等.这个时候,那个点击删除按钮往往也就触发了 ...
- paper 163: opencv知识点回顾
From Here: https://zhuanlan.zhihu.com/p/24425116 Python下使用OpenCV 本篇将介绍和深度学习数据处理阶段最相关的基础使用,并完成4个有趣实用的 ...
- C#将Json字符串转化为对象
实体类: public class CheckData { public string msg; public string code; public string data; public stri ...
- jQuery积累:serialize()、stringify()、toJSON()
*)表单serialize()序列化,和serializeArray() ##)应用场景 当Ajax或者get请求发送表单中的某一个,或者某几个值到后台时,通过jQuery就能获取到这些值.然后作为A ...
- appium常见问题09_MAC打开uiautimatorviewer闪退怎么办?
问题: 下载安装Android SDK后,并且已在.bash_profile文件中配置环境变量.但是在tools中打开定位工具uiautomatorviewer出现闪退. 解决: 首先检查环境变量配置 ...
- <读书笔记>《React:引领未来的用户界面开发框架》
<React:引领未来的用户界面开发框架>(GitHub 附demo版) 1.Component的创建与复合 1.1 React简介 背景介绍,全书概览 1.本质上是一个状态机,它以精简的 ...
- linux to extract contents between patterns
参考:http://stackoverflow.com/questions/19177721/extract-lines-between-two-patterns-from-a-lfile awk ' ...
- spring, spring mvc, mybatis整合文件配置详解
转自:http://www.cnblogs.com/wxisme/p/4924561.html 使用SSM框架做了几个小项目了,感觉还不错是时候总结一下了.先总结一下SSM整合的文件配置.其实具体的用 ...