#P3350. [ZJOI2016] 旅行者

[ZJOI2016] 旅行者

Description

Xiao Y came to a new city to travel. She found that the city layout is a grid: there are nn roads running from east to west and mm roads running from south to north. These roads intersect pairwise to form n×mn \times m intersections (i,j)(i,j), where 1in,1jm1 \le i \le n, 1 \le j \le m.

She found that the conditions of different roads vary, so passing through different intersections takes different amounts of time. After investigation, she learned that going from intersection (i,j)(i,j) to (i,j+1)(i,j+1) takes time r(i,j)r(i,j), and going from (i,j)(i,j) to (i+1,j)(i+1,j) takes time c(i,j)c(i,j). Note that the roads are bidirectional. Xiao Y has qq queries and wants to know the minimum time needed to travel from intersection (x1,y1)(x_1,y_1) to intersection (x2,y2)(x_2,y_2).

Input Format

The first line contains 2 positive integers n,mn, m, representing the size of the city.

The next nn lines each contain m1m-1 integers. In the ii-th line, the jj-th positive integer represents the time r(i,j)r(i,j) to move between adjacent intersections.

The next n1n-1 lines each contain mm integers. In the ii-th line, the jj-th positive integer represents the time c(i,j)c(i,j) to move between adjacent intersections.

The next line contains a single positive integer qq, the number of Xiao Y’s queries.

The following qq lines each contain 4 positive integers x1,y1,x2,y2x_1, y_1, x_2, y_2, representing the positions of two intersections.

Output Format

Output qq lines. Each line contains a single integer, the minimum time required to travel from one intersection to the other.

2 2
2
3
6 4
2
1 1 2 2
1 2 2 1
6
7

Hint

Constraints

  • n×m2×104n \times m \le 2 \times 10^4.
  • q105q \le 10^5.
  • 1r(i,j),c(i,j)1041 \le r(i,j), c(i,j) \le 10^4.

Translated by ChatGPT 5