线段树 BZOJ3888 [Usaco2015 Jan]Stampede

3888: [Usaco2015 Jan]Stampede

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 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

The first line of the input contains N. Each of the following N lines describes a cow with three integers x y r, corresponding to a cow whose left endpoint is at (x,y) at time t=0, moving to the right at a continuous speed of 1 unit of distance every r units of time. The value of x is in the range -1000..-1, the value of y is in the range 1..1,000,000 (and distinct for every cow, to prevent any possible collisions), and the value of r is in the range 1..1,000,000.

Output

A single integer, specifying the number of cows FJ can see during the entire race (from t=0 onward).

Sample Input

3

-2 1 3

-3 2 3

-5 100 1

Sample Output

2

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 }
时间: 2024-08-03 00:22:23

线段树 BZOJ3888 [Usaco2015 Jan]Stampede的相关文章

BZOJ3888 [Usaco2015 Jan]Stampede

我们只要把每头牛开始遮挡视线和结束遮挡视线的时间点都搞出来就好= = 再按照y轴排序...然后变成线段覆盖了..线段树搞一下就好了? 1 /************************************************************** 2 Problem: 3888 3 User: rausen 4 Language: C++ 5 Result: Accepted 6 Time:1204 ms 7 Memory:22684 kb 8 ****************

[BZOJ 3888] [Usaco2015 Jan] Stampede 【线段树】

题目链接:BZOJ - 3888 题目分析 首先,计算出每个线段在 x 坐标 0 处出现的时间开始点和结束点,就转成了时间轴上的线段. 然后就是看每条线段是否被 y 比它小的线段完全覆盖了.注意求出的时间点要离散化,然后应该使用时间轴上的区间来表示,两线段端点重合并不是有共同部分. 将所有线段按照 y 从小到大排序之后,使用线段树判断它覆盖的区间是否已经都被前面的线段覆盖了. 然后将它所覆盖的区间覆盖. 就这样的一道题我WA了7次,还有救吗.. 代码 #include <iostream> #

BZOJ 1699 [Usaco2007 Jan]Balanced Lineup排队 线段树

题意:链接 方法:线段树 解析: 题意即题解. 多次询问区间最大值与最小值的差,显然直接上线段树或者rmq维护区间最值即可. 代码: #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define N 50010 #define lson l,mid,rt<<1 #define rson mid+1,r,rt<<1|1 #def

【bzoj3939】[Usaco2015 Feb]Cow Hopscotch 动态开点线段树优化dp

题目描述 Just like humans enjoy playing the game of Hopscotch, Farmer John's cows have invented a variant of the game for themselves to play. Being played by clumsy animals weighing nearly a ton, Cow Hopscotch almost always ends in disaster, but this has

Bzoj 3050: [Usaco2013 Jan]Seating(线段树裸题,然而区间修改标记下放和讨论Push_up很揪心)

题目链接 题意:开始有一个空白的区间,每次可能进行两个操作:A 将一个长度为p的区间加入一段连续空白的位置 L:一个区间恢复空白:要求出A不能进行的次数. 非常裸的线段树题目,用线段树统计最大的空白区间,每个节点需要记录当前区间的最长空白区间,从左端开始的最长空白区间,从右端开始的最长空白区间.Push_up的时候要讨论下,可以分别取[l,mid]和[mid+1,r]的最大空白区间,也可以用[l,mid]的从右端开始的最长空白区间+[mid+1,r]从左端开始的最大空白区间. 每次A的时候,就查

BZOJ 4756 [Usaco2017 Jan]Promotion Counting(线段树合并)

[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=4756 [题目大意] 给出一棵树,对于每个节点,求其子树中比父节点大的点个数 [题解] 我们考虑每个权值建立一棵线段树,边dfs边将子节点合并为一颗线段树, 那么只要查询当前点的树上后缀和即可. [代码] #include <cstdio> #include <algorithm> #include <cstring> #include <vector&

bzoj 1594: [Usaco2008 Jan]猜数游戏——二分+线段树

Description 为了提高自己低得可怜的智商,奶牛们设计了一个新的猜数游戏,来锻炼她们的逻辑推理能力. 游戏开始前,一头指定的奶牛会在牛棚后面摆N(1 <= N<= 1,000,000)堆干草,每堆有若干捆,并且没有哪两堆中的草一样多.所有草堆排成一条直线,从左到右依次按1..N编号,每堆中草的捆数在1..1,000,000,000之间. 然后,游戏开始.另一头参与游戏的奶牛会问那头摆干草的奶牛 Q(1 <= Q <= 25,000)个问题,问题的格式如下: 编号为Ql..Q

hdu 4122 Alice&amp;#39;s mooncake shop (线段树)

题目大意: 一个月饼店每一个小时做出月饼的花费不一样. 储存起来要钱.最多存多久.问你把全部订单做完的最少花费. 思路分析: ans = segma( num[]*(cost[] + (i-j)*s) ) 整理一下会发现式子就是 cost[]-j*s + i*s 对于每个订单,我们把i拿出来分析 所以也就用cost - j*s 建树. 然后在储存期间找到最小的花费即可了. #include <cstdio> #include <iostream> #include <algo

hdu 4122 Alice&#39;s mooncake shop (线段树)

题目大意: 一个月饼店每个小时做出月饼的花费不一样. 储存起来要钱,最多存多久.问你把所有订单做完的最少花费. 思路分析: ans = segma( num[]*(cost[] + (i-j)*s) ) 整理一下会发现式子就是 cost[]-j*s + i*s 对于每一个订单,我们把i拿出来分析 所以也就用cost - j*s 建树. 然后在储存期间找到最小的花费就行了. #include <cstdio> #include <iostream> #include <algo