Solución: CF 1409 E \ mathrm {CF1409E}C F 1 4 0 9 E
Significado del tema
S ol \ mathrm {Sol} S o l
¿Es cierto que utilizo estructuras de datos para este problema? ? !
Primero discretizamos las coordenadas y luego calculamos iii al comienzo de la plataforma[i, i + k] [[ yo ,yo+k ] ¿ Cuántas bolas pequeñas pueden caer se registran comosi s_isyo. Somos si s_isyo Construya un árbol de segmento de línea para mantener el intervalo máximo.
Entonces enumeramos el punto de partida de la primera plataforma st sts t , entonces la contribución esta vez essst + max j = st + 1 n (sj) s_ {st} + \ max \ limits_ {j = st + 1} ^ {n} (s_j)ss t+j = s t + 1maxn( sj) , Simplemente calcule el árbol del segmento de línea directamente.
Complejidad de tiempo O (n log n) O (n \ log n)O ( nlo gn )
C ode \ mathrm {Código} C o d e
int n,m,Q,x[N],y[N],a[N],gs[N],pre[N],s[N],mx[N<<2],ans;
inline void build(int x,int l,int r)
{
if(l==r)
{
mx[x]=s[l];
return;
}
int mid=l+r>>1;
build(x<<1,l,mid);
build(x<<1|1,mid+1,r);
mx[x]=max(mx[x<<1],mx[x<<1|1]);
}
inline int ask(int x,int l,int r,int ll,int rr)
{
if(ll>r||rr<l) return 0;
if(ll<=l&&r<=rr) return mx[x];
int mid=l+r>>1,ans=0;
ans=max(ask(x<<1,l,mid,ll,rr),ask(x<<1|1,mid+1,r,ll,rr));
return ans;
}
int main()
{
io.read(Q);
for (;Q--;)
{
io.read(n),io.read(m);
For(i,0,n) gs[i]=0,pre[i]=s[i]=0;
For(i,1,n) io.read(x[i]),a[i]=x[i];
For(i,1,n) io.read(y[i]);
sort(a+1,a+n+1);
int tot=unique(a+1,a+n+1)-a-1;
For(i,1,n) gs[lower_bound(a+1,a+tot+1,x[i])-a]++;
For(i,1,n) pre[i]=pre[i-1]+gs[i];
For(i,1,n) s[i]=pre[upper_bound(a+i+1,a+tot+1,a[i]+m)-a-1]-pre[i-1];
build(1,1,tot);
For(i,1,tot) ans=max(ans,s[i]+ask(1,1,tot,upper_bound(a+i+1,a+tot+1,a[i]+m)-a,tot));
io.write(ans),puts(""),ans=0;
}
return 0;
}