Language: C++


#include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>

using namespace std;
using ll = long long;

ll n;
ll x[123456];//n
ll l;
ll q;
ll a[123456], b[123456];//q

ll lbar[123456];//n
ll rbar[123456];//n

vector<ll> lgraph[123456];

ll dist[123456];
int bfs(int v1, int v2)
{
	queue<ll> que;
	memset(dist, 0xFF, sizeof(ll) * n);
	que.push(v1);
	while (!que.empty())
	{
		ll vn = que.front(); que.pop();
		for (ll vx : lgraph[vn])
		{
			if (vx == v2)return dist[vn] + 1;
			if (dist[vx] == -1)
			{
				dist[vx] = dist[vn] + 1;
				que.push(vx);
			}
		}
	}
	return 0;
}

int main()
{
	scanf("%lld",&n);
	for (int i = 0; i < n; ++i)scanf("%lld", x + i);
	scanf("%lld %lld", &l, &q);
	for (int j = 0; j < q; ++j)
	{
		scanf("%lld %lld", a + j, b + j);
		--a[j]; --b[j];
	}
	for (int i = 0; i < n; ++i)
	{
		lbar[i] = lower_bound(x, x + i + 1, x[i] - l) - x;
		rbar[i] = max(upper_bound(x + i, x + n, x[i] + l) - x - 1, i);
	}
	for (int i = 0; i < n; ++i)
	{
		for (int vert = lbar[i]; vert < i;++vert)
		{
			lgraph[vert].push_back(i);
			lgraph[i].push_back(vert);
		}
	}
	for (int j = 0; j < q; ++j)
	{
		ll ans = bfs(a[j], b[j]);
		printf("%lld\n",ans);
	}


	return 0;
}

Compiler output:
a.cc: In function 'int bfs(int, int)':
a.cc:25:9: error: 'memset' was not declared in this scope
   25 |         memset(dist, 0xFF, sizeof(ll) * n);
      |         ^~~~~~
a.cc:6:1: note: 'memset' is defined in header '<cstring>'; this is probably fixable by adding '#include <cstring>'
    5 | #include <queue>
  +++ |+#include <cstring>
    6 | 
a.cc: In function 'int main()':
a.cc:56:30: error: no matching function for call to 'max(long int, int&)'
   56 |                 rbar[i] = max(upper_bound(x + i, x + n, x[i] + l) - x - 1, i);
      |                           ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/algorithm:60,
                 from a.cc:3:
/usr/include/c++/14/bits/stl_algobase.h:257:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  257 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note:   template argument deduction/substitution failed:
a.cc:56:30: note:   deduced conflicting types for parameter 'const _Tp' ('long int' and 'int')
   56 |                 rbar[i] = max(upper_bound(x + i, x + n, x[i] + l) - x - 1, i);
      |                           ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  303 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note:   candidate expects 3 arguments, 2 provided
In file included from /usr/include/c++/14/algorithm:61:
/usr/include/c++/14/bits/stl_algo.h:5706:5: note: candidate: 'template<class _Tp> constexpr _Tp std::max(initializer_list<_Tp>)'
 5706 |     max(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5706:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5716:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::max(initializer_list<_Tp>, _Compare)'
 5716 |     max(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5716:5: note:   template argument deduction/substitution failed:
a.cc:56:30: note:   mismatched types 'std::initializer_list<_Tp>' and 'long int'
   56 |                 rbar[i] = max(upper_bound(x + i, x + n, x[i] + l) - x - 1, i);
      |                           ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~