数据结构(线段树):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

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.

  

  考思维的一道好题啊,值得一做。

 1 #include <iostream>
 2 #include <cstring>
 3 #include <cstdio>
 4 using namespace std;
 5 const int INF=2147483647;
 6 const int maxn=200010;
 7 int L[maxn],R[maxn];
 8 int tr[maxn<<2],n,m;
 9 int Query(int node,int l,int r,int a,int b){
10     if(a<=0)a=1;
11     if(a>b||b<=0)return 0;
12
13     if(l>=a&&r<=b)return tr[node];
14     int mid=(l+r)>>1,ret=-INF;
15     if(mid>=a)ret=Query(node<<1,l,mid,a,b);
16     if(mid<b)ret=max(ret,Query(node<<1|1,mid+1,r,a,b));
17     return ret;
18 }
19
20 void Modify(int node,int l,int r,int g,int d){
21
22     if(l==r){tr[node]=d;return;}
23     int mid=(l+r)>>1;
24     if(mid>=g)Modify(node<<1,l,mid,g,d);
25     else Modify(node<<1|1,mid+1,r,g,d);
26     tr[node]=max(tr[node<<1],tr[node<<1|1]);
27 }
28
29 int main(){
30     freopen("3126.in","r",stdin);
31     freopen("3126.out","w",stdout);
32     scanf("%d%d",&m,&n);m++;
33     for(int i=1;i<=m;i++)
34         R[i]=i-1;
35     for(int i=1,a,b;i<=n;i++){
36         scanf("%d%d",&a,&b);
37         L[b+1]=max(L[b+1],a);
38         R[b]=min(R[b],a-1);
39     }
40     for(int i=2;i<=m;i++)
41         L[i]=max(L[i],L[i-1]);
42     for(int i=m-1;i>=1;i--)
43         R[i]=min(R[i],R[i+1]);
44     for(int i=1;i<m;i++)
45         Modify(1,1,m,i,L[i]<=R[i]?Query(1,1,m,L[i],R[i])+1:-INF);
46     printf("%d\n",max(-1,Query(1,1,m,L[m],R[m])));
47     return 0;
48 }
时间: 2024-08-25 14:32:29

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

[BZOJ] 3126: [Usaco2013 Open]Photo

3126: [Usaco2013 Open]Photo Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 318  Solved: 162[Submit][Status][Discuss] Description Farmer John has decided to assemble a panoramic photo of a lineup of his N cows (1 <= N <= 200,000), which, as always,

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

Description 给你一个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. S

HDU 4902 Nice boat(数据结构-线段树)

Nice boat Problem Description There is an old country and the king fell in love with a devil. The devil always asks the king to do some crazy things. Although the king used to be wise and beloved by his people. Now he is just like a boy in love and c

Chapter 3. 数据结构 线段树

Chapter 3. 数据结构 线段树 Sylvia's I.单点修改,区间查询. 模板: //单点修改 区间求和 //1操作 单点修改//2操作 区间求和 #include<cstdio> #include<iostream> using namespace std; #define MAXN 500005 int sum[MAXN<<2]; int n,m; void PushUp(int rt){//求和 sum[rt]=sum[rt<<1]+sum[

HDU 1394 Minimum Inversion Number (数据结构-线段树)

Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 9514    Accepted Submission(s): 5860 Problem Description The inversion number of a given number sequence a1, a2, ..., an

不可能数据结构(线段树+思维+找规律)

不可能数据结构 Description造一道数据结构题是一件很累的事情.即使是有了坚固的数据结构造题程式,出题人仍然不能够一劳永逸.问题大概出在,造题程式并不总能生成对的题.比如,造题程式无意产生了一道出题人认为不可做题,这便成了一道错题.嘛,出了错题,其实也是没有关系的.出题人可以拿着暴力程序跑一个星期来跑出所需要的测试数据.一个星期跑不出来就延期一个星期机考好了,至于算法和讲题嘛,也就看看现场ac代码现学现卖咯?可能对你们来说很不幸的现实是,这道题似乎是个错题.给你一个初始n个元素的序列,有

数据结构——线段树

线段树是一种基于分治思想的类似于二叉树的数据结构,一般用于数组的信息统计,相比于树状数组,线段树有着更广阔的应用空间,但是相对的其代码量长,且常数大 一. 首先我们来讲线段树的建树过程,请看下图: 这张图就是线段树的存储结构,我们从最长的区间开始依次分成两部分,每一部分都有一个需要维护的权,建树过程比较简单,代码如下: inline void build(int l,int r,int rt) //l表示当前的左端点,r表示右端点,rt是当前区间的编号 { if(l == r) //当左右端点相

[数据结构-线段树] poj 2528

在一面墙上贴海报,贴的顺序给出了,求最后能被看到的海报数量. 纯粹的线段树模拟题. 但数据范围给了10^7,超内存了. 实际上这里用了一个小技巧,虽然墙的宽度是很大的,但海报数量只有10000,所以这10^7个数中真正用到的数很少,这样的话就只需要把没用到的数给"删去",剩下来的数从小到大映射为新的数,这样空间复杂度就大大降低了. 比如题目给的样例: 1 4 2 6 8 10 3 4 7 10   用到的数有:1 2 3 4 6 7 8 10 可以把它们映射为: 1 2 3 4 6 7

数据结构---线段树

线段树 转载请注明出处,谢谢!http://blog.csdn.net/metalseed/article/details/8039326  持续更新中···   一:线段树基本概念 1:概述 线段树,类似区间树,是一个完全二叉树,它在各个节点保存一条线段(数组中的一段子数组),主要用于高效解决连续区间的动态查询问题,由于二叉结构的特性,它基本能保持每个操作的复杂度为O(lgN)! 性质:父亲的区间是[a,b],(c=(a+b)/2)左儿子的区间是[a,c],右儿子的区间是[c+1,b],线段树