Mayor's posters
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 55156   Accepted: 16000

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 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

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

Source

 
 

题意:给出一些海报,后贴的会把先贴的覆盖,问最多能看到多少张海报。

题解:

线段切割

学习了一个线段切割

其实很简单的。

具体看 IOI2004年薛矛的论文《剖析线段树与矩形切割》

概括起来就是前面的线段不会影响后面的线段,所以从后面往前面推。对于每个线段,我们只考虑后面的线段对它的影响,影响有五类(论文第26页中有),我们可以将其简化为三类 :

不相交:

1.两个线段不相交。

相交:

2.前一个线段的左端点小于后一个线段的左端点(包括了 前一个线段覆盖了后一个线段的前半部分 和 前一个线段覆盖了后一个线段)

3.前一个线段的右端点大于后一个线段的右端点(包括了 前一个线段覆盖了后一个线段的后半部分 和 前一个线段覆盖了后一个线段)

具体看程序:

 #include<bits/stdc++.h>
using namespace std;
int N,x[],y[],ans[];
int read()
{
int s=,fh=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')fh=-;ch=getchar();}
while(ch>=''&&ch<=''){s=s*+(ch-'');ch=getchar();}
return s*fh;
}
void Cover(int l,int r,int k,int k1)
{
while(k<=N&&(r<x[k]||l>y[k]))k++;
if(k>=N+){ans[k1]+=(r-l+);return;}
if(l<x[k])Cover(l,x[k]-,k+,k1);
if(r>y[k])Cover(y[k]+,r,k+,k1);
}
int main()
{
int T,i,tot;
T=read();
while(T--)
{
N=read();
for(i=;i<=N;i++){x[i]=read();y[i]=read();}
memset(ans,,sizeof(ans));
for(i=N;i>=;i--)Cover(x[i],y[i],i+,i);
tot=;
for(i=;i<=N;i++)if(ans[i]>)tot++;
printf("%d\n",tot);
}
fclose(stdin);
fclose(stdout);
return ;
}

Poj 2528-Mayor's posters 线段切割的更多相关文章

  1. POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化)

    POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化) 题意分析 贴海报,新的海报能覆盖在旧的海报上面,最后贴完了,求问能看见几张海报. 最多有10000张海报,海报 ...

  2. poj 2528 Mayor's posters 线段树+离散化技巧

    poj 2528 Mayor's posters 题目链接: http://poj.org/problem?id=2528 思路: 线段树+离散化技巧(这里的离散化需要注意一下啊,题目数据弱看不出来) ...

  3. poj 2528 Mayor's posters 线段树区间更新

    Mayor's posters Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://poj.org/problem?id=2528 Descript ...

  4. POJ 2528 Mayor's posters(线段树+离散化)

    Mayor's posters 转载自:http://blog.csdn.net/winddreams/article/details/38443761 [题目链接]Mayor's posters [ ...

  5. poj 2528 Mayor's posters 线段树+离散化 || hihocode #1079 离散化

    Mayor's posters Description The citizens of Bytetown, AB, could not stand that the candidates in the ...

  6. POJ 2528 Mayor's posters (线段树+离散化)

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

  7. POJ 2528 Mayor's posters(线段树,区间覆盖,单点查询)

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

  8. POJ 2528 Mayor's posters (线段树)

    题目链接:http://poj.org/problem?id=2528 题目大意:有一个很上的面板, 往上面贴海报, 问最后最多有多少个海报没有被完全覆盖 解题思路:将贴海报倒着想, 对于每一张海报只 ...

  9. poj 2528 Mayor's posters(线段树)

    题目:http://poj.org/problem?id=2528 题意:有一面墙,被等分为1QW份,一份的宽度为一个单位宽度.现在往墙上贴N张海报,每张海报的宽度是任意的, 但是必定是单位宽度的整数 ...

  10. POJ 2528 Mayor's posters (线段树区间更新+离散化)

    题目链接:http://poj.org/problem?id=2528 给你n块木板,每块木板有起始和终点,按顺序放置,问最终能看到几块木板. 很明显的线段树区间更新问题,每次放置木板就更新区间里的值 ...

随机推荐

  1. 为什么aspx这么“慢”

    首先你要明白什么viewstate:由系统生成的一个隐藏域,用来进行页面状态保持的 里面存放着关于判断页面是否提交的Ispostback,和一些关于服务器控件的状态和数据: (说明下 ,ViewSta ...

  2. 360极速浏览器在XP系统下的一个bug

    今天在做页面开发的时候,发现360浏览器在XP系统下不支持focus事件,而主流的IE(包括IE6),firefox,chrome都没有这个问题.前段开发的悲剧啊,各种浏览器兼容性的问题.

  3. thinkphp 整合 ucenter

    http://xcodebox.com/2013/06/8855.html 参考 1   ucenter源码目录下 /advanced/examples/api目录 copy到thinkphp项目根目 ...

  4. 每天一条linux命令——halt

    halt命令用来关闭正在运行的Linux操作系统.halt命令会先检测系统的runlevel,若runlevel为0或6,则关闭系统,否则即调用shutdown来关闭系统. 语法: halt(选项) ...

  5. [JS]Cookie精通之路

    [JS]Cookie精通之路 转http://blog.163.com/neu_pdh1983/blog/static/572407020077310528915/ 发布:Cary 媒体:www.Ju ...

  6. Spring Cloud Eureka Server例子程序

    Spring-Cloud-Eureka-Server 及Client 例子程序 参考源代码:https://github.com/spring-cloud-samples/eureka 可以启动成功, ...

  7. PHP图片上传程序(完整版)

    从PHP100上搜刮来的,功能很强大.几乎考虑到了每个细节,与大家分享!~~~ <meta http-equiv="Content-Type" content="t ...

  8. POJ 2002 Squares 哈希

    题目链接: http://poj.org/problem?id=2002 #include <stdio.h> #include <string.h> ; struct Has ...

  9. Xcode6 Xcode7 Xcode 官方链接 --备用

    Xcode 6 官方下载链接: http://adcdownload.apple.com//wwdc_2014/xcode_6_beta_ie8g3n/xcode_6_beta.dmg   Xcode ...

  10. leetcode第八题 String to Integer (atoi) (java)

    String to Integer (atoi) time=272ms   accepted 需考虑各种可能出现的情况 public class Solution { public int atoi( ...