3126: [Usaco2013 Open]Photo

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 222  Solved: 116

Description

Farmer John has decided to assemble a panoramic photo of a lineup of his N cows (1 <= N <= 200,000), which, as always, are conveniently numbered from 1..N. Accordingly, he snapped M (1 <= M <= 100,000) photos, each covering a contiguous range of cows: photo i contains cows a_i through b_i inclusive. The photos collectively may not necessarily cover every single cow. After taking his photos, FJ notices a very interesting phenomenon: each photo he took contains exactly one cow with spots! FJ was aware that he had some number of spotted cows in his herd, but he had never actually counted them. Based on his photos, please determine the maximum possible number of spotted cows that could exist in his herd. Output -1 if there is no possible assignment of spots to cows consistent with FJ's photographic results.

给你一个n长度的数轴和m个区间,每个区间里有且仅有一个点,问能有多少个点

Input

* Line 1: Two integers N and M.

* Lines 2..M+1: Line i+1 contains a_i and b_i.

Output

* Line 1: The maximum possible number of spotted cows on FJ's farm, or -1 if there is no possible solution.

Sample Input

5 3
1 4
2 5
3 4

INPUT DETAILS: There are 5 cows and 3 photos. The first photo contains cows 1 through 4, etc.

Sample Output

1
OUTPUT DETAILS: From the last photo, we know that either cow 3 or cow 4 must be spotted. By choosing either of these, we satisfy the first two photos as well. 
  
  考思维的一道好题啊,值得一做。
 #include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int INF=;
const int maxn=;
int L[maxn],R[maxn];
int tr[maxn<<],n,m;
int Query(int node,int l,int r,int a,int b){
if(a<=)a=;
if(a>b||b<=)return ; if(l>=a&&r<=b)return tr[node];
int mid=(l+r)>>,ret=-INF;
if(mid>=a)ret=Query(node<<,l,mid,a,b);
if(mid<b)ret=max(ret,Query(node<<|,mid+,r,a,b));
return ret;
} void Modify(int node,int l,int r,int g,int d){ if(l==r){tr[node]=d;return;}
int mid=(l+r)>>;
if(mid>=g)Modify(node<<,l,mid,g,d);
else Modify(node<<|,mid+,r,g,d);
tr[node]=max(tr[node<<],tr[node<<|]);
} int main(){
freopen("3126.in","r",stdin);
freopen("3126.out","w",stdout);
scanf("%d%d",&m,&n);m++;
for(int i=;i<=m;i++)
R[i]=i-;
for(int i=,a,b;i<=n;i++){
scanf("%d%d",&a,&b);
L[b+]=max(L[b+],a);
R[b]=min(R[b],a-);
}
for(int i=;i<=m;i++)
L[i]=max(L[i],L[i-]);
for(int i=m-;i>=;i--)
R[i]=min(R[i],R[i+]);
for(int i=;i<m;i++)
Modify(,,m,i,L[i]<=R[i]?Query(,,m,L[i],R[i])+:-INF);
printf("%d\n",max(-,Query(,,m,L[m],R[m])));
return ;
}

数据结构(线段树):BZOJ 3126: [Usaco2013 Open]Photo的更多相关文章

  1. Bzoj 3126[Usaco2013 Open]Photo 题解

    3126: [Usaco2013 Open]Photo Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 335  Solved: 169[Submit] ...

  2. BZOJ 3126 [USACO2013 Open]Photo (单调队列优化DP)

    洛谷传送门 题目大意:给你一个长度为$n$的序列和$m$个区间,每个区间内有且仅有一个1,其它数必须是0,求整个序列中数字1最多的数量 神题,竟然是$DP$ 定义$f_{i}$表示第i位放一个1时,最 ...

  3. ●BZOJ 3126 [Usaco2013 Open]Photo

    题链: http://www.lydsy.com/JudgeOnline/problem.php?id=3126 题解: 单调队列优化DP,神奇.. (好像某次考试考过,当时我用了差分约束+SPFA优 ...

  4. bzoj 3126: [Usaco2013 Open]Photo——单调队列优化dp

    Description 给你一个n长度的数轴和m个区间,每个区间里有且仅有一个点,问能有多少个点 Input * Line 1: Two integers N and M. * Lines 2..M+ ...

  5. 算法手记 之 数据结构(线段树详解)(POJ 3468)

    依然延续第一篇读书笔记,这一篇是基于<ACM/ICPC 算法训练教程>上关于线段树的讲解的总结和修改(这本书在线段树这里Error非常多),但是总体来说这本书关于具体算法的讲解和案例都是不 ...

  6. 树链剖分+线段树 BZOJ 1036 [ZJOI2008]树的统计Count

    题目链接 题意: I. CHANGE u t : 把结点u的权值改为t II. QMAX u v: 询问从点u到点v的路径上的节点的最大权值 III. QSUM u v: 询问从点u到点v的路径上的节 ...

  7. ACM/ICPC 之 数据结构-线段树+区间离散化(POJ2528)

    这道题用线段树做更方便更新和查询,但是其数据范围很大,因此要将离散化和线段树结合起来,算是一道比较经典的线段树+离散化的例题. 线段树的离散化有很多方法,在这里,我先用一次结点离散化,间接将源左右端点 ...

  8. ACM/ICPC 之 数据结构-线段树思想(POJ2182,含O(n^2)插入式解法)

    这道题在一定程度上体现了线段树的一种用法,解决的问题是:对于总计n个元素的第i个元素,已知其在[1,i]上部分序列的排名,求第i个元素在所有n个元素中的排名. 当然这道题数据比较水,所以用O(n^2) ...

  9. 线段树 || BZOJ 1112: [POI2008]砖块Klo

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1112 题解: 希望有连续K柱的高度是一样的,就先把1~K的数扔进线段树(线段树的下标就是数值 ...

随机推荐

  1. Python建立socket并获取信息

    import socket, sys port = 80 host = "www.baidu.com" print "Creating socket..." s ...

  2. 洛谷比赛 堕落的Joe

    /*暴力50*/ #include<iostream> #include<cstdio> #include<cstring> #define maxn 100010 ...

  3. 委托、 Lambda表达式和事件——委托

    简单示例 /* * 由SharpDevelop创建. * 用户: David Huang * 日期: 2015/7/27 * 时间: 10:22 */ using System; namespace ...

  4. 实现Android K的伪沉浸式

    在Android 5.0之后引入了MD风格,从而状态栏沉浸也成为了一种设计习惯.而停留在之Android L之前的Android系统则不能直接实现沉浸式,这里就介绍一下如何实现Android K系列的 ...

  5. C# 通过hessian调Java注意事项

    照理说C#可以通过标准的web服务可以轻松地调用Java,但是鉴于hessian的高性能及开发效率,个人认为C#通过hessian调用java是很值得提倡的.之前完成的一个比较大型的企业应用项目就是采 ...

  6. ORACLE用户管理方式下备份数据和复制数据库

    首先要明确的是,oracle数据库的备份可以分为逻辑备份和物理备份.           逻辑备份的是通过数据导出对数据进行备份,主要方式有老式的IMP/EXP和数据泵灯方式.适合变化较少的数据库,而 ...

  7. metalink下载补丁包

    以下截图 截取自 某 升级包中携带的 readme文档 把以上图片转换为 文字 Download and Install Patch Updates Refer to the My Oracle Su ...

  8. TOM大师脚本01-查找未建索引的外键

    [oracle@Oracle11g 2016]$ cat 022201.sql column columns format a30 word_wrappedcolumn tablename forma ...

  9. 【转】iOS8 推送 获取 devicetoken

    标签:推送 push ios8 devicetoken token xcode6 原文:http://roybaby.blog.51cto.com/1508945/1557854 打开AppDeleg ...

  10. IE6解决固定定位代码

    有些朋友在进行网页布局时,会遇到IE6浏览器不支持固定定位的兼容性问题,本博将详细介绍此问题的解决方法,需要了解的朋友可以参考下. ie6 垂直居中固定定位,代码如下: #center {_posit ...