bzoj5191 [Usaco2018 Feb]Slingshot(离线+树状数组)

对于a,b答案就是
minni=1|axi|+|byi|+ti
根据a与x,b与y的大小关系分四类讨论。
比如第一类: xi<=a,yi<=b
答案就是 a+bxiyi+ti ,设
vi=xiyi+ti ,于是乎我们在满足条件的i中挑一个 vi 最小的即可。可以排序+树状数组求前缀/后缀最小值解决。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
#define ll long long
#define inf 1LL<<60
#define N 100010
inline char gc(){
    static char buf[1<<16],*S,*T;
    if(S==T){T=(S=buf)+fread(buf,1,1<<16,stdin);if(T==S) return EOF;}
    return *S++;
}
inline int read(){
    int x=0,f=1;char ch=gc();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=gc();}
    while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=gc();
    return x*f;
}
int n,m,aa[N<<1],tot=0;ll c[N<<1],ans[N];
struct pile{
    int id,a,b,bb;
}a[N];
inline bool cmp1(pile a,pile b){return a.a<b.a;}
struct slingshot{
    int x,y,t,yy;
}b[N];
inline bool cmp2(slingshot a,slingshot b){return a.x<b.x;}
inline void add(int x,ll val){
    for(;x<=tot;x+=x&-x) c[x]=min(c[x],val);
}
inline ll ask(int x){
    ll res=inf;for(;x;x-=x&-x) res=min(res,c[x]);return res;
}
int main(){
//  freopen("slingshot.in","r",stdin);
//  freopen("slingshot.out","w",stdout);
    n=read();m=read();
    for(int i=1;i<=n;++i) b[i].x=read(),aa[++tot]=b[i].y=read(),b[i].t=read();
    for(int i=1;i<=m;++i) a[i].a=read(),aa[++tot]=a[i].b=read(),a[i].id=i,ans[i]=abs(a[i].b-a[i].a);
    sort(aa+1,aa+tot+1);tot=unique(aa+1,aa+tot+1)-aa-1;
    for(int i=1;i<=n;++i) b[i].yy=lower_bound(aa+1,aa+tot+1,b[i].y)-aa;
    for(int i=1;i<=m;++i) a[i].bb=lower_bound(aa+1,aa+tot+1,a[i].b)-aa;
    sort(a+1,a+m+1,cmp1);sort(b+1,b+n+1,cmp2);
    int now=1;for(int i=1;i<=tot;++i) c[i]=inf;
    for(int i=1;i<=m;++i){
        while(now<=n&&b[now].x<=a[i].a) add(b[now].yy,(ll)b[now].t-b[now].x-b[now].y),++now;
        ans[a[i].id]=min(ans[a[i].id],(ll)a[i].a+a[i].b+ask(a[i].bb));
    }now=1;for(int i=1;i<=tot;++i) c[i]=inf;
    for(int i=1;i<=m;++i){
        while(now<=n&&b[now].x<=a[i].a) add(tot-b[now].yy+1,(ll)b[now].t-b[now].x+b[now].y),++now;
        ans[a[i].id]=min(ans[a[i].id],(ll)a[i].a-a[i].b+ask(tot-a[i].bb+1));
    }now=n;for(int i=1;i<=tot;++i) c[i]=inf;
    for(int i=m;i>=1;--i){
        while(now&&b[now].x>=a[i].a) add(b[now].yy,(ll)b[now].t+b[now].x-b[now].y),--now;
        ans[a[i].id]=min(ans[a[i].id],(ll)-a[i].a+a[i].b+ask(a[i].bb));
    }now=n;for(int i=1;i<=tot;++i) c[i]=inf;
    for(int i=m;i>=1;--i){
        while(now&&b[now].x>=a[i].a) add(tot-b[now].yy+1,(ll)b[now].t+b[now].x+b[now].y),--now;
        ans[a[i].id]=min(ans[a[i].id],(ll)-a[i].a-a[i].b+ask(tot-a[i].bb+1));
    }for(int i=1;i<=m;++i) printf("%lld\n",ans[i]);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值