func_code_string: #include <bits/stdc++.h>   using std::make_pair;   using std::pair;   const int N = 3e5 + 1e3;   int n, k;   int tot, rt;   struct Pair {     int l, r, pos, v1;     bool operator<(const Pair m) const { return v1 > m.v1; }   } p1[N], p2[N];   int sum[N << 4], ls[N << 4], rs[N << 4], mn[N << 4];   int r[N], a[N], mx[N], ans[N];   int ask(int cur, int l, int r, int L, int R) {     if (!cur) return 0;     if (L <= l && r <= R) {       return sum[cur];     }     int mid = l + r >> 1;     int ans = 0;     if (L <= mid) ans += ask(ls[cur], l, mid, L, R);     if (R > mid) ans += ask(rs[cur], mid + 1, r, L, R);     return ans;   }   int get(int cur, int l, int r, int L, int R) {     if (!cur) return 0;     if (L <= l && r <= R) {       return mn[cur];     }     int mid = l + r >> 1;     int ans = 0;     if (L <= mid) ans = std::max(ans, get(ls[cur], l, mid, L, R));     if (R > mid) ans = std::max(ans, get(rs[cur], mid + 1, r, L, R));     return ans;   }   void add(int &cur, int l, int r, int x, int v) {     if (!cur) cur = ++tot;     if (l == r) {       sum[cur] += v;       mn[cur] = std::max(mn[cur], v);       return;     }     int mid = l + r >> 1;     if (x <= mid)       add(ls[cur], l, mid, x, v);     else       add(rs[cur], mid + 1, r, x, v);     sum[cur] = sum[ls[cur]] + sum[rs[cur]];     mn[cur] = std::max(mn[ls[cur]], mn[rs[cur]]);   }   int main() {     scanf( %d %d , &n, &k);     for (int i = 1; i <= n; ++i) scanf( %d , &r[i]);     for (int i = 1; i <= n; ++i) scanf( %d , &a[i]);     for (int i = 1; i <= n; ++i) {       p1[i].v1 = r[i], p1[i].l = a[i], p1[i].pos = i;     }     std::sort(p1 + 1, p1 + 1 + n);     std::reverse(p1 + 1, p1 + 1 + n);     for (int i = 1; i <= n; ++i) {       add(rt, 1, 1e9 + 2, p1[i].l, 1);       mx[p1[i].pos] = ask(rt, 1, 1e9 + 2, std::max(1, p1[i].l - k),                           std::min(p1[i].l + k, (int(1e9)) + 2));     }     int q;     scanf( %d , &q);     memset(ls, 0, sizeof(ls));     memset(rs, 0, sizeof(rs));     memset(sum, 0, sizeof(sum));     rt = tot = 0;     memset(mn, 0, sizeof(mn));     for (int i = 1; i <= q; ++i) {       int l, r;       scanf( %d %d , &l, &r);       p2[i].l = l, p2[i].r = r, p2[i].pos = i,       p2[i].v1 = std::max(::r[l], ::r[r]);     }     std::sort(p2 + 1, p2 + 1 + q);     std::sort(p1 + 1, p1 + 1 + n);     int now = 1;     for (int i = 1; i <= q; ++i) {       while (p2[i].v1 <= p1[now].v1 && now <= n) {         add(rt, 1, 1e9 + 1, a[p1[now].pos], mx[p1[now].pos]);         now++;       }       if (abs(a[p2[i].l] - a[p2[i].r]) > 2 * k) {         ans[p2[i].pos] = 0;         continue;       }       ans[p2[i].pos] = get(           rt, 1, 1e9 + 1, std::max(1, std::max(a[p2[i].l] - k, a[p2[i].r] - k)),           std::min((int(1e9)), std::min(a[p2[i].l] + k, a[p2[i].r] + k)));     }     for (int i = 1; i <= q; ++i) printf( %d  , ans[i] <= 0 ? -1 : ans[i]);     return 0;   }   