线段树..

--------------------------------------------------------------------------------------

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
 
#define rep( i , n ) for( int i = 0 ; i < n ; i++ )
#define clr( x , c ) memset( x , c , sizeof( x ) )
#define L( x ) ( x << 1 )
#define R( x ) ( L( x ) ^ 1 )
#define LC( x ) tree[ L( x ) ]
#define RC( x ) tree[ R( x ) ]
#define mid( l , r ) ( ( l + r ) >> 1 )
 
using namespace std;
 
const int n = 1000000;
 
struct Node {
int l , r;
int Max , add;
Node() : Max( 0 ) , add( 0 ) { }
};
 
Node tree[ n << 2 ];
 
void maintain( int x ) {
Node &o = tree[ x ];
o.Max = 0;
if( o.r > o.l )
   o.Max = max( LC( x ).Max , RC( x ).Max );
   
o.Max += o.add;
}
 
int L , R;
 
void update( int x ) {
Node &o = tree[ x ];
if( L <= o.l && o.r <= R ) 
   
   o.add += 1;
   
else {
int m = mid( o.l , o.r );
if( L <= m ) update( L( x ) );
if( m < R ) update( R( x ) );
}
maintain( x );
}
 
void build( int x , int l , int r ) {
Node &o = tree[ x ];
o.l = l , o.r = r;
if( l == r )
   return ;
   
int m = mid( l , r );
build( L( x ) , l , m );
build( R( x ) , m + 1 , r );
int main() {
// freopen( "test.in" , "r" , stdin );
int m;
cin >> m;
build( 1 , 1 , n );
while( m-- ) {
scanf( "%d%d" , &L , &R );
update( 1 );
}
printf( "%d\n" , tree[ 1 ].Max );
return 0;
}

--------------------------------------------------------------------------------------

1651: [Usaco2006 Feb]Stall Reservations 专用牛棚

Time Limit: 10 Sec  Memory Limit: 64 MB
Submit: 587  Solved: 327
[Submit][Status][Discuss]

Description

Oh those picky N (1 <= N <= 50,000) cows! They are so picky that each one will only be milked over some precise time interval A..B (1 <= A <= B <= 1,000,000), which includes both times A and B. Obviously, FJ must create a reservation system to determine which stall each cow can be assigned for her milking time. Of course, no cow will share such a private moment with other cows. Help FJ by determining: * The minimum number of stalls required in the barn so that each cow can have her private milking period * An assignment of cows to these stalls over time

有N头牛,每头牛有个喝水时间,这段时间它将专用一个Stall 现在给出每头牛的喝水时间段,问至少要多少个Stall才能满足它们的要求

Input

* Line 1: A single integer, N

* Lines 2..N+1: Line i+1 describes cow i's milking interval with two space-separated integers.

Output

* Line 1: The minimum number of stalls the barn must have.

* Lines 2..N+1: Line i+1 describes the stall to which cow i will be assigned for her milking period.

Sample Input

5
1 10
2 4
3 6
5 8
4 7

Sample Output

4

OUTPUT DETAILS:

Here's a graphical schedule for this output:

Time 1 2 3 4 5 6 7 8 9 10
Stall 1 c1>>>>>>>>>>>>>>>>>>>>>>>>>>>
Stall 2 .. c2>>>>>> c4>>>>>>>>> .. ..
Stall 3 .. .. c3>>>>>>>>> .. .. .. ..
Stall 4 .. .. .. c5>>>>>>>>> .. .. ..

Other outputs using the same number of stalls are possible.

HINT

不妨试下这个数据,对于按结束点SORT,再GREEDY的做法 1 3 5 7 6 9 10 11 8 12 4 13 正确的输出应该是3

Source

BZOJ 1651: [Usaco2006 Feb]Stall Reservations 专用牛棚( 线段树 )的更多相关文章

  1. BZOJ 1651: [Usaco2006 Feb]Stall Reservations 专用牛棚

    题目 1651: [Usaco2006 Feb]Stall Reservations 专用牛棚 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 553   ...

  2. BZOJ 1651 [Usaco2006 Feb]Stall Reservations 专用牛棚:优先队列【线段最大重叠层数】

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1651 题意: 给你n个线段[a,b],问你这些线段重叠最多的地方有几层. 题解: 先将线段 ...

  3. bzoj 1651: [Usaco2006 Feb]Stall Reservations 专用牛棚【贪心+堆||差分】

    这个题方法还挺多的,不过洛谷上要输出方案所以用堆最方便 先按起始时间从小到大排序. 我用的是greater重定义优先队列(小根堆).用pair存牛棚用完时间(first)和牛棚编号(second),每 ...

  4. 1651: [Usaco2006 Feb]Stall Reservations 专用牛棚

    1651: [Usaco2006 Feb]Stall Reservations 专用牛棚 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 566  Sol ...

  5. 【BZOJ】1651: [Usaco2006 Feb]Stall Reservations 专用牛棚(线段树/前缀和 + 差分)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1651 很奇妙.. 我们发现,每一时刻的重叠数选最大的就是答案.... orz 那么我们可以线段树维护 ...

  6. BZOJ1651: [Usaco2006 Feb]Stall Reservations 专用牛棚

    1651: [Usaco2006 Feb]Stall Reservations 专用牛棚 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 509  Sol ...

  7. BZOJ.3307.雨天的尾巴(dsu on tree/线段树合并)

    BZOJ 洛谷 \(dsu\ on\ tree\).(线段树合并的做法也挺显然不写了) 如果没写过\(dsu\)可以看这里. 对修改操作做一下差分放到对应点上,就成了求每个点子树内出现次数最多的颜色, ...

  8. BZOJ 1652: [Usaco2006 Feb]Treats for the Cows( dp )

    dp( L , R ) = max( dp( L + 1 , R ) + V_L * ( n - R + L ) , dp( L , R - 1 ) + V_R * ( n - R + L ) ) 边 ...

  9. BZOJ 1652: [Usaco2006 Feb]Treats for the Cows

    题目 1652: [Usaco2006 Feb]Treats for the Cows Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 234  Solve ...

随机推荐

  1. Springmvc+Spring+Hibernate搭建方法及实例

    Springmvc+Spring+Hibernate搭建方法及实例  

  2. Android Studio 代码混淆

    新建一个项目,Android Studio默认关闭代码混淆开关,在build.gradle文件中,如下图所示的minifyEnabled 开关,因此如果需要混淆代码,需将false改为true,然后在 ...

  3. tarjan算法大意

    Tarjan算法 (以发现者Robert Tarjan命名)是一个在图中寻找强连通分量的算法.算法的基本思想为:任选一结点开始进行深度优先搜索dfs(若深度优先搜索结束后仍有未访问的结点,则再从中任选 ...

  4. Unslider – 轻量的响应式 jQuery 内容轮播插件

    Unslider 是一款非常轻量的 jQuery 插件(压缩后不到3KB),能够用于任何 HTML 内容的滑动. 可以响应容器的大小变化,自动排布不用大小的滑块.可以通过整合 jQuery.event ...

  5. js 计算两个时间差

    /* * 计算两个日期的间隔天数* BeginDate:起始日期的文本框,格式為:2012-01-01* EndDate:結束日期的文本框,格式為:2012-01-02* 返回兩個日期所差的天數* 調 ...

  6. 1.2. chromium源代码分析 - chromiumframe - 入口函数

    ChromiumFrame的入口函数在main.cpp中,打开main.cpp.中包含3个类和_tWinMain函数._tWinMain就是我们要找的入口函数.我做了部分注释: int APIENTR ...

  7. 【转】Qt Mode/View

    1.view与Widget 在UI中,最常用的就是list/grid/tree了(在Qt中,grid被称为table).尤其是做那些数据库相关的程序,可能每个界面都要用到 list或grid.在Qt中 ...

  8. [NewCoder]复杂链表的复制

    看下面一个链表结点的定义: struct ComplexListNode { int val; struct ComplexListNode *next; struct ComplexListNode ...

  9. Hadoop学习笔记(3)hadoop伪分布模式安装

    为了学习这部分的功能,我们这里的linux都是使用root用户登录的.所以每个命令的前面都有一个#符号. 伪分布模式安装步骤: 关闭防火墙 修改ip地址 修改hostname 设置ssh自动登录 安装 ...

  10. iptables 简单配置

    通过命令 netstat -tnl 可以查看当前服务器打开了哪些端口  Ssh代码   netstat -tnl     查看防火墙设置  Ssh代码   iptables -L -n      开放 ...