International Irregularities

시간 제한3초메모리 제한1024 MB

요약
감염도 순으로 정렬된 국가들과 격리 비용이 주어질 때, 각 출발지와 도착지 사이의 최단 이동 시간을 구한다.
난이도

어려움10점 중 8점

유형
그래프, 최단 경로, 동적 계획법, 이분 탐색
정답자
아직 제출이 없습니다

문제

Long, long ago on a planet far, far away, a highly contagious virus caused an enduring pandemic.

Even so, the people wanted to travel between countries for their summer holidays. In the good old before-days, travelling from any country to any other country took 1 full day. However, during the pandemic, certain countries preferred not to receive travellers from areas that had higher infection rates, so they made them quarantine for a certain number of days before allowing them to continue their trip or start their holiday.

To keep everything fair, an independent Bureau for Accurate Pandemic Classification was founded. They assigned a rr-value to each country based on the infection rate in that country. A higher rr-value indicates higher infection rate.

Each country asked tourists to quarantine if the country they just came from had a rr-value significantly higher than their own. In particular, when you wanted to travel from country ii to country jj, you would have to quarantine for t_jt\_j days if r_i>r_j+mr\_i > r\_j + m.

Archaeologists have found evidence of qq tourists travelling between nn countries. For each tourist, the start and destination are known. The question that remains to be answered is: how long was each tourist's minimal travel time?

입력

The input consists of:

  • One line with three integers nn, qq, and mm (2≤n≤1052\leq n \leq 10^5, 1≤q≤1051\leq q \leq 10^5, 0≤m≤1090\leq m \leq 10^9), the number of countries, the number of tourists, and the maximum allowed difference between two rr-values when travelling to a country with a lower infection rate.
  • One line with nn integers r_1,…,r_nr\_1, \dots, r\_n (0≤r_1≤⋯≤r_n≤1090 \leq r\_1 \leq \dots \leq r\_n \leq 10^9), the rr-value for each country.
  • One line with nn integers t_1,…,t_nt\_1, \dots, t\_n (0≤t_i≤1090 \leq t\_i \leq 10^9 for all ii), the required quarantine time in days when travelling to a country with a significantly lower rr-value.
  • qq lines, each with two integers xx and yy (1≤x,y≤n1 \leq x, y \leq n, x≠yx \neq y), indicating a tourist departing from country xx with final destination yy.

출력

For each tourist, output their minimal travel time in days between their departure country and destination country, in the order in which they appear in the input.

예제2

  1. 예제 1

    입력
    5 4 1
    0 5 6 7 8
    3 4 1 5 10
    1 4
    4 1
    4 2
    5 2
    
    예상 출력
    1
    4
    2
    3
    
  2. 예제 2

    입력
    5 4 10
    0 8 20 25 30
    5 11 13 6 3
    5 1
    5 2
    5 3
    5 4
    
    예상 출력
    6
    7
    1
    1