POJ训练计划2528_Mayor's posters(线段树/成段更新+离散化)
解题报告
题意:
一些海报,覆盖上去后还能看到几张。
思路:
第一道离散化的题。
离散化的意思就是区间压缩然后映射。
给你这么几个区间[1,300000],[3,5],[6,10],[4,9]
区间左右坐标排序完就是
1,3,4,5,6,9,10,300000;
1,2,3,4,5,6, 7 ,8;
我们能够把上面的区间映射成[1,8],[2,4],[5,7],[3,6];
这样就节省了非常多空间。
给线段染色, lz标记颜色。
#include <map>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
struct node
{
int x,y;
} p[10100];
int zb[20100],_hash[10100],lz[100000],ans;
void push_down(int root )
{
if(lz[root])
{
lz[root*2]=lz[root*2+1]=lz[root];
lz[root]=0;
}
}
void update(int root,int l,int r,int ql,int qr,int v)
{
if(ql>r||qr<l)return;
if(ql<=l&&r<=qr)
{
lz[root]=v;
return;
}
push_down(root);
int mid=(l+r)/2;
update(root*2,l,mid,ql,qr,v);
update(root*2+1,mid+1,r,ql,qr,v);
}
void _q(int root,int l,int r)
{
if(lz[root])
{
if(!_hash[lz[root]])
ans++;
_hash[lz[root]]=1;
return ;
}
if(l==r)return;
int mid=(l+r)/2;
_q(root*2,l,mid);
_q(root*2+1,mid+1,r);
}
int main()
{
int t,i,j,n;
scanf("%d",&t);
while(t--)
{
ans=0;
memset(_hash,0,sizeof(_hash));
scanf("%d",&n);
for(i=0; i<n; i++)
{
scanf("%d%d",&p[i].x,&p[i].y);
zb[i]=p[i].x;
zb[i+n]=p[i].y;
}
sort(zb,zb+n*2);
int m=unique(zb,zb+n*2)-zb;
for(i=0; i<n; i++)
{
int ql=lower_bound(zb,zb+m,p[i].x)-zb+1;
int qr=lower_bound(zb,zb+m,p[i].y)-zb+1;
update(1,1,m,ql,qr,i+1);
}
_q(1,1,m);
printf("%d\n",ans);
}
}
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 41877 | Accepted: 12199 |
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
POJ训练计划2528_Mayor's posters(线段树/成段更新+离散化)的更多相关文章
- POJ 2777 Count Color (线段树成段更新+二进制思维)
题目链接:http://poj.org/problem?id=2777 题意是有L个单位长的画板,T种颜色,O个操作.画板初始化为颜色1.操作C讲l到r单位之间的颜色变为c,操作P查询l到r单位之间的 ...
- poj 3468 A Simple Problem with Integers 【线段树-成段更新】
题目:id=3468" target="_blank">poj 3468 A Simple Problem with Integers 题意:给出n个数.两种操作 ...
- 线段树(成段更新) POJ 3468 A Simple Problem with Integers
题目传送门 /* 线段树-成段更新:裸题,成段增减,区间求和 注意:开long long:) */ #include <cstdio> #include <iostream> ...
- poj 3648 线段树成段更新
线段树成段更新需要用到延迟标记(或者说懒惰标记),简单来说就是每次更新的时候不要更新到底,用延迟标记使得更新延迟到下次需要更新or询问到的时候.延迟标记的意思是:这个区间的左右儿子都需要被更新,但是当 ...
- ACM: Copying Data 线段树-成段更新-解题报告
Copying Data Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Description W ...
- Codeforces Round #149 (Div. 2) E. XOR on Segment (线段树成段更新+二进制)
题目链接:http://codeforces.com/problemset/problem/242/E 给你n个数,m个操作,操作1是查询l到r之间的和,操作2是将l到r之间的每个数xor与x. 这题 ...
- hdu 4747【线段树-成段更新】.cpp
题意: 给出一个有n个数的数列,并定义mex(l, r)表示数列中第l个元素到第r个元素中第一个没有出现的最小非负整数. 求出这个数列中所有mex的值. 思路: 可以看出对于一个数列,mex(r, r ...
- HDU1698_Just a Hook(线段树/成段更新)
解题报告 题意: 原本区间1到n都是1,区间成段改变成一个值,求最后区间1到n的和. 思路: 线段树成段更新,区间去和. #include <iostream> #include < ...
- HDU 3577 Fast Arrangement ( 线段树 成段更新 区间最值 区间最大覆盖次数 )
线段树成段更新+区间最值. 注意某人的乘车区间是[a, b-1],因为他在b站就下车了. #include <cstdio> #include <cstring> #inclu ...
随机推荐
- Python装饰器进阶
装饰器进阶 现在,我们已经明白了装饰器的原理.接下来,我们还有很多事情需要搞清楚.比如:装饰带参数的函数.多个装饰器同时装饰一个函数.带参数的装饰器和类装饰器. 装饰带参数函数 def foo(fun ...
- maven打包时包含本地jar
项目中需要使用maven的打包工具,生成zip压缩包,使用的插件是assembly-plugin.因为一些特殊的原因,需要使用一些本地的jar进行依赖,加载外部jar后编码过程中没有任何问题,但是打包 ...
- 【python学习-5】面向对象的python
python是一种面向对象的编程语言,虽然与C++一样,支持面向过程的程序设计,python完全可以使用函数.模块等方式来完成工作,但是当使用python编写一个较大的项目时,则应该考虑使用面向对象的 ...
- BZOJ.1014.[JSOI2008]火星人(Splay 二分 Hash)
题目链接 后缀数组显然不行啊.求LCP还可以哈希+二分,于是考虑用平衡树维护哈希值. \[某一节点的哈希值 = hs[lson]*base^{sz[rson]+1} + s[rt]*base^{sz[ ...
- C/C++ 和 PHP 技术经典图书,学习视频资料总结
技术经典图书 1.<计算机科学导论> 作者:(美)佛罗赞,(美)莫沙拉夫著,刘艺等译(强推) 涵盖了大部分计算机课程的内容,但都是简介,是最基础的知识,非常适合计算机初学者看,强烈建议把课 ...
- Java多线程runnable
主要为大家分享Java多线程怎么实现Runnable方式 一 :主要步骤 1.定义实现Runnable接口 2.覆盖Runnable接口中run方法,将线程要运行的代码存在run方法里 3.用Thre ...
- Bootstrap入门学习(三)——简单项目
此样例来自Bootstrap官网提供的入门级模版.仅仅有主要的东西:引入了预编译版的 CSS 和 JavaScript 文件,页面仅仅包括了一个 container 元素. 引入Bootstrap 创 ...
- Nginx担当WebSockets代理
Nginx担当WebSockets代理 英文原文:http://nginx.com/blog/websocket-nginx/ 作者:chszs,转载需注明. 博客主页:http://blog.csd ...
- Ubuntu 14 安装Java(JRE、JDK)、Maven
JRE vs OpenJDK vs Oracle JDK JRE(Java Runtime Environment),它是你运行一个基于Java语言应用程序的所正常需要的环境.如果你不是一个程序员的话 ...
- muduo 的 shutdown() 没有直接关闭 TCP 连接?
http://blog.csdn.net/Solstice/article/details/6208634 今天收到一位网友来信: 在 simple 中的 daytime 示例中,服务端主动关闭时调用 ...