poj 2528 Mayor's posters 【线段树 + 离散化】
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 50643 | Accepted: 14675 |
Description
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 n lines contains two integer numbers li 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 <= li <= ri <= 10000000. After
the i-th poster is placed, it entirely covers all wall segments numbered li, li+1 ,... , ri.
Output
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
题意:一个城市要竞选市长。竞选者能够在一块墙上贴海报为自己拉票,每一个人能够贴连续的一块区域。后来贴的能够覆盖前面的,问到最后一共能够看到多少张海报。
第一道离散化:(滚动数组优化) ORZ网上的大牛
#include <cstdio>
#include <cstring>
#include <algorithm>
#define MAXN 10000+100
using namespace std;
struct Node
{
int x, y;
};
Node num[10100];
int color[MAXN<<4];
int rec[MAXN<<4];//离散化 存储
int Find(int val, int *a, int L, int R)//在a数组下标[L, R]范围里面 查找val值的下标
{
int left = L, right = R;
while(left <= right)
{
int mid = (left + right) >> 1;
if(a[mid] == val)
return mid;
if(a[mid] < val)
left = mid + 1;
else
right = mid - 1;
}
return -1;
}
void PushDown(int o)
{
if(color[o])
{
color[o<<1] = color[o<<1|1] = color[o];
color[o] = 0;
}
}
void update(int o, int l, int r, int L, int R, int v)
{
if(L <= l && R >= r)
{
color[o] = v;
return ;
}
PushDown(o);
int mid = (l + r) >> 1;
if(L <= mid)
update(o<<1, l, mid, L, R, v);
if(R > mid)
update(o<<1|1, mid+1, r, L, R, v);
}
int vis[10100];//标记该海报是否出现过
int ans;//纪录数目
void query(int o, int l, int r)
{
if(color[o])
{
if(!vis[color[o]])
ans++,vis[color[o]] = true;
return ;
}
//return ;
if(l == r)
return ;
int mid = (l + r) >> 1;
query(o<<1, l, mid);
query(o<<1|1, mid+1, r);
}
int main()
{
int t, N;
scanf("%d", &t);
while(t--)
{
scanf("%d", &N);
memset(color, 0, sizeof(color));
int len = 1;
for(int i = 1; i <= N; i++)
{
scanf("%d%d", &num[i].x, &num[i].y);
rec[len++] = num[i].x;
rec[len++] = num[i].y;
}
sort(rec+1, rec+len);
//离散化
int RR = 2;
for(int i = 2; i < len; i++)//滚动数组优化
{
if(rec[i] != rec[i-1])
rec[RR++] = rec[i];
}
for(int i = RR-1; i > 1; i--)
{
if(rec[i] != rec[i-1] + 1)
rec[RR++] = rec[i-1] + 1;
}
sort(rec+1, rec+RR);//不是RR+1
for(int i = 1; i <= N; i++)
{
int l = Find(num[i].x, rec, 1, RR-1);
int r = Find(num[i].y, rec, 1, RR-1);
update(1, 1, RR-1, l, r, i);
}
memset(vis, false, sizeof(vis));
ans = 0;
query(1, 1, RR-1);
printf("%d\n", ans);
}
return 0;
}
poj 2528 Mayor's posters 【线段树 + 离散化】的更多相关文章
- POJ 2528 Mayor's posters 离散化+线段树
题目大意:给出一些海报和贴在墙上的区间.问这些海报依照顺序贴完之后,最后能后看到多少种海报. 思路:区间的范围太大,然而最多仅仅会有10000张海报,所以要离散化. 之后用线段树随便搞搞就能过. 关键 ...
- POJ 2528 Mayor's posters 离散化和线段树题解
本题就是要往墙上贴海报,问最后有多少可见的海报. 事实上本题的难点并非线段树,而是离散化. 由于数据非常大,直接按原始数据计算那么就会爆内存和时间的. 故此须要把数据离散化. 比方有海报1 6 7 ...
- poj 2528 Mayor's posters
这个题意是市长竞选,然后每一个人都能够贴广告牌.能够覆盖别人的看最后剩几个广告牌 这题目想了两个多小时,最后忍不住看了一下题解. 发现仅仅是简单地hash 和线段树成段更新 由于有10000个人竞选 ...
- 线段树区间更新,区间统计+离散化 POJ 2528 Mayor's posters
题意:有一个非常长的板子(10000000长),在上面贴n(n<=10000)张海报.问最后从外面能看到几张不同的海报. 由于板子有10000000长,直接建树肯定会爆,所以须要离散化处理,对于 ...
- POJ训练计划2528_Mayor's posters(线段树/成段更新+离散化)
解题报告 id=2528">地址传送门 题意: 一些海报,覆盖上去后还能看到几张. 思路: 第一道离散化的题. 离散化的意思就是区间压缩然后映射. 给你这么几个区间[1,300000] ...
- 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 [ ...
- Mayor's posters (线段树+离散化)
Mayor's posters Description The citizens of Bytetown, AB, could not stand that the candidates in the ...
- [poj2528] Mayor's posters (线段树+离散化)
线段树 + 离散化 Description The citizens of Bytetown, AB, could not stand that the candidates in the mayor ...
随机推荐
- Python迭代与递归方法实现斐波拉契数列
首先是迭代的方法实现: def f(n): f1, f2, fn = 1, 1, 1 while n>2: fn = f1 + f2 f1 = f2 f2 = fn n = n - 1 retu ...
- [Luogu1273] 有线电视网
[Luogu1273] 有线电视网 题目描述 某收费有线电视网计划转播一场重要的足球比赛.他们的转播网和用户终端构成一棵树状结构,这棵树的根结点位于足球比赛的现场,树叶为各个用户终端,其他中转站为该树 ...
- Git Learning Part II - Working locally
file status life circle basic: modified: Examples: untracked: unmodified: modified: Git branching ...
- (转载)Android 方法数超过64k、编译OOM、编译过慢解决方案。
Android 方法数超过64k.编译OOM.编译过慢解决方案. 目前将项目中的leancloud的即时通讯改为环信的即时通讯.当引入easeui的时候 出现方法数超过上限的问题. 搜索一下问题, ...
- [原创]C++带空格字符串的输入问题
字符串一直是一个重点加难点,很多笔试面试都会涉及,带空格的字符串更是十分常见,现在对字符串的输入问题进行一下总结. C++用cin输入的时候会忽略空格以后的字符,比如 char a[100]; cin ...
- 关于Eclipse安装Scala插件不显示
关于Eclipse安装Scala插件不显示, 改变java版本仍然不能使用, 办法还是有的:下载Eclipse Scala版本 解压使用 下载在这里:http://scala-ide.org/down ...
- CorelDRAW 2019线上发布会报名已开始
近日,由苏州思杰马克丁软件公司独家代理的CorelDRAW 2019将在苏州开启一场设计上的饕餮盛宴,您报名了么? 不管您是专业的设计师还是热爱设计的狂热粉丝,都将有机会参与到我们的活动中,为了这场盛 ...
- CorelDRAW X8官方正版特惠下载
CorelDRAW X8自发布以来,价格居高不下,这也使一众忠粉望而却步,之前看过CorelDRAW做活动,都是X6\X7这些比较早的版本,比较新的版本也没做什么优惠,不过还好看了一下,CorelDR ...
- JavaScript回顾一下js的基础知识,以及学习一下在项目中了解到的新知识
学习文档:https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Guide/Functions https://www.cnblogs.com ...
- Linux下挂载分区 (本人实例)
设置分区开机自动挂载 要在/etc/fstab里设置一行 把上面空格去掉就行了