Language: C++

#include<bits/stdc++.h>
#define N 100005
using namespace std;
inline int read(){
	int ans=0;
	char ch=getchar();
	while(!isdigit(ch))ch=getchar();
	while(isdigit(ch))ans=(ans<<3)+(ans<<1)+(ch^48),ch=getchar();
	return ans;
}
int l,n,x[N],Q,tim[N],next[N],hd,tl,q[N],blo[N],sig;
int main(){
	n=read(),sig=sqrt(n);
	for(int i=1;i<=n;++i)x[i]=read();
	l=read(),Q=read();
	hd=tl=1,q[tl]=n,tim[n]=0,next[n]=n+1;
	for(int i=n-1;i;--i){
		while(x[q[hd]]-x[i]>l)++hd;
		next[i]=q[hd],q[++tl]=i;
	}
	for(int i=1;i<=n;++i)blo[i]=(i-1)/sig+1;
	for(int i=n-1;i;--i)
		if(blo[next[i]]==blo[i])tim[i]=x[i]+l>=x[n]?1:tim[next[i]]+1,next[i]=next[next[i]];
		else tim[i]=1;
	while(Q--){
		int a=read(),b=read(),cnt=0;
		if(a>b)a^=b,b^=a,a^=b;
		while(blo[a]<blo[b])cnt+=tim[a],a=next[a];
		int sum=0;
		for(int i=a+1;i<=b;++i){
			sum+=x[i]-x[i-1];
			if(sum>=l)sum=0,++cnt;
		}
		printf("%d\n",cnt);
	}
	return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:16:34: error: reference to 'next' is ambiguous
   16 |         hd=tl=1,q[tl]=n,tim[n]=0,next[n]=n+1;
      |                                  ^~~~
In file included from /usr/include/c++/14/bits/stl_algobase.h:66,
                 from /usr/include/c++/14/algorithm:60,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:11:23: note:                 'int next [100005]'
   11 | int l,n,x[N],Q,tim[N],next[N],hd,tl,q[N],blo[N],sig;
      |                       ^~~~
a.cc:19:17: error: reference to 'next' is ambiguous
   19 |                 next[i]=q[hd],q[++tl]=i;
      |                 ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:11:23: note:                 'int next [100005]'
   11 | int l,n,x[N],Q,tim[N],next[N],hd,tl,q[N],blo[N],sig;
      |                       ^~~~
a.cc:23:24: error: reference to 'next' is ambiguous
   23 |                 if(blo[next[i]]==blo[i])tim[i]=x[i]+l>=x[n]?1:tim[next[i]]+1,next[i]=next[next[i]];
      |                        ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:11:23: note:                 'int next [100005]'
   11 | int l,n,x[N],Q,tim[N],next[N],hd,tl,q[N],blo[N],sig;
      |                       ^~~~
a.cc:23:67: error: reference to 'next' is ambiguous
   23 |                 if(blo[next[i]]==blo[i])tim[i]=x[i]+l>=x[n]?1:tim[next[i]]+1,next[i]=next[next[i]];
      |                                                                   ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:11:23: note:                 'int next [100005]'
   11 | int l,n,x[N],Q,tim[N],next[N],hd,tl,q[N],blo[N],sig;
      |                       ^~~~
a.cc:23:78: error: reference to 'next' is ambiguous
   23 |                 if(blo[next[i]]==blo[i])tim[i]=x[i]+l>=x[n]?1:tim[next[i]]+1,next[i]=next[next[i]];
      |                                                                              ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:11:23: note:                 'int next [100005]'
   11 | int l,n,x[N],Q,tim[N],next[N],hd,tl,q[N],blo[N],sig;
      |                       ^~~~
a.cc:23:86: error: reference to 'next' is ambiguous
   23 |                 if(blo[next[i]]==blo[i])tim[i]=x[i]+l>=x[n]?1:tim[next[i]]+1,next[i]=next[next[i]];
      |                                                                                      ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:11:23: note:                 'int next [100005]'
   11 | int l,n,x[N],Q,tim[N],next[N],hd,tl,q[N],blo[N],sig;
      |                       ^~~~
a.cc:23:91: error: reference to 'next' is ambiguous
   23 |                 if(blo[next[i]]==blo[i])tim[i]=x[i]+l>=x[n]?1:tim[next[i]]+1,next[i]=next[next[i]];
      |                                                                                           ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:11:23: note:                 'int next [100005]'
   11 | int l,n,x[N],Q,tim[N],next[N],hd,tl,q[N],blo[N],sig;
      |                       ^~~~
a.cc:28:51: error: reference to 'next' is ambiguous
   28 |                 while(blo[a]<blo[b])cnt+=tim[a],a=next[a];
      |                                                   ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:11:23: note:                 'int next [100005]'
   11 | int l,n,x[N],Q,tim[N],next[N],hd,tl,q[N],blo[N],sig;
      |                       ^~~~