记录编号 |
575247 |
评测结果 |
AAAAAAAAAA |
题目名称 |
双倍腹肌量 |
最终得分 |
100 |
用户昵称 |
Skloud |
是否通过 |
通过 |
代码语言 |
C++ |
运行时间 |
0.882 s |
提交时间 |
2022-09-07 21:30:43 |
内存使用 |
7.56 MiB |
显示代码纯文本
#include <bits/stdc++.h>
using namespace std;
const int N=100000+5;
const int inf=0x7fffffff;
int n,m;
struct xcv{
int x,y;
}p[N];
struct sdf{
int l,r,mn,mx;
}tr[4*N];
bool cmp(xcv a,xcv b){
return a.x<b.x;
}
void update(int pt){
tr[pt].mn=min(tr[pt*2].mn,tr[pt*2+1].mn);
tr[pt].mx=max(tr[pt*2].mx,tr[pt*2+1].mx);
return ;
}
void build(int pt,int l,int r){
tr[pt]={l,r,inf,0};
if (l==r){
tr[pt].mn=tr[pt].mx=p[l].y;return ;
}
int mid=(l+r)/2;
build(pt*2,l,mid);build(pt*2+1,mid+1,r);
update(pt);
return ;
}
struct wer{
int mn,mx;
};
wer ask(int pt,int l,int r){
if (l>r)return {0,0};
if (l<=tr[pt].l&&tr[pt].r<=r)return {tr[pt].mn,tr[pt].mx};
int mid=(tr[pt].l+tr[pt].r)/2;
wer now={inf,0},tmp;
if (l<=mid){
tmp=ask(pt*2,l,r);
now.mn=min(now.mn,tmp.mn);now.mx=max(now.mx,tmp.mx);
}
if (mid+1<=r){
tmp=ask(pt*2+1,l,r);
now.mn=min(now.mn,tmp.mn);now.mx=max(now.mx,tmp.mx);
}
return now;
}
int main(){
freopen ("double_muscle.in","r",stdin);
freopen ("double_muscle.out","w",stdout);
scanf("%d%d",&n,&m);
for (int i=1;i<=n;i++){
scanf("%d%d",&p[i].x,&p[i].y);
}
sort(p+1,p+n+1,cmp);
build(1,1,n);
int l=1,r=0,ans=inf;wer now;
while(l<=n){
now=ask(1,l,r);
if (now.mx-now.mn<m&&r!=n){
r++;
}
else{
if (now.mx-now.mn>=m)ans=min(ans,p[r].x-p[l].x);
else if (r==n)break;
l++;
}
}
if (ans==inf)printf("-1\n");
else printf("%d\n",ans);
return 0;
}