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. //图片粘不上来,一直转圈圈,uva链接,洛谷链接

Sample Input

1
5
1 4
2 6
8 10
3 4
7 10

Sample Output

4

解题思路

  按顺序一张张贴上,这就是线段树的区间修改,最后统计时把墙从左到右每个格子扫一遍,用一个桶统计还剩下哪些编号的海报,嗯,没了。
  然后,MLE#滑稽,加个离散化。以前写的用map、set去重、离散化的方式效率太低,这次去洛谷上学了个更好用一点的离散化 链接 ,sort+unique+lower_bound(好像这才是别人的标配啊)
  然后愉快交题,然后WA了。随便来一组数据——
1
3
1 6
1 3
5 6
  离散化以后会发现,3和5之间的空隙4被离散化没了。解决方法——把每张海报结束的下一个编号也离散化一下,就是对每张海报,多离散化一个数据——7、4、7
  然后,数组下标小心一点,AC。

源代码

 #include<stdio.h>
#include<algorithm> int n,T; int post[][]/*海报位置*/,input[]/*需要离散化的数*/; struct Segtree{
int l,r;
int c;//如果区间长度为1,则记录海报编号,否则随缘(记录的啥我不管)(这里好像可以再优化一下,把c弄出去,搞成一个长度1e5的数组)
}s[];//从1号开始
int lazy[];
inline int lson(int a){return a<<;}
inline int rson(int a){return (a<<)|;}
void maketree(int x,int l,int r)
{
lazy[x]=;
if(l==r)
{
s[x]={l,r,};
return;
}
s[x].l=l;
s[x].r=r;
int mid=l+r>>;
maketree(lson(x),l,mid);
maketree(rson(x),mid+,r);
s[x].c=;
}
inline void pushdown(int x)
{
if(!lazy[x]) return;
int ls=lson(x),rs=rson(x);
s[ls].c=lazy[x];
lazy[ls]=lazy[x];
s[rs].c=lazy[x];
lazy[rs]=lazy[x];
lazy[x]=;
}
int quepos(int x,int pos)
{
int mid=s[x].l+s[x].r>>;
if(s[x].l==s[x].r) return s[x].c;
if(lazy[x]) return lazy[x];
if(pos<=mid) return quepos(lson(x),pos);
else return quepos(rson(x),pos);
}
void update(int x,int l,int r,int k)
{
if(l>s[x].r||r<s[x].l) return;
if(l<=s[x].l&&s[x].r<=r)
{
// s[x].sum+=k*(s[x].r-s[x].l+1);
if(s[x].l==s[x].r) s[x].c=k;
else lazy[x]=k;
return;
}
pushdown(x);
update(lson(x),l,r,k);
update(rson(x),l,r,k);
} int main()
{
//freopen("test.in","r",stdin);//因为忘记注释这个,WA了不知多少
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
int len=;//离散化数组input的长度
for(int i=;i<=n;i++)
{
scanf("%d%d",&post[i][],&post[i][]);
input[len++]=post[i][];
input[len++]=post[i][];
input[len++]=post[i][]+;
} std::sort(input,input+len+);
len=std::unique(input,input+len+)-input;
for(int i=;i<=n;i++)
{
post[i][]=std::lower_bound(input,input+len,post[i][])-input;
post[i][]=std::lower_bound(input,input+len,post[i][])-input;
} maketree(,,len);
for(int i=;i<=n;i++)
update(,post[i][],post[i][],i);//海报编号1~n
int *count;
count=new int[n]();
for(int i=;i<=len;i++)
count[quepos(,i)]=; int ans=;
for(int i=;i<=n;i++) ans+=count[i];
delete count;
printf("%d\n",ans);
}
return ;
}

POJ2528 Uva10587 Mayor's posters的更多相关文章

  1. 【poj2528】Mayor's posters

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

  2. 【SDOJ 3741】 【poj2528】 Mayor's posters

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

  3. 【poj2528】Mayor's posters

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 59254   Accepted: 17167 Description The ...

  4. 线段树---poj2528 Mayor’s posters【成段替换|离散化】

    poj2528 Mayor's posters 题意:在墙上贴海报,海报可以互相覆盖,问最后可以看见几张海报 思路:这题数据范围很大,直接搞超时+超内存,需要离散化: 离散化简单的来说就是只取我们需要 ...

  5. Mayor's posters(线段树+离散化POJ2528)

    Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 51175 Accepted: 14820 Des ...

  6. poj2528 Mayor's posters(线段树之成段更新)

    Mayor's posters Time Limit: 1000MSMemory Limit: 65536K Total Submissions: 37346Accepted: 10864 Descr ...

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

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

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

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

  9. [POJ2528]Mayor's posters(离散化+线段树)

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

随机推荐

  1. mysql 的索引hash和b+tree 区别

    索引hash相当于数组,键值对组合,对于id = 6或者status= 2这样条件查询,但是对于id>12等这样,用btree索引最好.

  2. 【计蒜客习题】 取石子游戏(gcd)

    问题描述 蒜头君和花椰妹在玩一个游戏,他们在地上将 n 颗石子排成一排,编号为 1 到 n.开始时,蒜头君随机取出了 2 颗石子扔掉,假设蒜头君取出的 2 颗石子的编号为 a, b.游戏规则如下,蒜头 ...

  3. 生成自签名ca 证书 使nginx 支持https

    创建服务器私钥,命令会让你输入一个口令:$ openssl genrsa -des3 -out server.key 1024创建签名请求的证书(CSR):$ openssl req -new -ke ...

  4. 一个包含所有C++头文件的头函数

    #include<bits/stdc++.h> using namespace std; 使用方法和平常的头文件一样,#include<bits/stdc++.h>包含以下头文 ...

  5. Spring加载applicationContext.xml实现spring容器管理的几种方式

    package com.etc.test; import org.junit.Test; import org.springframework.beans.factory.BeanFactory; i ...

  6. 全面学习ORACLE Scheduler特性(9)创建Chains

    五.使用Chains 今天要来认识一位新同学:CHAIN(注意不要敲成CHINA).CHAIN可以被视做一组Programs的复合,举个简单的例子:运行PROGRAM:A以及PROGRAM:B,如果成 ...

  7. 基于Web的Kafka管理器工具之Kafka-manager的编译部署详细安装 (支持kafka0.8、0.9和0.10以后版本)(图文详解)(默认端口或任意自定义端口)

    不多说,直接上干货! 至于为什么,要写这篇博客以及安装Kafka-manager? 问题详情 无奈于,在kafka里没有一个较好自带的web ui.启动后无法观看,并且不友好.所以,需安装一个第三方的 ...

  8. Spark学习笔记1:Application,Driver,Job,Task,Stage理解

    看了spark的原始论文和相关资料,对spark中的一些经常用到的术语做了一些梳理,记录下. 1,Application application(应用)其实就是用spark-submit提交的程序.比 ...

  9. C语言指针的理解以及指针的指针的理解

    指针指向的是内存地址编号,内存地址编号指向的是对应的内容. 我们需要一个变量,来储存内存地址编号,这个变量的值是一个内存地址编号,但是我们可以通过修改变量的值,来不断的改变内存地址编号. 但是,我们如 ...

  10. Asp.net MVC中文件上传的参数转对象的方法

    参照博友的.NET WebApi上传文件接口(带其他参数)实现文件上传并带参数,当需要多个参数时,不想每次都通过HttpContext.Request.Params去取值,就针对HttpRequest ...