posters

时间限制:1000 ms  |  内存限制:65535 KB
难度:6
 
描述
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. 

 
输入
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 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.
输出
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. 
http://acm.pku.edu.cn/JudgeOnline/images/2528_1.jpg

样例输入
1
5
1 4
2 6
8 10
3 4
7 10
样例输出
4
来源
POJ
上传者
iphxer

  离散化+线段树

  难度较高的一道线段树的题,用了离散化处理。在南阳理工学院的oj(http://acm.nyist.net/JudgeOnline/problem.php?pid=9)上提交成功,但是在poj上提交WA,不知道为什么……听说poj上测试数据有些问题,还要算上端点两侧的点??不清楚,有知道的同仁告诉我一下,拜谢。。。

  题意

  按顺序给你一些海报,这些海报可能相互重叠,求最后没有被完全盖住的海报的数量。

  给你海报的两个端点的位置,最多有10000张海报,但是位置最多可以是10000000。

  思路

  由于涉及到区间,首先应该想到线段树,但是海报的宽度太大,如果给你一个1-10000000大小的海报怎么办?肯定MLE超内存啊(这的new多少节点)。所以就要用到离散化的方法,听起来很神秘,但理解起来很简单,例如给你两张海报,[1,6]和[3,10],排序之后是1,3,6,10,离散化为1,2,3,4,即第一个区间为[1,3],第二个区间为[2,4],如此,创建一个[1,4]的线段树即可,而不用创建[1,10]的线段树,节省了空间。

  代码

 #include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std; #define MAXN 20000
bool tree[MAXN*+]; //存储区间有无海报
short hash[]; //离散化后的端点位置
short x[MAXN+]; //存储海报的两个端点
struct post{
short L;
short R;
}a[MAXN+]; //原数组
int cnt; //记录露在外面的海报数量 bool Insert(int d,int L,int R,int l,int r)
{
if(tree[d])
return false;
if(L==l && R==r){ //找到区间
tree[d] = true;
return true;
} int mid = (L+R)/;
bool res;
if(mid>=r){
res = Insert(d<<,L,mid,l,r);
}
else if(mid<l){
res = Insert(d<<|,mid+,R,l,r);
}
else {
bool f1 = Insert(d<<,L,mid,l,mid);
bool f2 = Insert(d<<|,mid+,R,mid+,r);
res = f1||f2;
}
if(tree[d<<] && tree[d<<|])
tree[d] = true;
return res;
} int main()
{
int T;
scanf("%d",&T);
while(T--){
memset(tree,,sizeof(tree));
memset(hash,,sizeof(hash));
memset(x,,sizeof(x));
int i,n,nCount=; //输入
scanf("%d",&n);
for(i=;i<n;i++){
scanf("%d%d",&a[i].L,&a[i].R);
x[nCount++] = a[i].L;
x[nCount++] = a[i].R;
} sort(x,x+nCount);
nCount = unique(x,x+nCount) - x; //去重 int key=;
for(i=;i<nCount;i++){ //离散化处理
hash[x[i]] = key;
if(i<nCount-){
if(x[i+]-x[i]==)
key++;
else
key=key+;
}
} cnt = ;
for(i=n-;i>=;i--){
if(Insert(,,key,hash[a[i].L],hash[a[i].R])) //将离散化后的海报插入到线段树中
cnt++;
}
printf("%d\n",cnt);
}
return ;
}

Freecode : www.cnblogs.com/yym2013

南阳理工 题目9:posters(离散化+线段树)的更多相关文章

  1. 【POJ】2528 Mayor's posters ——离散化+线段树

    Mayor's posters Time Limit: 1000MS    Memory Limit: 65536K   Description The citizens of Bytetown, A ...

  2. POJ 2528 Mayor&#39;s posters 离散化+线段树

    题目大意:给出一些海报和贴在墙上的区间.问这些海报依照顺序贴完之后,最后能后看到多少种海报. 思路:区间的范围太大,然而最多仅仅会有10000张海报,所以要离散化. 之后用线段树随便搞搞就能过. 关键 ...

  3. Mayor's posters(离散化线段树)

    Mayor's posters Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 54067   Accepted: 15713 ...

  4. Mayor's posters (离散化线段树+对lazy的理解)

    题目 题意: n(n<=10000) 个人依次贴海报,给出每张海报所贴的范围 li,ri(1<=li<=ri<=10000000) .求出最后还能看见多少张海报. 思路: 由于 ...

  5. SGU 180 Inversions(离散化 + 线段树求逆序对)

    题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=180 解题报告:一个裸的求逆序对的题,离散化+线段树,也可以用离散化+树状数组.因为 ...

  6. hpu校赛--雪人的高度(离散化线段树)

    1721: 感恩节KK专场——雪人的高度 时间限制: 1 Sec  内存限制: 128 MB 提交: 81  解决: 35 [提交][状态][讨论版] 题目描述 大雪过后,KK决定在春秋大道的某些区间 ...

  7. 【bzoj4636】蒟蒻的数列 离散化+线段树

    原文地址:http://www.cnblogs.com/GXZlegend/p/6801379.html 题目描述 蒟蒻DCrusher不仅喜欢玩扑克,还喜欢研究数列 题目描述 DCrusher有一个 ...

  8. 离散化+线段树/二分查找/尺取法 HDOJ 4325 Flowers

    题目传送门 题意:给出一些花开花落的时间,问某个时间花开的有几朵 分析:这题有好几种做法,正解应该是离散化坐标后用线段树成端更新和单点询问.还有排序后二分查找询问点之前总花开数和总花凋谢数,作差是当前 ...

  9. 干物妹小埋 (离散化 + 线段树 + DP)

    链接:https://ac.nowcoder.com/acm/contest/992/B来源:牛客网 题目描述 在之前很火的一个动漫<干物妹小埋>中,大家对小埋打游戏喝可乐的印象十分的深刻 ...

随机推荐

  1. Codeforces 710 D. Two Arithmetic Progressions

    Description \(x=a_1k+b_1=a_2l+b_2,L\leqslant x \leqslant R\) 求满足这样条件的 \(x\) 的个数. Sol 扩展欧几里得+中国剩余定理. ...

  2. 如何给ZenCart网站livezilla客服系统?

    大致步骤: 1 去官网下载livezilla

  3. Citrix运行检测出错

    Citrix运行检测出错: 出现意外错误.请确认服务器名称正确.服务器已打开.Citrix XenApp 已安装在服务器上并且 Citrix MFCOM 服务正在运行. 看Windows日志出现如下错 ...

  4. JQuery textarea中val(),text()

    val()是当前输入框的前台显示内容 text()是原始内容, 调试时浏览器审查元素可以发现如果只改变val(),text()值是不会改变的

  5. NDK学习4: Eclipse HelloWorld

    NDK学习4: Eclipse HelloWorld 1.配置Eclipse NDK环境  Window->preferences->android->ndk   2.新建Andro ...

  6. 【Linux】find grep 联合使用 过滤所有子目录、文件

    find . -type f -name '*.*' | xargs grep --color -n 'Admin@123'find . -type f -name '*.*' | xargs sed ...

  7. Lua简易入门教程

    环境:lua for windows (lfW)主页:http://luaforwindows.luaforge.net/https://code.google.com/p/luaforwindows ...

  8. 转:C++编程隐蔽错误:error C2533: 构造函数不能有返回类型

    C++编程隐蔽错误:error C2533: 构造函数不能有返回类型 今天在编写类的时候,出现的错误. 提示一个类的构造函数不能够有返回类型.在cpp文件里,该构造函数定义处并没有返回类型.在头文件里 ...

  9. iOS App Extensions 推荐文章

    写的非常不错,读完后,基本的extension的套路就清楚了,也是我们的园友写的,感谢他: http://www.cnblogs.com/xdream86/p/3855932.html 下面这个教程是 ...

  10. ACM/ICPC 之 树形DP(POJ1192)

    将某点看做根状态,邻接点看做子状态,由子状态向根状态转移. POJ1192-最优连通子集 题解:将每一个点分成两个状态进行保存,因此可以构造一个数组dp[i][2]. dp[i][0]:不包括该点权值 ...