线段树..

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

#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. Telnet自动登录

    http://zw7534313.iteye.com/blog/1603808 http://network.51cto.com/art/201007/212255_all.htm (s=`stty ...

  2. COB(Chip On Board) 工艺技术

    COX(Chip On X) •X 基板:  PCB (Printed circuit board)  FPC (Flexible Printed Circuit)  Glass •导线焊接 球形焊接 ...

  3. 再探Delphi2010 Class的构造和析构顺序

    发了上一篇博客.盒子上有朋友认为Class的构造和析构延迟加载.是在Unit的初始化后调用的Class的构造.在Unit的反初始化前调用的Class的析构函数. 为了证明一下我又做了个试验 unit ...

  4. centos 磁盘扩容,新建lv

    1,扩容已有lvm 上的lv 1.1 新建pv --> pvcreate /dev/sd* 1.2 把新增的pv添加到lvm -->vgextend vg_ruiy /dev/sd* 1. ...

  5. angularJS使用$watch监控数据模型的变化

    使用$watch监控数据模型的变化 在scope 内置的全部函数中,用得最多的可能就是$watch 函数了.当你的数据模型中某一部分发生变化时,$watch 函数能够向你发出通知.你能够监控单个对象的 ...

  6. C++学习笔记29,引用变量(1)

    引用变量在创建的时候就必须初始化.无法创建一个未被初始化的引用. #include <iostream> using namespace std; int main() { int x=1 ...

  7. HDU3853-LOOPS(概率DP求期望)

    LOOPS Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 125536/65536 K (Java/Others) Total Su ...

  8. C标准中一些预定义的宏

    C标准中指定了一些预定义的宏,对于编程经常会用到.下面这个表中就是一些常常用到的预定义宏. 宏(双下滑线) 意义 __DATE__ 进行预处理的日期(“Mmm dd yyyy”形式的字符串文字) __ ...

  9. XML 文档解析操作

    sing System;using System.Data;using System.Configuration;using System.Web;using System.Web.Security; ...

  10. 【SVN】使用花生壳映射VisualSVN外网地址

    内网端口设置为443,花生壳映射设置的时候不能使用80端口,,需要用其他端口,,