Language: C++

#pragma GCC optimize("O3")

#include <iostream>
#include <cstdio>
#include <cstring>
#include <list>
#include <cmath>
#include <algorithm>
#include <vector>
#include <map>
#include <queue>
#include <stack>
#include <set>
#include <complex>

#define in(x) scanf("%d",&x)
#define ein(x) ~scanf("%d",&x)
#define inl(x) scanf("%lld",&x)
#define ins(x) scanf("%s",x)
#define inf(x) scanf("%lf",&x)
#define inc(x) scanf("%c",&x)
#define gc() getchar()
#define out(x) printf("%d\n",x)
#define outl(x) printf("%lld\n",x)
#define od(x, y) printf("%d %d\n",x,y)
#define ms(x) memset(x,0,sizeof(x))
#define msc(x, n) memset(x,n,sizeof(x))
#define cp(x) while(!x.empty())x.pop()
#define rep(i, a, b) for(int i=a;i<b;++i)
#define rvep(i, a, b) for(int i=a;i>=b;--i)
#define elif else if
#define el else
#define wl(x) while(x)
#define sn scanf
#define bl bool
#define mp make_pair
#define pii pair<int,int>
#define rtn return
#define ope operator
#define cst const
#define it int
#define cr char
#define ctn continue
#define stt struct
#define il inline
#define tpl template
#define cl class
#define db double
#define sf(x) sizeof(x)
#define vt void
#define pf printf
typedef long long ll;
using namespace std;
cst it INF = 0x3f3f3f3f;

/*
 * 输入挂
 * 场场AK buff
 */

class endln {
};

class iofstream {
private:
    it idx;
    bl eof;
    cr buf[100000], *ps, *pe;
    cr bufout[100000], outtmp[50], *pout, *pend;

    il vt rnext() {
        if (++ps == pe)
            pe = (ps = buf) + fread(buf, sf(cr), sf(buf) / sf(cr), stdin), eof = true;
        if (ps == pe)
            eof = false;
    }

    il vt write() {
        fwrite(bufout, sf(cr), pout - bufout, stdout);
        pout = bufout;
    }

public:
    iofstream(char *in = NULL, char *out = NULL) : idx(-1), eof(true) {
        ps = buf, pe = buf + 1;
        pout = bufout, pend = bufout + 100000;
        if (in)
            freopen(in, "r", stdin);
        if (out)
            freopen(out, "w", stdout);
    }

    tpl<cl T>
    il bl fin(T &ans) {
#ifdef ONLINE_JUDGE
        ans = 0;
    T f = 1;
    if (ps == pe)
    {
        eof=false;
        rtn false;//EOF
    }
    do
    {
        rnext();
        if ('-' == *ps) f = -1;
    } wl(!isdigit(*ps) && ps != pe);
    if (ps == pe)
     {
        eof=false;
         rtn false;//EOF
    }
    do
    {
        ans = (ans << 1) + (ans << 3) + *ps - 48;
        rnext();
    } wl(isdigit(*ps) && ps != pe);
    ans *= f;
#else
        cin >> ans;
#endif
        rtn true;
    }

    tpl<cl T>
    il bl fdb(T &ans) {
#ifdef ONLINE_JUDGE
        ans = 0;
    T f = 1;
    if (ps == pe) rtn false;//EOF
    do
    {
        rnext();
        if ('-' == *ps) f = -1;
    } wl(!isdigit(*ps) && ps != pe);
    if (ps == pe) rtn false;//EOF
    do
    {
        ans = ans*10 + *ps - 48;
        rnext();
    } wl(isdigit(*ps) && ps != pe);
    ans *= f;
        if(*ps=='.')
        {
            rnext();
            T small=0;
            do
            {
                small=small*10+*ps-48;
                rnext();
            }wl(isdigit(*ps)&&ps!=pe);
            wl(small>=1)
            {
                small/=10;
            }
            ans+=small;
        }
#else
        cin >> ans;
#endif
        rtn true;
    }

/*
 * 输出挂
 * 超强 超快
 */

    il bl out_char(cst cr c) {
#ifdef ONLINE_JUDGE
        *(pout++) = c;
    if (pout == pend) write();
    write();
#else
        cout << c;
#endif
        rtn true;
    }

    il bl out_str(cst cr *s) {
#ifdef ONLINE_JUDGE
        wl(*s)
    {
        *(pout++) = *(s++);
        if (pout == pend) write();
    }
    write();
#else
        cout << s;
#endif
        rtn true;
    }

    tpl<cl T>
    il bl out_double(T x, it idx) {
        char str[50];
        string format = "%";
        if (~idx) {
            format += '.';
            format += (char) (idx + '0');
        }
        format += "f";
        sprintf(str, format.c_str(), x);
        out_str(str);
    }

    tpl<cl T>
    il bl out_int(T x) {
#ifdef ONLINE_JUDGE
        if (!x)
        {
            out_char('0');
            rtn true;
        }
        if (x < 0) x = -x, out_char('-');
        it len = 0;
        wl(x)
        {
            outtmp[len++] = x % 10 + 48;
            x /= 10;
        }
        outtmp[len] = 0;
        for (it i = 0, j = len - 1; i < j; i++, j--) swap(outtmp[i], outtmp[j]);
        out_str(outtmp);
#else
        cout << x;
#endif
        rtn true;
    }

    tpl<cl T>
    il bl out_intln(T x) {
#ifdef ONLINE_JUDGE
        out_int(x),out_char('\n');
        write();
#else
        cout << x << endl;
#endif
        rtn true;
    }

    tpl<cl T>
    il bl out_doubleln(T x, it idx) {
        out_double(x, idx), out_char('\n');
    }

    il iofstream &ope<<(cst db &x) {
        out_double(x, idx);
        rtn *this;
    }

    il iofstream &ope<<(cst it &x) {
        out_int(x);
        rtn *this;
    }

    il iofstream &ope<<(cst unsigned long long &x) {
        out_int(x);
        rtn *this;
    }

    il iofstream &ope<<(cst unsigned &x) {
        out_int(x);
        rtn *this;
    }

    il iofstream &ope<<(cst long &x) {
        out_int(x);
        rtn *this;
    }

    il iofstream &ope<<(cst ll &x) {
        out_int(x);
        rtn *this;
    }

    il iofstream &ope<<(cst endln &x) {
        out_char('\n');
        rtn *this;
    }


    il iofstream &ope<<(cst cr *x) {
        out_str(x);
        rtn *this;
    }

    il iofstream &ope<<(cst string &x) {
        out_str(x.c_str());
        rtn *this;
    }

    il iofstream &ope<<(cst char &x) {
        out_char(x);
        rtn *this;
    }

    il bl setw(it x) {
        if (x >= 0) {
            idx = x;
            rtn true;
        }
        rtn false;
    }

    il iofstream &ope>>(it &x) {
        if (!fin(x))eof = false;
        rtn *this;
    }

    il iofstream &ope>>(ll &x) {
        if (!fin(x))eof = false;
        rtn *this;
    }

    il iofstream &ope>>(db &x) {
        if (!fdb(x))eof = false;
        rtn *this;
    }

    il iofstream &ope>>(float &x) {
        if (!fdb(x))eof = false;
        rtn *this;
    }

    il iofstream &ope>>(unsigned &x) {
        if (!fin(x))eof = false;
        rtn *this;
    }

    il iofstream &ope>>(unsigned long long &x) {
        if (!fin(x))eof = false;
        rtn *this;
    }

    il ope bl() {
        rtn eof;
    }

    il cr getchar() {
#ifdef ONLINE_JUDGE
        if (ps == pe){
            eof=false;//EOF
            rtn 0;
        }
        rnext();
        if(ps+1==pe)
            eof=false;
        rtn *ps;
#else
        rtn std::getchar();
#endif
    }

    il iofstream &ope>>(char *str) {
#ifdef ONLINE_JUDGE
        if (ps == pe){
            eof=false;//EOF
            rtn *this;
        }
        do
        {
            rnext();
        } wl(isspace(*ps)&&iscntrl(*ps) && ps != pe);
        if (ps == pe){
            eof=false;//EOF
            rtn *this;
        }
        do
        {
            *str=*ps;
            ++str;
            rnext();
        } wl(!(isspace(*ps)||iscntrl(*ps)) && ps != pe);
        *str='\0';
        rtn *this;
#else
        cin >> str;
        rtn *this;
#endif
    }

    il iofstream &ope>>(string &str) {
#ifdef ONLINE_JUDGE
        str.clear();
        if (ps == pe){
            eof=false;//EOF
            rtn *this;
        }
        do
        {
            rnext();
        } wl(isspace(*ps)&&iscntrl(*ps) && ps != pe);
        if (ps == pe){
            eof=false;//EOF
            rtn *this;
        }
        do
        {
            str+=*ps;
            rnext();
        } wl(!(isspace(*ps)||iscntrl(*ps)) && ps != pe);
        rtn *this;
#else
        cin >> str;
        rtn *this;
#endif
    }

    il iofstream &getline(char *str) {
#ifdef ONLINE_JUDGE
        if (ps == pe){
            eof=false;//EOF
            rtn *this;
        }
        do
        {
            rnext();
            *str=*ps;
        }while(*ps!='\n'&&ps!=pe&&str++);
        *str='\0';
        rtn *this;
#else
        gets(str);
        rtn *this;
#endif
    }

    il bl endfile() {
        rtn eof;
    }
};

static iofstream fin;
static endln ln;

class range {
public:
    class iterator {
        friend class range;

    public:
        int operator*() const { return i_; }

        iterator &operator++() {
            ++i_;
            return *this;
        }

        iterator operator++(int) {
            iterator copy(*this);
            ++i_;
            return copy;
        }

        bool operator==(const iterator &other) const { return i_ == other.i_; }

        bool operator!=(const iterator &other) const { return i_ != other.i_; }

    protected:
        iterator(int start) : i_(start) {}

    private:
        int i_;
    };

    iterator begin() const { return begin_; }

    iterator end() const { return end_; }

    range(int begin, int end) : begin_(begin), end_(end) {}

private:
    iterator begin_;
    iterator end_;
};

template<typename T>
class reverse_iterator_class {
public:
    explicit reverse_iterator_class(const T &t) : t(t) {}

    typename T::const_reverse_iterator begin() const { return t.rbegin(); }

    typename T::const_reverse_iterator end() const { return t.rend(); }

private:
    const T &t;
};

using ll = long long;

template<typename T>
reverse_iterator_class<T> reverse(const T &t) {
    return reverse_iterator_class<T>(t);
}

using LL = long long;
const int inf = 0x3f3f3f3f;
const int maxn = 1e5 + 6;
array<int, maxn> x;
array<array<int, maxn>, 20> r;

int main() {
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    scanf("%d", &n);
    for (auto i:range(0, n)) {
        cin >> x[i];
    }
    int l, q;
    cin >> l >> q;
    for (auto i:range(0, n)) {
        r[0][i] = upper_bound(x.begin(), x.begin() + n, x[i] + l) - x.begin() - 1;
    }
    for (auto i:range(1, 17)) {
        for (auto j:range(0, n)) {
            r[i][j] = r[i - 1][r[i - 1][j]];
        }
    }
    for (auto i:range(0, q)) {
        int a, b;
        cin >> a >> b;
        --a;
        --b;
        if (a > b)swap(a, b);
        int cnt = 0;
        for (int i = 16; i >= 0; i--) {
            if (r[i][a] < b) {
                a = r[i][a];
                cnt += 1 << i;
            }
        }
        cout << cnt + 1 << endl;
    }
}

Compiler output:
a.cc: In member function 'bool iofstream::out_double(T, int)':
a.cc:205:5: warning: no return statement in function returning non-void [-Wreturn-type]
  205 |     }
      |     ^
a.cc: In member function 'bool iofstream::out_doubleln(T, int)':
a.cc:245:5: warning: no return statement in function returning non-void [-Wreturn-type]
  245 |     }
      |     ^
a.cc: In member function 'iofstream& iofstream::getline(char*)':
a.cc:424:9: error: 'gets' was not declared in this scope; did you mean 'getw'?
  424 |         gets(str);
      |         ^~~~
      |         getw
a.cc: At global scope:
a.cc:501:18: error: aggregate 'std::array<int, 100006> x' has incomplete type and cannot be defined
  501 | array<int, maxn> x;
      |                  ^
a.cc:15:1: note: 'std::array' is defined in header '<array>'; this is probably fixable by adding '#include <array>'
   14 | #include <complex>
  +++ |+#include <array>
   15 | 
a.cc:502:29: error: aggregate 'std::array<std::array<int, 100006>, 20> r' has incomplete type and cannot be defined
  502 | array<array<int, maxn>, 20> r;
      |                             ^
a.cc:502:29: note: 'std::array' is defined in header '<array>'; this is probably fixable by adding '#include <array>'