甚至DFS也能过吧

Mayor's posters POJ - 2528

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
kuangbing专题也放了这道题,确实是比较经典的线段树,但是这个还没2有涉及到修改操作
sort+lower_bound+unique离散下
#include <stdio.h>
#include <algorithm>
using namespace std;
const int N=;
int b[N<<],a[N<<][],bj[N<<],M,H,bn;
int T[N*];
void built(int n) {
H=;
for(int i=; i<n+; i<<=)H++;
M=<<H;
for(int i=; i<=M<<; i++) T[i]=;
for(int i=; i<=n; i++)bj[i]=;
}
void update(int l,int r,int val) {
for(l=l+M-,r=r+M+;l^r^;l>>=,r>>=) {
if(~l&)T[l^]=val;
if(r&)T[r^]=val;
}
}
void query(int pos) {
int ans=;
for(int i=pos+M; i>; i>>=)
ans=max(ans,T[i]);
bj[ans]=;
}
int main() {
int t,n;
scanf("%d",&t);
while(t--) {
bn=;
scanf("%d",&n);
for(int i=; i<=n; i++) {
scanf("%d%d",&a[i][],&a[i][]);
b[++bn]=a[i][];
b[++bn]=a[i][];
}
sort(b+,b+bn+);
bn=unique(b+,b+bn+)-b-;
built(bn);
for(int i=; i<=n; i++) {
int l=lower_bound(b+,b+bn+,a[i][])-b;
int r=lower_bound(b+,b+bn+,a[i][])-b;
update(l,r,i);
}
int ans=;
for(int i=; i<=bn; i++) query(i);
for(int i=; i<=bn; i++) if(bj[i])ans++;
printf("%d\n",ans);
}
return ;
}

线段树 Mayor's posters的更多相关文章

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

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

  2. poj 2528 Mayor's posters(线段树+离散化)

    /* poj 2528 Mayor's posters 线段树 + 离散化 离散化的理解: 给你一系列的正整数, 例如 1, 4 , 100, 1000000000, 如果利用线段树求解的话,很明显 ...

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

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

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

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

  5. 【POJ】2528 Mayor's posters ——离散化+线段树

    Mayor's posters Time Limit: 1000MS    Memory Limit: 65536K   Description The citizens of Bytetown, A ...

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

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

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

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

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

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

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

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

随机推荐

  1. mysql 定时任务和存储过程

    mysql 定时任务和存储过程 最近在做日志系统,中间用到了 mysql, 其中有一个要求: 把数据库中 7天之后的日志清除了.看到 mysql 也支持 定时任务.于是就用 mysql 来做了.下面就 ...

  2. 使用Spring Cloud Feign

    使用Spring Cloud Feign作为HTTP客户端调用远程HTTP服务 在spring Cloud Netflix栈中,各个微服务都是以HTTP接口的形式暴露自身服务的,因此在调用远程服务时就 ...

  3. volatile双重锁实现单例

    双重锁实现单例时遭到质疑,既是:双重锁也无法保证单例模式! 原因是:指令会重排序,普通的变量仅仅会保证该方法在执行时,所有依赖的赋值结果是正确的,但不会保证执行顺序! 为什么会重排序:指令重排序是指c ...

  4. Android ScrollView嵌套RecyclerView导致在三星s8曲面屏显示不全问题

    当RecyclerView适配显示不全时可以单独给其嵌套一个相对布局!!!(必须是相对布局),这样在曲面屏手机就可以全部显示出来如下图所示 <RelativeLayout android:lay ...

  5. Xmind几个有用的技巧

    Xmind是一个很好的思维导图工具,是学习研究总结的好帮手. Xmind功能很丰富,这里只是简要列出几个比较有用的技巧. 1.善用属性 选中一个xmind元素(专业名词叫[主题])后,一般在右下角会出 ...

  6. sql server Cannot resolve the collation conflict between "Chinese_PRC_BIN" and "Chinese_PRC_CI_AS" in the equal to operation

    今天查询二个db,出现这个错误,二种方法,一种是把db里的collation改成一样的:如果不方便可以直接在sql语句后面转一下: select * from table where crm_mscr ...

  7. 洛谷 P1926 小书童——刷题大军

    题目背景 数学是火,点亮物理的灯:物理是灯,照亮化学的路:化学是路,通向生物的坑:生物是坑,埋葬学理的人. 文言是火,点亮历史宫灯:历史是灯,照亮社会之路:社会是路,通向哲学大坑:哲学是坑,埋葬文科生 ...

  8. Python 学习日志9月19日

    9月19日 周二 今天是普通的一天,昨天也是普通的一天,刚才我差点忘记写日志,突然想起来有个事情没做,回来写. 今天早晨学习<Head First HTML and CSS>第十一章节“布 ...

  9. Scalatra

    SBT和giter8 在你开始着手之前,你需要安装两个工具(我假设你已经安装了JDK1.6+).我将给你提供简缩的安装指令,详细版的安装指令可通过 下面的scalatra页面找到( http://ww ...

  10. 签名ipa,让其它手机也安装

    开发的时候,需要将app让其它人装上测试,虽然通过xcode可以使用编译进去,但是仍显不方便. 网上有个工具, http://code.google.com/p/iresign/ 通过这个工具,使用自 ...