#P3315. [SDOI2014] 酗酒者

[SDOI2014] 酗酒者

Description

Alice\text{Alice} finds that when people are in a bad mood, they tend to drink heavily. Unlike the jubilant celebration after an OI\text{OI} contestant’s victory, a drunkard often forgets the way home and wanders randomly around the streets, shouting incomprehensible words.

These days, Bob\text{Bob} is in a bad mood due to exams. Every night he finds a bar in the city. After getting drunk and leaving the bar, he begins to wander randomly through the city streets until, at some moment, if he happens to meet Alice\text{Alice} who is out watching the stars at night, she will take him home.

The city streets where Alice\text{Alice} and Bob\text{Bob} live can be modeled as a grid of NN rows and MM columns. Rows are numbered from 00 to N1N - 1, and columns from 00 to M1M - 1. There are N×MN \times M intersections in total, and each intersection is denoted by coordinates (i,j)(i, j). If i<N1i < N - 1, then (i,j)(i, j) and (i+1,j)(i + 1, j) are connected by an undirected edge whose length is p(i,j)p_{(i,j)}, representing the time needed to traverse this road. If j<M1j < M - 1, then (i,j)(i, j) and (i,j+1)(i, j + 1) are connected by an undirected edge whose length is q(i,j)q_{(i,j)}.

Given two points (u,v)(u, v) and (s,t)(s, t), which are the locations of the bar that Bob\text{Bob} visits tonight and the place where Alice\text{Alice} watches the stars tonight, respectively. After leaving the bar, at each intersection, Bob\text{Bob} will choose uniformly at random one of the adjacent intersections and then move to it. Before reaching the next intersection, Bob\text{Bob} will not turn back mid-edge. Also, Bob\text{Bob}’s future movement is not affected by the roads he has previously taken.

Specifically: if Bob\text{Bob} moves from (3,4)(3, 4) to (3,5)(3, 5), he may immediately return to (3,4)(3, 4) after arriving at (3,5)(3, 5). At a four-way intersection, Bob\text{Bob} moves in each direction with probability 1/41/4; at a three-way intersection (only on the boundary), with probability 1/31/3 for each; at a two-way intersection (only at the four corners), with probability 1/21/2 for each.

Alice\text{Alice} wants to know, starting from when Bob\text{Bob} leaves the bar, how long she expects to wait until meeting Bob\text{Bob}. That is, for the given points (u,v)(u, v) and (s,t)(s, t), what is the expected time for Bob\text{Bob} to go from (u,v)(u, v) to (s,t)(s, t)?

Input Format

The first line contains NN, MM.

Then there are N1N - 1 lines, each containing MM positive integers, where the entry in row ii and column jj is p(i,j)p_{(i,j)}.

Then there are NN lines, each containing M1M - 1 positive integers, where the entry in row ii and column jj is q(i,j)q_{(i,j)}.

A single line follows with an integer QQ, the number of queries.

Then QQ lines follow, each containing four integers uu, vv, ss, tt.

Output Format

Output QQ lines. For each query, print the expected time for Bob\text{Bob} to go from (u,v)(u, v) to (s,t)(s, t). You may output any number of decimal places, but your answer is considered correct only if the error rate is within 0.1%0.1\%.

2 2
1 2
3
4
4
0 0 0 1
1 0 0 1
1 1 0 1
0 1 1 0
7.0000
10.0000
8.0000
10.0000

Hint

For 10%10\% of the testdata, N×M25N \times M \le 25.

For 30%30\% of the testdata, N×M625N \times M \le 625.

For 50%50\% of the testdata, N×M2500N \times M \le 2500.

For 100%100\% of the testdata, 1N×M1041 \le N \times M \le 10^4, 1Q1001 \le Q \le 100, and 1p(i,j),q(i,j)2001 \le p_{(i,j)}, q_{(i,j)} \le 200.

Additionally, for 10%10\% of the testdata, min(N,M)10\min(N, M) \le 10.

Translated by ChatGPT 5