#include #include int main() { int n, q, m; std::cin >> n >> q >> m; int p[n], t[n]; for(int i = 0; i < n; i++) { std::cin >> p[i]; } for(int i = 0; i < n; i++) { std::cin >> t[i]; t[i]++; } // hop[l][i] Which place we land after 2 ^ l steps to the right starting at i. // This is needed to compute the number of hops needed when we do not quarantine. int L = 18; int hop[L][100000]; // Initialize the first hop. int r = 0; for(int i = 0; i < n; i++) { while(r < n - 1 && p[r + 1] <= p[i] + m) { r++; } hop[0][i] = r; } // Do the recursion for the next layers. for(int l = 1; l < L; l++) { for(int i = 0; i < n; i++) { hop[l][i] = hop[l - 1][hop[l - 1][i]]; } } // The number of hops to go from j to i, or inf if this is impossible. int inf = 2 * 1000000000; auto num_hops = [&](int i, int j) { if(i > j) { std::swap(i, j); } int h = 0; while(i < j) { h += 1; int new_i = hop[0][i]; if(new_i == i) { return inf; } i = new_i; } return h; }; // For each position, find the minimal cost of getting there given that you quarantine. // First find the cost when jumping to a place with lower p and then hopping up once. int jumpleft[n]; int best = t[0]; for(int i = 0; i < n; i++) { jumpleft[i] = std::min(t[i], best + 1); best = std::min(t[i], best); } // Next find the cost when jumping and then taking some additional hops to the left. int jumpright[n]; jumpright[n - 1] = t[n - 1]; best = n - 1; for(int i = n - 2; i >= 0; i--) { int h = num_hops(i, best); if(h == inf) { best = i; jumpright[i] = t[i]; continue; } int viabest = num_hops(i, best) + jumpright[best]; if(t[i] < viabest) { jumpright[i] = t[i]; best = i; } else { jumpright[i] = viabest; } } // Answer all queries. for(int i = 0; i < q; i++) { int x, y; std::cin >> x >> y; x--; y--; // Given assert(x != y); // For free ? if(x < y) { std::cout << 1 << std::endl; continue; } int h = num_hops(x, y), l = jumpleft[y], r = jumpright[y]; // print(h, l, r) std::cout << std::min(h, std::min(l, r)) << std::endl; } }