博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【POJ 2528 --- Mayor's posters】线段树+延迟更新+离散化
阅读量:2038 次
发布时间:2019-04-28

本文共 3831 字,大约阅读时间需要 12 分钟。

【POJ 2528 --- Mayor's posters】线段树+延迟更新+离散化

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

解题思路:

由于数据量过大,所以我们首先要离散化,降低数据的量级。

通过线段树标记[l,r]范围是被第几张海报覆盖,最后直接查找线段树中有几种海报没有被完全覆盖就行了。

需要注意的是由于每次输入的海报范围是l和r两个数,所以最坏的情况离散化后是有2n个点,所以要注意线段树就不能直接用4n了,需要扩大。

AC代码:

#include 
#include
#include
#include
using namespace std;#define SIS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)#define endl '\n'#define lson root<<1#define rson root<<1|1const int MAXN = 1e4+5;int sum[MAXN<<3],sl[MAXN],sr[MAXN],ans=0;vector
vec;bool vis[MAXN];int getId(int x){
return lower_bound(vec.begin(),vec.end(),x)-vec.begin()+1;}void push_down(int root){
if(sum[root]) {
sum[lson]=sum[root]; sum[rson]=sum[root]; sum[root]=0; }}void update(int root,int l,int r,int L,int R,int num){
if(L<=l && R>=r) {
sum[root]=num; return; } if(l==r) return; push_down(root); int mid=(l+r)>>1; if(L<=mid) update(lson,l,mid,L,R,num); if(R>mid) update(rson,mid+1,r,L,R,num);}void query(int root,int l,int r,int L,int R){
if(sum[root]) {
if(!vis[sum[root]]) ans++; vis[sum[root]]=true; return; } if(l==r) return; int mid=(l+r)>>1; if(L<=mid) query(lson,l,mid,L,R); if(R>mid) query(rson,mid+1,r,L,R);}int main(){
SIS; int T,n; cin >> T; while(T--) {
ans=0; memset(sum,0,sizeof(sum)); memset(vis,false,sizeof(vis)); vec.clear(); cin >> n; for(int i=0;i
> sl[i] >> sr[i]; vec.push_back(sl[i]); vec.push_back(sr[i]); } sort(vec.begin(),vec.end()); vec.erase(unique(vec.begin(),vec.end()),vec.end()); for(int i=0;i

转载地址:http://aiyof.baihongyu.com/

你可能感兴趣的文章
ObjectMapper 的一些坑
查看>>
spring 几种获得bean的方法
查看>>
Server returns invalid timezone. Go to ‘Advanced‘ tab and set ‘serverTimezon‘
查看>>
SQL查询语句执行顺序详解
查看>>
如何避免创建不必要的对象
查看>>
老司机入职一周,给我们解读 Spring Boot 最流行的 16 条实践
查看>>
maven删除不必要的依赖;优化pom依赖研究
查看>>
不同类型接口的异常处理规范
查看>>
2017 年你不能错过的 Java 类库
查看>>
Java 异常处理的误区和经验总结
查看>>
浅析bootstrap原理及优缺点
查看>>
MVVM模式中ViewModel和View、Model有什么区别
查看>>
面向切面编程的两种实现
查看>>
Java笔记——面向切面编程(AOP模式)
查看>>
算法----五大算法之分支限界法
查看>>
Docker 在分布式和大数据框架中的应用
查看>>
javaweb学习总结——获得MySQL数据库自动生成的主键
查看>>
【zabbix教程三】——centos7 安装zabbix客户端并监控
查看>>
SpringMVC
查看>>
多线程
查看>>