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.

===================================华丽丽的分割线============================================

只要写一个支持区间修改和全局最大值查询的东西就好辣~

那不如直接写一个线段数暖手手~~~

这题好像可以直接差分然后就完了吧。。。

时间复杂度O(nlogn),代码如下:

 #include <bits/stdc++.h>
#define Maxn 1000007
using namespace std;
int read()
{
int x=,f=;char ch=getchar();
while (ch<''||ch>''){if (ch=='-') f=-;ch=getchar();}
while (ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
struct seg
{
int lx,rx,mx,tag;
};
seg tree[Maxn*];
int n;
void build(int node, int l, int r)
{
tree[node].lx=l,tree[node].rx=r;
tree[node].tag=,tree[node].mx=;
if (l==r) return;
int mid=(l+r)/;
build(node*,l,mid);
build(node*+,mid+,r);
}
void pushdown(int node)
{
if (tree[node].tag==) return;
tree[node*].tag+=tree[node].tag;
tree[node*].mx+=tree[node].tag;
tree[node*+].tag+=tree[node].tag;
tree[node*+].mx+=tree[node].tag;
tree[node].tag=;
}
void update(int node, int l, int r, int del)
{
if (tree[node].rx<l) return;
if (tree[node].lx>r) return;
if (tree[node].lx>=l&&tree[node].rx<=r)
{
tree[node].tag+=del;
tree[node].mx+=del;
return;
}
pushdown(node);
update(node*,l,r,del);
update(node*+,l,r,del);
tree[node].mx=max(tree[node*].mx,tree[node*+].mx);
}
int main()
{
n=read();
build(,,);
for (int i=;i<=n;i++)
{
int x=read(),y=read();
update(,x,y,);
}
printf("%d\n",tree[].mx);
return ;
}

【数据结构】bzoj1651专用牛棚的更多相关文章

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

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

  2. BZOJ 1651: [Usaco2006 Feb]Stall Reservations 专用牛棚( 线段树 )

    线段树.. -------------------------------------------------------------------------------------- #includ ...

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

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

  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. bzoj 1651: [Usaco2006 Feb]Stall Reservations 专用牛棚【贪心+堆||差分】

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

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

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

  8. usaco silver刷水~其实是回顾一下,补题解

    [BZOJ1606][Usaco2008 Dec]Hay For Sale 裸01背包 ;i<=n;i++) for(int j=m;j>=a[i];j--) f[j]=max(f[j], ...

  9. bzoj AC倒序

    Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...

随机推荐

  1. js字符串操作函数

    js字符串函数 JS自带函数 concat 将两个或多个字符的文本组合起来,返回一个新的字符串. var a = "hello"; var b = ",world&quo ...

  2. Python连接符的种类和使用区别

    python的连接符主要有 加号(+).逗号(,).空格(   ) .反斜线(\).join()的方式. 加号(+),demo如下: #注意,+只能连接字符串,如果一个是字符串一个是数字就会报错 pr ...

  3. 使用Visual Studio 2017构建.Net Core的Docker镜像

    1 Docker  镜像优化 微软在为开发人员生成 Docker 镜像时,提供以下三种主要方案: 用于开发 .NET Core 应用的 镜像 用于构建生成 .NET Core 应用的 镜像 用于运行 ...

  4. Qt 实现脉搏检测-1-心跳曲线部分

    最新的想法就是写一个显示脉搏的东西,主要就是通过串口读取硬件(检测心跳的)传来的数据,在显示一下. 先实现画心跳曲线 如下图 先来电干货, 首先,在这个代码中,第一次用到了list这个东东 所以,关于 ...

  5. Python全栈 MongoDB 数据库(数据的修改)

    修改操作符的使用   $set 修改一个域的值,增加一个域   阿哲年龄修改为33 db.class1.update({name:'阿哲'},{$set:{age:33}})   如果sex域不存在则 ...

  6. Java并发基础--线程通信

    java中实现线程通信的四种方式 1.synchronized同步 多个线程之间可以借助synchronized关键字来进行间接通信,本质上是通过共享对象进行通信.如下: public class S ...

  7. Java项目启动时候报Neither the JAVA_HOME nor the JRE_HOME environment variable is defined 解决办法

    今天在发布Java项目的时候又遇到    Neither the JAVA_HOME nor the JRE_HOME environment variable is defined  At leas ...

  8. VS 附加进程调试

    1.在IIS 上新建一个项目,制定目录到 项目根目录. 2.制定IIS上指定 主机名称如: vk.com 3. 修改主机HOST 文件:C:\Windows\System32\drivers\etc ...

  9. SQLAlchemy技术文档(中文版)(上)

    在学习SQLAlchemy的过程中,好多时候需要查官方Tutorial,发现网上并没有完整的中文版,于是利用这两天空余时间粗略翻译了一下. 翻译效果很差....但也算是强迫自己通读一遍Tutorial ...

  10. sql如何先排序再去重

    场景 有一张得分表(score),记录了用户每次的得分,同一个人可能有多个得分. id name score 1 tom 45 2 jack 78 3 tom 34 . . . 需求:找出分数最高的前 ...