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. href与src的区别

    src是source的缩写,指向外部资源的位置,指向的内容将会嵌入到文档中当前标签所在位置:在请求src资源时会将其指向的资源下载并应用到文档内,例如js脚本,img图片和frame等元素. href ...

  2. Html5新增的语义化标签(部分)

    2014年10月29日,万维网联盟宣布,经过接近8年的艰苦努力,html5的标准规范终于制定完成.这是互联网的一次重大变革,这也许是一个时代的来临! 总结一些h5新增的语义化标签,记录下来方便自己学习 ...

  3. angularjs-ngModel 控制页面的宽度

    js NiDialog.open({ windowClass: '', size:'elements', backdrop: 'static', keyboard: false, templateUr ...

  4. [总结]RTMP流媒体技术零基础学习方法

    本文主要总结一些我在学习RTMP流媒体技术过程中积累的经验.也为后来学习RTMP流媒体技术的人们一个参考.本文力图从简到难,循序渐进的介绍RTMP流媒体技术的方方面面,先从应用说起,逐步深化剖析相关工 ...

  5. Python:运算符

    #!/usr/bin/python3 #运算符 #算术运算符 print("算术运算符:","+ - * / % **(幂) //(取整)") #比较运算符 p ...

  6. python 数据结构

    Python的数据结构主要分为set(),list[],和dict{}.这篇文章主要记载这几种结果在使用过程中的一些技巧或其中一些函数的用法区别. 1.函数get()与setdefault()区别: ...

  7. 【POJ3243】【拓展BSGS】Clever Y

    Description Little Y finds there is a very interesting formula in mathematics: XY mod Z = K Given X, ...

  8. 不学就吃亏的underscorejs类库学习示例 ——(集合篇)

    underscorejs是一个很不错的类库,我的很多项目都引用了这个类库,的确可以带来很多方便. 记得我当初学的时候,看underscorejs的api是看的一知半解的,甚至不明白api里的conte ...

  9. CSS3中动画transform必须要了解的Skew变化原理

    transform是CSS3中比较安全的动画(对于性能来说),其中有一些动画属性,来执行不同的变化.今天我们来了解skew的变化原理. skew,其实使用的频率不是很高,当然也没有最低.但是往往,一直 ...

  10. Extjs4.2.1学习笔记[更新]

    心血来潮准备学习一下Extjs,就从官方网站http://extjs.org.cn/下载了最新版本4.2.1,开始从头学习,记一下笔记,让自己能够持之以恒. 先说一下基本文件类库引用吧, 每个项目一开 ...