func_code_string: #include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 7;   const int LG = 27;   const int INF = 1e9 + 7;   pair<int, int> seg[LG][N];   int mx[LG][N];   int r[N], a[N], sol[N];   pair<int, int> p[N];   int n, k;   void build(int l = 0, int r = n, int d = 0) {     if (r - l == 1) {       seg[d][l] = make_pair(p[l].second, l);       mx[d][l] = sol[l];       return;     }     int mid = (l + r) / 2;     build(l, mid, d + 1);     build(mid, r, d + 1);     merge(seg[d + 1] + l, seg[d + 1] + mid, seg[d + 1] + mid, seg[d + 1] + r,           seg[d] + l);     int out = 0;     for (int i = r - 1; i >= l; i--) {       out = max(out, sol[seg[d][i].second]);       mx[d][i] = out;     }   }   pair<int, int> get(int l, int r, int val, int s = 0, int e = n, int d = 0) {     if (l >= e || r <= s) return make_pair(0, 0);     if (l <= s && e <= r) {       int pos = lower_bound(seg[d] + s, seg[d] + e, make_pair(val, 0)) - seg[d];       int pos2 =           lower_bound(seg[d] + s, seg[d] + e, make_pair(val + 1, 0)) - seg[d];       int ans1 = 0;       if (pos < e) ans1 = mx[d][pos];       return make_pair(ans1, pos2 - s);     }     int mid = (s + e) / 2;     pair<int, int> a = get(l, r, val, s, mid, d + 1);     pair<int, int> b = get(l, r, val, mid, e, d + 1);     return make_pair(max(a.first, b.first), a.second + b.second);   }   int main() {     scanf( %d %d , &n, &k);     for (int i = 0; i < n; i++) {       scanf( %d , &r[i]);       p[i].second = r[i];     }     for (int i = 0; i < n; i++) {       scanf( %d , &a[i]);       p[i].first = a[i];     }     sort(p, p + n);     build();     int L = 0, R = 0;     for (int i = 0; i < n; i++) {       while (L < n && p[L].first + k < p[i].first) L++;       while (R < n && p[i].first + k >= p[R].first) R++;       sol[i] = get(L, R, p[i].second).second;     }     build();     int m;     cin >> m;     while (m--) {       int x, y;       scanf( %d %d , &x, &y);       x--, y--;       if (a[x] > a[y]) swap(x, y);       int L = lower_bound(p, p + n, make_pair(a[y] - k, 0)) - p;       int R = lower_bound(p, p + n, make_pair(a[x] + k + 1, 0)) - p;       int ans = get(L, R, max(r[x], r[y])).first;       if (ans < 2)         printf( -1 n );       else         printf( %d n , ans);     }     return 0;   }   