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. bzoj 1631: [Usaco2007 Feb]Cow Party【spfa】

    正反加边分别跑spfa最短路,把两次最短路的和求个max就是答案 #include<iostream> #include<cstdio> #include<queue&g ...

  2. bzoj 1231: [Usaco2008 Nov]mixup2 混乱的奶牛【状压dp】

    设f[i][j]为奶牛选取状态为i,最后一头选的为j,转移直接f[k][(1<<(k-1)|i]+=f[j][i] #include<iostream> #include< ...

  3. HDU 5514 欧拉函数应用

    前置技能: <=i且与i互质的数的和是phi(i)*i/2 思路: 显然每个人的步数是gcd(a[i],m) 把m的所有因数预处理出来 1~m-1中的每个数 只会被gcd(m,i)筛掉一遍 // ...

  4. 数学 HDOJ 5301 Buildings

    题目传送门 /* 题意:n*m列的矩阵,删除一个格子x,y.用矩形来填充矩阵.且矩形至少有一边是在矩阵的边缘上. 求满足条件的矩形填充方式中面积最大的矩形,要使得该最大矩形的面积最小. 分析:任何矩形 ...

  5. 计算科学(转自wiki)

    计算科学(也称科学计算 scientific computation 或 SC)是一个快速增长的多学科领域,使用先进的计算能力来理解和解决复杂的问题. 计算科学包括三个不同的方面: 1. 开发用于解决 ...

  6. python批量下载图片

    从数据库拿了一批图片地址,需要一张一张的把图片下载下来,自从有了python,想到能省事就琢磨如何省事. 代码如下: import urllib.requestf=open("E:\999\ ...

  7. working hard to be a professional coder

    1:read 2 : code 3 : 勤奋 4:技术栈 就前端主流技术框架的发展而言,过去的几年里发展极快,在填补原有技术框架空白和不足的同时也渐渐趋于成熟.未来前端在已经趋向成熟的技术方向上面将会 ...

  8. TensorFlow OOM when allocating tensor with shape[5000,384707]

    在session范围内不要进行eval()或者convert_to_tensor()操作, 否则会造成OOM,或者报出错误:GraphDef cannot be larger than 2GB usi ...

  9. Python 开发初识

    从今天开始记录自己的python开发之路,用博客记录自己的学习经历,以及学习小结,小的项目模块,努力充实,做最好的自己!!!

  10. 【Linux】centos7 添加脚本到/etc/rc.local文件里,实现开机自启

    Linux 设置开机自启动,添加命令到/etc/rc.d/rc.local,本文以设置tomcat自启动为例: 一:添加自启动命令 export JAVA_HOME=/usr/java/jdk1.8. ...