Codeforces 522D Closest Equals

时间:2021-09-16 15:57:38

题解:

傻逼题

直接从左向右扫描每个点作为右端点

然后单点修改区间查询就行了

另外一种更直观的做法就是$(i,j)$之间产生了$(j-i)$

于是变成矩形查最大值,kd-tree维护

代码:

#include <bits/stdc++.h>
#define rint register int
#define IL inline
#define rep(i,h,t) for (int i=h;i<=t;i++)
#define dep(i,t,h) for (int i=t;i>=h;i--)
#define me(x) memset(x,0,sizeof(x))
#define ll long long
namespace IO{
char ss[<<],*A=ss,*B=ss;
IL char gc()
{
return A==B&&(B=(A=ss)+fread(ss,,<<,stdin),A==B)?EOF:*A++;
}
template<class T>void read(T &x)
{
rint f=,c; while (c=gc(),c<||c>) if (c=='-') f=-; x=(c^);
while (c=gc(),c>&&c<) x=(x<<)+(x<<)+(c^); x*=f;
}
char sr[<<],z[]; int Z,C=-;
template<class T>void wer(T x)
{
if (x<) sr[++C]='-',x=-x;
while (z[++Z]=x%+,x/=);
while (sr[++C]=z[Z],--Z);
}
IL void wer1() {sr[++C]=' ';}
IL void wer2() {sr[++C]='\n';}
template<class T>IL void maxa(T &x,T y) {if (x<y) x=y;}
template<class T>IL void mina(T &x,T y) {if (x>y) x=y;}
template<class T>IL T MAX(T x,T y) { return x>y?x:y;}
template<class T>IL T MIN(T x,T y) { return x<y?x:y;}
};
using namespace std;
using namespace IO;
const int N=6e5;
map<int,int> M;
int cnt,a[N],cmp_d,rt;
struct re{
int x,y,z;
}p[N];
bool cmp(re x,re y)
{
if (!cmp_d) return x.x<y.x;
else return x.y<y.y;
}
const int INF=1e9;
struct kd_tree{
int v[N],pv[N],ls[N],rs[N],Mx[N],Nx[N],My[N],Ny[N];
kd_tree()
{
v[]=pv[]=INF;
Nx[]=INF; Mx[]=;
Ny[]=INF; My[]=;
}
IL void updata(int x)
{
pv[x]=MIN(MIN(pv[ls[x]],pv[rs[x]]),v[x]);
Mx[x]=MAX(p[x].x,MAX(Mx[ls[x]],Mx[rs[x]]));
Nx[x]=MIN(p[x].x,MIN(Nx[ls[x]],Nx[rs[x]]));
My[x]=MAX(p[x].y,MAX(My[ls[x]],My[rs[x]]));
Ny[x]=MIN(p[x].y,MIN(Ny[ls[x]],Ny[rs[x]]));
}
int build(int h,int t,int o)
{
int x,mid; x=mid=(h+t)/;
cmp_d=o; nth_element(p+h,p+mid,p+t+,cmp);
v[x]=p[x].z;
if (h<mid) ls[x]=build(h,mid-,o^);
if (mid<t) rs[x]=build(mid+,t,o^);
updata(x);
return x;
}
int query(int x,int x1,int x2,int y1,int y2)
{
if (x1>Mx[x]||x2<Nx[x]||y1>My[x]||y2<Ny[x]) return(INF);
if (x1<=Nx[x]&&Mx[x]<=x2&&y1<=Ny[x]&&My[x]<=y2) return(pv[x]);
int ans=INF;
if (p[x].x>=x1&&p[x].x<=x2&&p[x].y>=y1&&p[x].y<=y2) ans=v[x];
mina(ans,query(ls[x],x1,x2,y1,y2));
mina(ans,query(rs[x],x1,x2,y1,y2));
return ans;
}
}K;
int main()
{
freopen("1.in","r",stdin);
freopen("1.out","w",stdout);
int n,m;
read(n); read(m);
rep(i,,n)
{
read(a[i]);
int k=M[a[i]];
if (k) p[++cnt]=(re){k,i,i-k};
M[a[i]]=i;
}
rt=K.build(,cnt,);
rep(i,,m)
{
int x,y;
read(x); read(y);
int ans=K.query(rt,x,y,x,y);
if (ans<INF) wer(ans); else wer(-);
wer2();
}
fwrite(sr,,C+,stdout);
return ;
}