题目链接

http://poj.org/problem?id=2528

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

There will be several test cases in the input. Each test case consists of N + 1 lines where N (1 ≤ N ≤ 200,000) is given in the first line of the test case. The next N lines contain the pairs of values Posi and Vali in the increasing order of i (1 ≤ iN). For each i, the ranges and meanings of Posi and Vali are as follows:

  • Posi ∈ [0, i − 1] — The i-th person came to the queue and stood right behind the Posi-th
    person in the queue. The booking office was considered the 0th person
    and the person at the front of the queue was considered the first person
    in the queue.
  • Vali ∈ [0, 32767] — The i-th person was assigned the value Vali.

There no blank lines between test cases. Proceed to the end of input.

 

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

HINT

题意

一个区间按照顺序贴n海报,海报高都为1,位置为(l,r),表示海报的位置和长度。

最后问你在最后,能看见多少个海报(即没有被其他海报完全覆盖)。

题解:

这题就是区间覆盖,区间修改,最后将标记全部下放到底,扫一遍叶子节点就好了。

需要注意的是离散化时对于区间(l,r),需要加入l,l+1,r,r+1四个点离散。

如果只离散左右端点,比如 (1,3) (3,10) (10,13)  三个海报离散后,就成了(1,2) (2,3) (3, 4),这样(2,3)就没有了。

提供一个下载本题数据的网站:https://webdocs.cs.ualberta.ca/~acpc/2003/

代码:

 #include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
#define N 100050
int n,cnt,num,ans,kth[N<<],f[N];
struct Query{int l,r;}que[N];
struct Tree{int l,r,val;}tr[N<<];
template<typename T>void read(T&x)
{
int k=;char c=getchar();
x=;
while(!isdigit(c)&&c!=EOF)k^=c=='-',c=getchar();
if (c==EOF)exit();
while(isdigit(c))x=x*+c-'',c=getchar();
x=k?-x:x;
}
void push_down(int x)
{
if (tr[x].val==)return ;
Tree &a=tr[x<<],&b=tr[x<<|];
a.val=tr[x].val;
b.val=tr[x].val;
tr[x].val=;
}
void bt(int x,int l,int r)
{
++num;
tr[x]={l,r,};
if (l==r)return;
int mid=(l+r)>>;
bt(x<<,l,mid);
bt(x<<|,mid+,r);
}
void update(int x,int l,int r,int tt)
{
if (l<=tr[x].l&&tr[x].r<=r)
{
tr[x].val=tt;
return;
}
int mid=(tr[x].l+tr[x].r)>>;
push_down(x);
if (l<=mid)update(x<<,l,r,tt);
if (mid<r)update(x<<|,l,r,tt);
}
void query(int x)
{
if (tr[x].l==tr[x].r)
{
int tt=f[tr[x].val]==;
ans+=tt;
f[tr[x].val]=;
return;
}
int mid=(tr[x].l+tr[x].r)>>;
push_down(x);
query(x<<);
query(x<<|);
}
void clear()
{
num=; ans=; cnt=;
memset(f,,sizeof(f));
}
void input()
{
read(n);
for(int i=;i<=n;i++)
{
read(que[i].l);read(que[i].r);
kth[++cnt]=que[i].l;
kth[++cnt]=que[i].r;
kth[++cnt]=que[i].l+;
kth[++cnt]=que[i].r+;
}
sort(kth+,kth+cnt+);
cnt=unique(kth+,kth+cnt+)-kth-;
bt(,,cnt);
}
void work()
{ for(int i=;i<=n;i++)
{
int l=lower_bound(kth+,kth+cnt+,que[i].l)-kth;
int r=lower_bound(kth+,kth+cnt+,que[i].r)-kth;
update(,l,r,i);
}
f[]=;
/* for(int x=1;x<=n*8;x++)
if (tr[x].l==tr[x].r)
{
ans+=f[tr[x].val]==0;
f[tr[x].val]=1;
}
else push_down(x);
*/
query();
printf("%d\n",ans);
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("aa.in","r",stdin);
#endif
int q;
read(q);
while(q--)
{
clear();
input();
work();
}
}

Mayor's posters 线段树区间覆盖的更多相关文章

  1. poj2528 Mayor's posters(线段树区间覆盖)

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

  2. POJ 2528 Mayor's posters (线段树+区间覆盖+离散化)

    题意: 一共有n张海报, 按次序贴在墙上, 后贴的海报可以覆盖先贴的海报, 问一共有多少种海报出现过. 题解: 因为长度最大可以达到1e7, 但是最多只有2e4的区间个数,并且最后只是统计能看见的不同 ...

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

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

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

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

  5. POJ2528:Mayor's posters(线段树区间更新+离散化)

    Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral electio ...

  6. poj2528 Mayor's posters(线段树区间修改+特殊离散化)

    Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral electio ...

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

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

  8. poj-----(2528)Mayor's posters(线段树区间更新及区间统计+离散化)

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

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

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

随机推荐

  1. python:ord()和chr()——字符串和ASCll编码转换

    ord()函数:字符串——>ascll编码 chr()函数:ascll编码——>字符串 #函数 for i in range(ord('d'),ord('f')+1):#拿到d和f对应的a ...

  2. Lambda表达式在Android开发中的应用

    在Java8中拥有Lambda表达式的新功能,如果现在Android项目中使用,首先,必须在项目中的build.gradle配置一下 使用Lambda表达式必须满足只有一个待实现方法这个规则,否则就不 ...

  3. 在MyEclipse中用debug调试应用程序

    F5:单步测试,作用是跳入,比如说一大步中分为10小步,单击F5一次就会走完一小步,走完这一大步则需要单步10次.F6:与F5一样也是单步测试.只不过与F5不同的是F5追求的是过程,而F6追求的是结果 ...

  4. sqlserver里常用的语法

    bb 为nvarchar(50)CAST(bb AS int) select MAX(CAST(bb AS int)) from AAA

  5. java基于feemarker 生成word文档(超级简单)

    问题由来: 开发个新需求,需要按规定导出word文档,文档截图如下 因为之前没做过这个,一脸懵B啊,导出excel和txt倒是经常接触到,对于这个word这种格式不严谨的文件怎么处理呢? 技术选型:可 ...

  6. Action的方法访问

    -------------------siwuxie095 Action 的方法访问 Action 的方法访问方式共有三种,如下: 1.通过 method 属性的配置实现 在 action 标签的 m ...

  7. mysql5.6配置semi_sync

    测试环境:Red Hat Enterprise Linux Server release 6.3 (Santiago)Server version: 5.6.22-log MySQL Communit ...

  8. DALSA相机SDK不完全教程

    1.SDK介绍 Dalsa是全球顶尖的CCD/CMOS芯片和相机制造商,总部位于加拿大,我使用的是 Dalsa的 Genie_TS_M1920(黑白)和 Genie_TS_C2048(彩色)两款工业相 ...

  9. Source Insight: has been changed outside of Source Insight

    has been changed outside of Source Insight 2018年04月26日 09:41:01 linux_c_coding_man 阅读数:247 摘自:https: ...

  10. servletConfig的应用

    在Servlet的配置文件中,可以使用一个或多个<init-param>标签为servlet配置一些初始化参数. 当servlet配置了初始化参数后,web容器在创建servlet实例对象 ...