Codeforces 165D Buses and People(线段树)

转个文章 = =

http://codeforces.com/problemset/problem/160/E

题意 :

一条直线上有很多车站

有n辆车 每辆车的信息是 s,f,t  出发的站点,目的地,以及出发的时间,中途可以在其他站停留

m个乘客 每个乘客的信息是 start ,to ,tx 出发的站点 要到的站点 出发时间 

如果一个乘客能够做上某辆车必须满足s<=start,to<=f,tx<=t而且要满足车的出发时间越早越好。

输出时每个乘客应该坐上的车,如果没有输出-1

限制条件比较多,出发点 终点 时间都要题目满足要求,而且要在满足前两者的情况下,出发的尽可能早

首先很容易想到排序,把汽车和乘客的信息放在一起排

第一关键字 出发的站点 

第二关键字 id(乘客的id排在汽车后面)

这样的话如果汽车和乘客的出发站点是一样的,汽车会派到前面

这样一排之后,有一个好处是排在乘客后面的汽车肯定是出发站点比乘客大的 即s>start,所以乘客不会坐上后面的车走

然后离散化,对时间建树,每个节点保存区间内最右的车站。单点更新即可。

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define havemid int m=(l+r)>>1
#define left (rt<<1)
#define right (rt<<1|1)
#define clr(x) memset(x,0,sizeof(x))
const int maxn=250000;
int sum[maxn<<2];
int id[maxn<<2];
void pushup(int rt){
    sum[rt]=max(sum[left],sum[right]);
}
void update(int pos,int val,int number,int l,int r,int rt){
   if(l==r){
        sum[rt]=val;
        id[rt]=number;
        return ;
    }
    havemid;
    if(m>=pos) update(pos,val,number,lson);
    else update(pos,val,number,rson);
    pushup(rt);
}
int query(int pos,int val,int l,int r,int rt){
    if(sum[rt]<val)return -1;
    if(l==r)return id[rt];
    int res=-1;
    havemid;
    if(m>=pos){
        res=query(pos,val,lson);
        if(res>0) return res;
    }
    return query(pos,val,rson);
}
struct node{
    int l,r,ti,id;
}q[250000];
int cmp(struct node a,struct node b){
    if(a.l==b.l)return a.id<b.id;
    return a.l<b.l;
}
int t[250000];
int res[250000];
int n,m;
int main(){
    int i;
    while(scanf("%d%d",&n,&m)!=EOF){
        for(i=1;i<=n+m;i++){
            scanf("%d%d%d",&q[i].l,&q[i].r,&q[i].ti);
            q[i].id=i;
            t[i]=q[i].ti;
        }
        sort(q,q+n+m+1,cmp);
        sort(t,t+m+n+1);
        for(i=1;i<=m+n;i++){
            int pos=lower_bound(t+1,t+n+m+1,q[i].ti)-t;
            if(q[i].id<=n) update(pos,q[i].r,q[i].id,1,n+m,1);
            else res[q[i].id-n]=query(pos,q[i].r,1,n+m,1);
        }
     for(i=1;i<=m;i++)printf("%d ",res[i]);
    }
    return 0;
}


### Codeforces 1487D Problem Solution The problem described involves determining the maximum amount of a product that can be created from given quantities of ingredients under an idealized production process. For this specific case on Codeforces with problem number 1487D, while direct details about this exact question are not provided here, similar problems often involve resource allocation or limiting reagent type calculations. For instance, when faced with such constraints-based questions where multiple resources contribute to producing one unit of output but at different ratios, finding the bottleneck becomes crucial. In another context related to crafting items using various materials, it was determined that the formula `min(a[0],a[1],a[2]/2,a[3]/7,a[4]/4)` could represent how these limits interact[^1]. However, applying this directly without knowing specifics like what each array element represents in relation to the actual requirements for creating "philosophical stones" as mentioned would require adjustments based upon the precise conditions outlined within 1487D itself. To solve or discuss solutions effectively regarding Codeforces' challenge numbered 1487D: - Carefully read through all aspects presented by the contest organizers. - Identify which ingredient or component acts as the primary constraint towards achieving full capacity utilization. - Implement logic reflecting those relationships accurately; typically involving loops, conditionals, and possibly dynamic programming depending on complexity level required beyond simple minimum value determination across adjusted inputs. ```cpp #include <iostream> #include <vector> using namespace std; int main() { int n; cin >> n; vector<long long> a(n); for(int i=0;i<n;++i){ cin>>a[i]; } // Assuming indices correspond appropriately per problem statement's ratio requirement cout << min({a[0], a[1], a[2]/2LL, a[3]/7LL, a[4]/4LL}) << endl; } ``` --related questions-- 1. How does identifying bottlenecks help optimize algorithms solving constrained optimization problems? 2. What strategies should contestants adopt when translating mathematical formulas into code during competitive coding events? 3. Can you explain why understanding input-output relations is critical before implementing any algorithmic approach? 4. In what ways do prefix-suffix-middle frameworks enhance model training efficiency outside of just tokenization improvements? 5. Why might adjusting sample proportions specifically benefit models designed for tasks requiring both strong linguistic comprehension alongside logical reasoning skills?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值