#!/usr/bin/env python3 # Not 100% sure yet if this is AC 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.rq = [] 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)) # for each country x, calculate the country with lowest infection rate that you can travel to from x without quarantine, # i.e. it must have infection rate at least x.p - m l = [] for c in reversed(sorted_countries): l.append(c) if len(l) > 1: while l[0].p > l[-1].p + m: l[0].rq.append(l[-2]) del l[0] for c in l: c.rq.append(sorted_countries[0]) # for each country x and for every power 2^k of 2, starting at 2, calculate the country with lowest infection rate that # you can travel to from x without quarantining anywhere in at most 2^k days steps = 2 while steps <= n * 2: for c in reversed(countries): c.rq.append(c.rq[-1].rq[-1]) steps *= 2 # 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: dist = 0 location = countries[x - 1] while location.id != y: index = 0 prev_target = location.rq[index] target = location.rq[index] dist_added = 1 while ( target.p > countries[y - 1].p ): # in this case target does not yet reach/overshoot y index += 1 if index >= len( location.rq ): # index got too large, trying to look left of leftmost country dist = math.inf location = countries[y - 1] break prev_target = target target = location.rq[index] dist_added *= 2 if index > 0: dist_added = int(dist_added / 2) dist += dist_added else: dist += 1 break if dist < math.inf: location = prev_target # 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([dist, countries[y - 1].best_right + 1, countries[y - 1].best_left + 1]) )