数据结构(线段树):BZOJ 3126: [Usaco2013 Open]Photo
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
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
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的更多相关文章
- Bzoj 3126[Usaco2013 Open]Photo 题解
3126: [Usaco2013 Open]Photo Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 335 Solved: 169[Submit] ...
- BZOJ 3126 [USACO2013 Open]Photo (单调队列优化DP)
洛谷传送门 题目大意:给你一个长度为$n$的序列和$m$个区间,每个区间内有且仅有一个1,其它数必须是0,求整个序列中数字1最多的数量 神题,竟然是$DP$ 定义$f_{i}$表示第i位放一个1时,最 ...
- ●BZOJ 3126 [Usaco2013 Open]Photo
题链: http://www.lydsy.com/JudgeOnline/problem.php?id=3126 题解: 单调队列优化DP,神奇.. (好像某次考试考过,当时我用了差分约束+SPFA优 ...
- bzoj 3126: [Usaco2013 Open]Photo——单调队列优化dp
Description 给你一个n长度的数轴和m个区间,每个区间里有且仅有一个点,问能有多少个点 Input * Line 1: Two integers N and M. * Lines 2..M+ ...
- 算法手记 之 数据结构(线段树详解)(POJ 3468)
依然延续第一篇读书笔记,这一篇是基于<ACM/ICPC 算法训练教程>上关于线段树的讲解的总结和修改(这本书在线段树这里Error非常多),但是总体来说这本书关于具体算法的讲解和案例都是不 ...
- 树链剖分+线段树 BZOJ 1036 [ZJOI2008]树的统计Count
题目链接 题意: I. CHANGE u t : 把结点u的权值改为t II. QMAX u v: 询问从点u到点v的路径上的节点的最大权值 III. QSUM u v: 询问从点u到点v的路径上的节 ...
- ACM/ICPC 之 数据结构-线段树+区间离散化(POJ2528)
这道题用线段树做更方便更新和查询,但是其数据范围很大,因此要将离散化和线段树结合起来,算是一道比较经典的线段树+离散化的例题. 线段树的离散化有很多方法,在这里,我先用一次结点离散化,间接将源左右端点 ...
- ACM/ICPC 之 数据结构-线段树思想(POJ2182,含O(n^2)插入式解法)
这道题在一定程度上体现了线段树的一种用法,解决的问题是:对于总计n个元素的第i个元素,已知其在[1,i]上部分序列的排名,求第i个元素在所有n个元素中的排名. 当然这道题数据比较水,所以用O(n^2) ...
- 线段树 || BZOJ 1112: [POI2008]砖块Klo
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1112 题解: 希望有连续K柱的高度是一样的,就先把1~K的数扔进线段树(线段树的下标就是数值 ...
随机推荐
- Python开发【第二十篇】:缓存
Python开发[第二十篇]:缓存redis&Memcache 点击这里 Python之路[第九篇]:Python操作 RabbitMQ.Redis.Memcache.SQLAlchemy ...
- ASP.NET 打包下载文件
使用的类库为:ICSharpCode.SharpZipLib.dll 一种是打包整个文件夹,另一种是打包指定的多个文件,大同小异: using ICSharpCode.SharpZipLib.Zip; ...
- 21、javascript 基础
Javascript Javascript 是一种嵌入到HTML文档的脚本语言,由浏览器解释和执行,无需编译. Javascript 是大小写敏感的,大写字母与小写字母不同. 使用“:”分号来分割每一 ...
- spring验证事务的代码,用到了mockito
package *.withdraw; import javax.annotation.Resource; import org.junit.Before; import org.junit.Test ...
- Android Animation学习 实现 IOS 滤镜退出动画
IOS的用户体验做的很好,其中一点很重要的地方就是动画效果. 最近在学习Android的Animation,简单实现了一个IOS相机滤镜退出的动画: 布局文件:activity_animation_d ...
- java 引用资源-ClassLoader.getResource()方法
如图,eclipse中我的包结构为:,我在 spt.app.MainFrame 中可以通过一下代码段使用资源: public static Object obj = ImageIconProxy.cl ...
- 使用EMMET中的小坑
使用EMMET写HTML的时候,是一个非常爽的事情.但是今天我使用时,发现一个小坑.以前倒也没有注意,不过需要非常的小心. form[action="/process" metho ...
- CAEmitterLayer
-(void)createFireworks{ CAEmitterLayer *fireworks = [CAEmitterLayer layer]; fireworks.emitterPositio ...
- Java Swing 使用总结(转载)
随笔转载自:此去经年ぢ 地址:http://www.cnblogs.com/FLFL/p/5369756.html 1. GUI编程引言 以前的学习当中,我们都使用的是命令交互方式: 例如:在 ...
- slf4j与log4j
推荐使用SLF4J(Simple Logging Facade for Java)作为日志的api,SLF4J是一个用于日志系统的简单Facade,允许最终用户在部署其应用时使用其所希望的日志系统. ...