#!/usr/bin/env python3 import math from queue import PriorityQueue class Country: def __init__(self, id, p, t): self.id = id self.p = p self.t = t self.best_left = math.inf self.best_right = t self.dist = 0 self.source = self n, q, m = map(int, input().split(" ")) countries = [] p = [nr for nr in map(int, input().split(" "))] t = [nr for nr in map(int, input().split(" "))] for i in range(n): countries.append(Country(i + 1, p[i], t[i])) # sort countries by ascending infection rate sorted_countries = sorted(countries, key=lambda country: country.p) # go from the lowest infection rate country to the highest, keeping track of lowest t seen so far # this calculates the best way to get to a country by first quarantining in another country with lower infection rate lowest_seen_t = math.inf for c in sorted_countries: if lowest_seen_t + 1 < c.t: c.best_left = lowest_seen_t + 1 lowest_seen_t = min(lowest_seen_t, c.t) # go from the highest infection rate country to the lowest, keeping a priority queue of countries sorted by p # when you get a country from pq, check that it is still close enough (infection rate difference at most m) # this calculates the best way to get to a country by first quarantining in another country with higher infection rate, # then making some number of no-quarantine transits to the final destination pq = PriorityQueue(n) for c in reversed(sorted_countries): if not pq.empty(): candidate = pq.get()[2] while candidate.p > c.p + m and not pq.empty(): candidate = pq.get()[2] if candidate.p <= c.p + m: if candidate.best_right + 1 < c.best_right: c.best_right = candidate.best_right + 1 pq.put((candidate.best_right, candidate.id, candidate)) pq.put((c.best_right, c.id, c)) # idea: precompute the distance d without quarantine from the rightmost country to every other country # then use that info to compute the no-quarantine distance from x to y # problem: the result will be y.d - x.d or y.d - x.d + 1 but we do not know which one source = sorted_countries[-1] prev_dist_leftmost = sorted_countries[-1] prev_dist = 0 for index, c in enumerate(reversed(sorted_countries[:-1])): right_neighbor = sorted_countries[-index - 1] if right_neighbor.p > c.p + m: # in this case c is not reachable form the right by quarantine at all, since the next country has too high p c.dist = 0 source = c c.source = source prev_dist = 0 prev_dist_leftmost = c else: if prev_dist_leftmost.p > c.p + m: # cant get here with prev_dist + 1, it will take 1 more step c.dist = prev_dist + 2 prev_dist += 1 prev_dist_leftmost = right_neighbor else: c.dist = prev_dist + 1 # process queries for _ in range(q): x, y = map(int, input().split(" ")) if countries[x - 1].p <= countries[y - 1].p + m: print(1) else: # this is sometimes correct but not always if countries[x - 1].source.id == countries[y - 1].source.id: no_quarantine_distance = countries[y - 1].dist - countries[x - 1].dist else: no_quarantine_distance = math.inf # print(" ") # print("dist: %f" % dist) # print("right: %f" % (countries[y-1].best_right + 1)) # print("left: %f" % (countries[y-1].best_left + 1)) print( min( [ no_quarantine_distance, countries[y - 1].best_right + 1, countries[y - 1].best_left + 1, ] ) )