线段树 BZOJ3888 [Usaco2015 Jan]Stampede
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了线段树 BZOJ3888 [Usaco2015 Jan]Stampede相关的知识,希望对你有一定的参考价值。
3888: [Usaco2015 Jan]Stampede
Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 253 Solved: 81
[Submit][Status][Discuss]
Description
Farmer John‘s N cows (1 <= N <= 50,000) appear to be stampeding along the road at the front of FJ‘s farm, but they are actually just running in a foot race to see which cow is the fastest. Viewed from above, each cow is represented by a unit-length horizontal line segment, specified by the coordinates of its left corner point at time t=0. For example, (-3,6) would specify a cow who at time t=0 is represented by the segment from (-3,6) to (-2,6). Each cow is moving to the right (in the +x direction) at a certain rate, specified by the integer amount of time it takes her to move 1 unit to the right. FJ is not particularly thrilled that his cows are outside running instead of in the barn producing milk. He plans to admonish them with a stern lecture after the race ends. In order to determine which of his cows are participating in the race, FJ situates himself at (0,0) and looks along a ray extending in the +y direction. As the race unfolds, FJ sees a cow if she is ever the first cow visible along this ray. That is, a cow might not be visible if another cow is "in front" of her during the entire time she crosses FJ‘s line of sight. Please compute the number of cows FJ can see during the entire race.
PoPoQQQ站在原点上向y轴正半轴看,然后有一群羊驼从他眼前飞过。这些羊驼初始都在第二象限,尾巴在(Xi,Yi),头在(Xi+1,Yi),每Ci秒向右走一个单位。
PoPoQQQ能看见一匹羊驼当且仅当它身体任意某部位x坐标为0时,没有其它y坐标小于此羊驼的羊驼身体某部位x坐标为0。
问PoPoQQQ能看见几匹羊驼?
Input
Output
Sample Input
-2 1 3
-3 2 3
-5 100 1
Sample Output
SOLUTION NOTES:
FJ can see cows 1 and 2 but not cow 3.
HINT
谨记:
粗心引起的错误,浪费掉数小时人生。——sdfzyny
这个题和poj一个叫做star的题好像,把题意看穿,紧握影响答案的因素……
说的什么屁话,路过大神不要嘲讽
某一时刻仅能看到1只羊驼,那么只要查询这个时刻有没有羊驼就好啦
首先自然想到按y坐标排序
然后可以先自己举几个样例,发现此题羊驼的y坐标影响并不是多效的……又说了句屁话QAQ
神奇的感觉到每个羊驼占(zhi)着(pei)观察者的眼睛的时间段似乎好、可以用线段树实现
根据题意把时间离散一下做,时间是个坏孩子……
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<map> 6 using namespace std; 7 int n,size,cnt,ans; 8 struct dt{ 9 int l,r,h; 10 }yt[60010]; 11 int f[200010]; 12 map<int,int>mp; 13 bool cmp(const dt&aa,const dt&bb){ 14 return aa.h<bb.h; 15 } 16 int segtree[400010]; 17 int ask(int pl,int pr,int pos,int ll,int rr){ 18 if(pl>rr||pr<ll) return 0; 19 if(segtree[pos]) return 0; 20 //if(pl==pr) return 0;手残!!! 21 if(pl<=ll&&pr>=rr){ 22 segtree[pos]=1; 23 return 1; 24 } 25 int mid=(ll+rr)>>1; 26 int p1=0,p2=0; 27 if(pl<=mid) p1=ask(pl,pr,pos<<1,ll,mid); 28 if(pr>mid) p2=ask(pl,pr,pos<<1|1,mid+1,rr); 29 segtree[pos]=segtree[pos]|(segtree[pos<<1]&segtree[pos<<1|1]); 30 return p1|p2; 31 } 32 int main(){ 33 scanf("%d",&n); 34 f[0]=-1; 35 int x,t; 36 for(int i=1;i<=n;i++){ 37 scanf("%d%d%d",&x,&yt[i].h,&t); 38 yt[i].l=(-x-1)*t; 39 yt[i].r=-x*t; 40 f[++cnt]=yt[i].l; 41 f[++cnt]=yt[i].r; 42 } 43 sort(yt+1,yt+n+1,cmp); 44 sort(f+1,f+cnt+1); 45 for(int i=1;i<=cnt;i++) 46 if(f[i]!=f[i-1]) mp[f[i]]=++size; 47 for(int i=1;i<=n;i++){ 48 yt[i].l=mp[yt[i].l]; 49 yt[i].r=mp[yt[i].r]-1; 50 } 51 for(int i=1;i<=n;i++) ans+=ask(yt[i].l,yt[i].r,1,1,size); 52 printf("%d",ans); 53 return 0; 54 }
以上是关于线段树 BZOJ3888 [Usaco2015 Jan]Stampede的主要内容,如果未能解决你的问题,请参考以下文章
[bzoj3939_Usaco2015 Feb]Cow Hopscotch(线段树维护DP)
BZOJ 3943 [Usaco2015 Feb]SuperBull:最大生成树
BZOJ1230 [Usaco2008 Nov]lites 开关灯 线段树
BZOJ 4756 [Usaco2017 Jan]Promotion Counting(线段树合并)