搞了半天终于过了
2023-07-17 14:47:33
发布于:广东
16阅读
0回复
0点赞
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int read()
{
int x=0;char ch=getchar();
while (ch<'0'||ch>'9') ch=getchar();
while ('0'<=ch&&ch<='9') x=(x<<1)+(x<<3)+ch-'0',ch=getchar();
return x;
}
const int M=1e5+5;
const ll inf=1ll<<60;
int A[M],n,m,tot,t,B[M*2]; ll ans[M],bit[M*2];
vector<int> vec1[M],vec2[M];
struct node{ll a,b,c,d;}p[M],q[M];
bool cmp(const node &A,const node &B) {return A.a<B.a;}
int lowbit(int x) {return x&(-x);}
void add1(int x,ll y){while (x<=tot) bit[x]=min(bit[x],y),x+=lowbit(x);}//前缀最小值
ll qry1(int x){ll res=inf;while (x) res=min(res,bit[x]),x-=lowbit(x);return res;}
void add2(int x,ll y){while (x) bit[x]=min(bit[x],y),x-=lowbit(x);}//后缀最小值
ll qry2(int x){ll res=inf;while (x<=tot) res=min(res,bit[x]),x+=lowbit(x);return res;}
void init(){for (int i=1;i<=tot;i++) bit[i]=inf;}
int main()
{
n=read();m=read();
for (int i=1;i<=n;i++)
p[i].a=read(),p[i].b=B[++tot]=read(),p[i].c=read(),A[i]=p[i].a;
sort(p+1,p+n+1,cmp);sort(A+1,A+n+1);//寻址数组
for (int i=1;i<=m;i++)
{
q[i].a=read(),q[i].b=B[++tot]=read();ans[i]=abs(q[i].a-q[i].b);
int k=upper_bound(A+1,A+n+1,q[i].a)-A-1;
vec1[k+1].push_back(i);
vec2[k].push_back(i);
}
sort(B+1,B+tot+1);tot=unique(B+1,B+tot+1)-B-1;
for (int i=1;i<=n;i++) p[i].d=lower_bound(B+1,B+tot+1,p[i].b)-B;
for (int i=1;i<=m;i++) q[i].d=lower_bound(B+1,B+tot+1,q[i].b)-B;
init();
for (int i=n;i>=1;i--)
{
add1(p[i].d,p[i].a-p[i].b+p[i].c);
for (int j=0;j<vec1[i].size();j++)
t=vec1[i][j],ans[t]=min(ans[t],qry1(q[t].d)-q[t].a+q[t].b);
}
init();
for (int i=n;i>=1;i--)
{
add2(p[i].d,p[i].a+p[i].b+p[i].c);
for (int j=0;j<vec1[i].size();j++)
t=vec1[i][j],ans[t]=min(ans[t],qry2(q[t].d)-q[t].a-q[t].b);
}
init();
for (int i=1;i<=n;i++)
{
add1(p[i].d,-p[i].a-p[i].b+p[i].c);
for (int j=0;j<vec2[i].size();j++)
t=vec2[i][j],ans[t]=min(ans[t],qry1(q[t].d)+q[t].a+q[t].b);
}
init();
for (int i=1;i<=n;i++)
{
add2(p[i].d,-p[i].a+p[i].b+p[i].c);
for (int j=0;j<vec2[i].size();j++)
t=vec2[i][j],ans[t]=min(ans[t],qry2(q[t].d)+q[t].a-q[t].b);
}
for (int i=1;i<=m;i++) printf("%lld\n",ans[i]);
return 0;
}
这里空空如也
有帮助,赞一个