首页 > 其他 > 详细

数据结构(线段树):BZOJ 3126: [Usaco2013 Open]Photo

时间:2016-04-15 21:31:05      阅读:324      评论:0      收藏:0      [点我收藏+]

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 }

 

数据结构(线段树):BZOJ 3126: [Usaco2013 Open]Photo

原文:http://www.cnblogs.com/TenderRun/p/5396878.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!