(3888: [Usaco2015 Jan]Stampede)<离散化+线段树>

本文介绍了一种使用线段树数据结构解决特定赛牛问题的方法。该问题要求确定在赛牛过程中,哪些牛可以在特定视角下被观察者看到。通过线段树的有效区间查询与更新操作,实现了高效求解。

题目

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.

题解

  • 建立一颗关于时间的线段树
  • 把每一只羊驼的首和尾通过y轴的时间,同时离散化
  • 按羊驼的高度排序,分别把每一只的时间作为区间加入线段树
    如果这段区间已被覆盖,则返回0
    如果为未覆盖,则返回1,同时将区间覆盖

代码

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<map>
using namespace std;

const int M=50010;
int n;
struct cow{
    int L,R,h;
}a[M];
int f[M*2],s;
int ans;
map<int,int> m;

bool cmp(cow a,cow b){
    return a.h<b.h;
}

struct node{
    int L,R;
    bool vis;
}t[M*8+5];//

void build(int pos,int L,int R){
    t[pos].L=L;
    t[pos].R=R;
    t[pos].vis=0;
    if(L==R) return;
    int mid=(L+R)>>1;
    build(pos<<1,L,mid);
    build(pos<<1|1,mid+1,R);
}

int query(int pos,int ll,int rr){
    if(t[pos].vis) return 0;
    int L=t[pos].L;
    int R=t[pos].R;
    if(ll<=L&&rr>=R){
        t[pos].vis=1;
        return 1;
    }
    if(L==R) return 0;
    int mid=(L+R)>>1,res=0;
    if(rr<=mid) res=query(pos<<1,ll,rr);
    else if(ll>mid) res=query(pos<<1|1,ll,rr);
        else res=query(pos<<1,ll,rr)|query(pos<<1|1,ll,rr);
    t[pos].vis=t[pos].vis|(t[pos<<1].vis&t[pos<<1|1].vis);
    return res;
}

int main(){
    f[0]=-1;
    scanf("%d",&n);
    int x,y,c,cnt=0;
    for(int i=1;i<=n;++i){
        scanf("%d%d%d",&x,&y,&c);
        a[i].h=y;
        a[i].L=(-x-1)*c;
        a[i].R=(-x)*c;//
        f[++cnt]=a[i].L;
        f[++cnt]=a[i].R;
    }
    sort(a+1,a+1+n,cmp);
    sort(f+1,f+1+cnt);
    for(int i=1;i<=cnt;++i)
        if(f[i]!=f[i-1])
            m[f[i]]=++s;
    for(int i=1;i<=n;++i){
        a[i].L=m[a[i].L];
        a[i].R=m[a[i].R]-1;
    }
    build(1,1,s);
    for(int i=1;i<=n;++i) ans+=query(1,a[i].L,a[i].R);
    cout<<ans;
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值