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之路【第十四篇】:AngularJS --暂无内容-待更新

    Python之路[第十四篇]:AngularJS --暂无内容-待更新

  2. ASP.NET JSON的序列化和反序列化 之 Newtonsoft.Json

    我们用到的类库为:Newtonsoft.Json,通过VS工具中NuGet程序包可以下载. 一:对象转json-序列化 public class Student { public int ID { g ...

  3. 第二天——hibernate讲完了

    hibernate 逐步优化 第一步 只按照步骤来提取的 jre包导入错误 第二步 继续封装,把增删改查提取出来,同时进行代码的封装 HQL语句 be stranger in the code .be ...

  4. 关于Android4.x系统默认显示方向各种修改

    1.设置属性值 在device.mk文件中加入PRODUCT_PROPERTY_OVERRIDES += \ ro.sf.hwrotation=180 2.设置屏幕默认显示方向 在frameworks ...

  5. js正则实现用户输入银行卡号的控制及格式化

    //js正则实现用户输入银行卡号的控制及格式化 <script language="javascript" type="text/javascript"& ...

  6. 最简单的基于FFmpeg的移动端例子:IOS 视频转码器

    ===================================================== 最简单的基于FFmpeg的移动端例子系列文章列表: 最简单的基于FFmpeg的移动端例子:A ...

  7. 寻找链表中倒数第K个结点的位置

    输入一个链表,输出该链表中倒数第K个结点. struct ListNode { int m_nValue; ListNode* m_pNext; }; ListNode* FindKthToTail( ...

  8. Problem 1108 - 淼·诺贝尔

    #include<iostream> #include<vector> #include<algorithm> using namespace std; struc ...

  9. JS判断浏览器是否支持某一个CSS3属性的方法

    var div = document.createElement('div'); console.log(div.style.transition); //如果支持的话, 会输出 "&quo ...

  10. window对象细节(转载)

    Window对象是客户端javascript最高层对象之一,只要打开浏览器窗口,不管该窗口中是否有打开的网页,当遇到BODY.FRAMESET或FRAME元素时,都会自动建立window对象的实例.另 ...