【codeforces 160 E. Buses and People】 线段树

115 篇文章 0 订阅
71 篇文章 0 订阅

CF
题意 给你 n辆巴士的起始坐标 终止坐标 和启动时间
Q个人的起始坐标 终止坐标 和启动时间
只要满足人的起始坐标在巴士的起始坐标之后 并且终止坐标在巴士的终止坐标之前
并且时间在巴士的启动时间之前 人就可以坐上这巴士
问你每个人能坐上启动时间最早的巴士是哪一辆 坐不上就输出-1
做法 我们首先将所有人和巴士的坐标合在一起按照第一关键字起始坐标排序
第二关键字 id (这样能保证相同时间车一定在人前面
这样你问每个人的时候能保证之前处理过的巴士的起始时间都在这个人前面
然后我们怎么处理呢?
因为要时间最小 所以以时间为节点建立线段树 每个节点存的是该时间最大的 终止坐标
那么每个人都只要问他时间之后的有没有终止坐标大于他的最小的时间
用线段树查询就可以知道了
然后更新的时候就是往那个时间 插入终止坐标 和 巴士编号

/*
    if you can't see the repay
    Why not just work step by step
    rubbish is relaxed
    to ljq
*/
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#include <vector>
#include <stdlib.h>
#include <algorithm>
using namespace std;

#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define dbg2(x1,x2) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<endl
#define dbg3(x1,x2,x3) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<" "<<#x3<<" = "<<x3<<endl
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define lc (rt<<1)
#define rc (rt<<11)
#define mid ((l+r)>>1)

typedef pair<int,int> pll;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll _INF = 0xc0c0c0c0c0c0c0c0;
const ll mod =  (int)1e9+7;

ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll ksm(ll a,ll b,ll mod){int ans=1;while(b){if(b&1) ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
ll inv2(ll a,ll mod){return ksm(a,mod-2,mod);}
void exgcd(ll a,ll b,ll &x,ll &y,ll &d){if(!b) {d = a;x = 1;y=0;}else{exgcd(b,a%b,y,x,d);y-=x*(a/b);}}//printf("%lld*a + %lld*b = %lld\n", x, y, d);
const int MAX_N = 200025;
int b[MAX_N<<1],ans[MAX_N],tm[MAX_N],maxx[MAX_N<<2],id[MAX_N<<2];
int Max(int a,int b){if(a>b) return a;return b;}
struct node
{
    int l,r,tm,id;
    bool operator<(const node other) const
    {
        if(l==other.l) return id < other.id;
        return l < other.l;
    }
}arr[MAX_N<<1];
void up(int rt)
{
    maxx[rt] = Max(maxx[rt<<1],maxx[rt<<1|1]);
}
void build(int rt,int l,int r)
{
    maxx[rt] = 0;
    if(l==r)
    {
        maxx[rt] = -inf;
        return ;
    }
    build(rt<<1,l,mid);
    build(rt<<1|1,mid+1,r);
    up(rt);
}
void update(int rt,int l,int r,int x,int v,int ID)
{
    if(l==r)
    {
        maxx[rt] = v;
        id[rt] = ID;
        return ;
    }
    if(x<=mid) update(rt<<1,l,mid,x,v,ID);
    else update(rt<<1|1,mid+1,r,x,v,ID);
    up(rt);
}
int query(int rt,int l,int r,int x,int y,int v)
{
    if(maxx[rt]<v)
    {
        return -1;
    }
    if(l==r)
    {
        return id[rt];
    }
    int ans = -1;
    if(maxx[rt<<1]>=v&&x<=mid)
    {
        ans = query(rt<<1,l,mid,x,y,v);
        if(ans!=-1) return ans;
    }
    return query(rt<<1|1,mid+1,r,x,y,v);
}
int main()
{
    //ios::sync_with_stdio(false);
    //freopen("a.txt","r",stdin);
    //freopen("b.txt","w",stdout);
    int n,Q,cnt = 0,cnt_ = 0;
    scanf("%d%d",&n,&Q);
    for(int i = 1;i<=n;++i)
    {
        scanf("%d%d%d",&arr[i].l,&arr[i].r,&arr[i].tm);
        arr[i].id = i;tm[++cnt_] = arr[i].tm;
        b[++cnt] = arr[i].l,b[++cnt] = arr[i].r;
    }
    for(int i = n+1;i<=n+Q;++i)
    {
        scanf("%d%d%d",&arr[i].l,&arr[i].r,&arr[i].tm);
        arr[i].id = i;tm[++cnt_] = arr[i].tm;
        b[++cnt] = arr[i].l,b[++cnt] = arr[i].r;
    }
    sort(b+1,b+1+cnt);
    sort(tm+1,tm+1+cnt_);
    int sz = unique(b+1,b+1+cnt)-b-1;
    int sz_ = unique(tm+1,tm+1+cnt_) - tm - 1;
    build(1,1,n+Q);
    for(int i = 1;i<=n+Q;i++)
    {
        arr[i].tm = lower_bound(tm+1,tm+1+sz_,arr[i].tm) - tm;
        arr[i].l = lower_bound(b+1,b+1+sz,arr[i].l) - b;
        arr[i].r = lower_bound(b+1,b+1+sz,arr[i].r) - b;
    }
    sort(arr+1,arr+1+n+Q);
    for(int i = 1;i<=n+Q;i++)
    {
        if(arr[i].id <=n)
        {
            update(1,1,n+Q,arr[i].tm,arr[i].r,arr[i].id);
        }
        else
        {
            //dbg3(i,arr[i].id,query(1,1,n+Q,arr[i].tm,n+Q,arr[i].r));
            ans[arr[i].id] = query(1,1,n+Q,arr[i].tm,n+Q,arr[i].r);
        }
    }
    for(int i = n+1;i<=n+Q;++i)
        printf("%d ",ans[i]);
    //fclose(stdin);
    //fclose(stdout);
    //cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值