Record day to see (copy) solution to a problem of daily;
https://www.luogu.org/problem/P2698
We can coordinate x according to the order of increasing row number order, so we left the ordinate;
If the abscissa (l, r) interval, the maximum value minus the minimum ordinate is greater than d, then the answer can be updated;
As seen growth l, r must be incremented;
It may prove absent (l2, r2), l2> l1 and r2 <r, (maxy-miny)> d, and can affect the answers;
Because if this exists, then r2 l should be first matching objects, the answer is better;
This involves a sliding window method;
Ql with [] the recording position of the maximum vertical coordinate, q2 [] the recording position of the minimum vertical coordinate;
In the range of l to R & lt;
Update the queue interval range:
while(h1<=t1&&q1[h1]<l) h1++;
while(h2<=t2&&q2[h2]<l) h2++;
Updating r, maximum, minimum;
while(a[q1[h1]].y-a[q2[h2]].y<d&&r<n) { ++r; while(a[q1[t1]].y<a[r].y&&h1<=t1) t1--; q1[++t1]=r; while(a[q2[t2]].y>a[r].y&&h2<=t2) t2--; q2[++t2]=r; }
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int maxn=1e6+10; int q1[maxn],q2[maxn],h1=1,h2=1,t1,t2; int n,d; struct node { int x,y; }a[maxn]; bool cmp(node qw,node we) { return qw.x<we.x; } int ans=2147483647; int main() { scanf("%d%d",&n,&d); for(int i=1;i<=n;i++) { scanf("%d%d",&a[i].x,&a[i].y); } sort(a+1,a+n+1,cmp); for(int l=1,r=0;l<=n;l++) { while(h1<=t1&&q1[h1]<l) h1++; while(h2<=t2&&q2[h2]<l) h2++; while(a[q1[h1]].y-a[q2[h2]].y<d&&r<n) { ++r; while(a[q1[t1]].y<a[r].y&&h1<=t1) t1--; q1[++t1]=r; while(a[q2[t2]].y>a[r].y&&h2<=t2) t2--; q2[++t2]=r; } if(a[q1[h1]].y-a[q2[h2]].y>=d) ans=min(ans,a[r].x-a[l].x); } if(ans==2147483647) printf("-1"); else printf("%d",ans); return 0; }