Mirror IIOT 2022-2023, International Round | publictransport

This was the problem page during the contest. Access the current page here.
Time limit: 1.5s Memory limit: 256MB Input: Output:

Cairo City is a metropolis with Nβ‹…MN\cdot M public transport stations, where N,Mβ‰₯3N,M\ge 3. The stations are arranged in NN concentric circles, and there are MM stations over each circle. The circles are numbered from 00 to Nβˆ’1N-1, and for each circle, the stations are numbered from 00 to Mβˆ’1M-1, inclusive. We refer to station jj on circle ii as station (i,j)(i,j) (where i=0…Nβˆ’1i=0 \ldots N-1, j=0…Mβˆ’1j=0 \ldots M-1).

In the city, there are NN tram lines. Tram lines are bidirectional and periodic, each connecting consecutive stations of some circle. Let tram line kk (where k=0…Nβˆ’1k=0 \ldots N-1) connect the stations on circle kk, i.e., stations (k,0)…(k,Mβˆ’1)(k,0) \ldots (k,M-1). Station (k,Mβˆ’1)(k,M-1) is connected with station (k,0)k,0).

Additionally, there are MM metro lines in the city. Metro lines are bidirectional, each connecting consecutive stations at the same position over different circles. Let metro line ll (where l=0…Mβˆ’1l=0 \ldots M-1) connect stations at position ll on each circle, i.e., stations (0,l)…(Nβˆ’1,l)(0,l) \ldots (N-1,l). Stations (Nβˆ’1,l)(N-1,l) and(0,l)(0,l) are not connected.


The figure shown above is the transport network for N=3N=3 and M=6M=6, having S0=1,S1=2,S2=3S_0=1, S_1=2, S_2=3 and T0=1,T1=5T_0=1, T_1=5.

Traveling between consecutive stations via tram line kk takes SkS_k minutes (where k=0…Nβˆ’1k=0 \ldots N-1). For each metro line ll (where l=0…Mβˆ’1l=0 \ldots M-1), traveling between stations (i,l)(i,l) and (i+1,l)(i+1,l) takes TiT_i minutes (where i=0…Nβˆ’2i=0 \ldots N-2).

Your task is to determine shortest travel times in this public transport network. You are given QQ queries, each query specifies two public transport stations (Aq,Bq)(A_q,B_q) and (Cq,Dq)(C_q,D_q) (where q=0…Qβˆ’1q=0 \ldots Q-1). For each query, find the minimum amount of time it takes to get from the first station to the second one, assuming that changing lines doesn't take extra time.

Input data

The first line contains integers NN, MM and QQ. The second line contains NN integers SkS_k. The third line contains Nβˆ’1N-1 integers TiT_i.

Each of the next QQ lines contains four integers Aq,Bq,CqA_q, B_q, C_q and DqD_q.

Output data

For each query, output a single number on a separate line, the minimum travel time (in minutes) between the specified stations.

Constraints and Clarifications

  • 3≀N≀100Β 0003 \le N \le 100 \ 000.
  • 3≀M≀100Β 0003 \le M \le 100 \ 000.
  • 1≀Q≀200Β 0001 \le Q \le 200 \ 000.
  • 1≀Sk≀1Β 000Β 000Β 0001 \le S_k \le 1 \ 000 \ 000 \ 000 for each k=0…Nβˆ’1k=0\ldots N-1.
  • 1≀Ti≀1Β 000Β 000Β 0001 \le T_i \le 1 \ 000 \ 000 \ 000 for each i=0…Nβˆ’2i=0\ldots N-2.
  • 0≀Aq,Cq<N0 \le A_q,C_q < N and 0≀Bq,Dq<M0 \le B_q,D_q < M for each q=0…Qβˆ’1q=0\ldots Q-1.
  • (Aq,Bq)β‰ (Cq,Dq)(A_q, B_q) \neq (C_q, D_q) for each q=0…Qβˆ’1q=0\ldots Q-1.
# Score Restrictions
1 0 Examples.
2 16 N,M≀50N,M \le 50.
3 13 M=3M = 3 and N≀2Β 000N \le 2 \ 000.
4 15 Nβ‹…M≀500Β 000N\cdot M \le 500 \ 000, all TiT_i-s are equal and Skβˆ’1≀SkS_{k-1} \le S_{k} for each 1≀k<N1 \le k < N.
5 23 All TiT_i-s are equal and Skβˆ’1≀SkS_{k-1} \le S_{k} for each 1≀k<N1 \le k < N.
6 10 Skβˆ’1≀SkS_{k-1} \le S_{k} for each 1≀k<N1 \le k < N.
7 23 No additional limitations.

Example

stdin

3 6 2
1 2 3
1 5
1 2 1 5
2 1 2 4

stdout

5
9

Explanation

The public transport network in the sample case is displayed in the figure above.

In the first query, the minimum time to reach station (1,5)(1,5) from (1,2)(1,2) is 55 minutes, e.g., by taking the path
(1,2)βˆ’(0,2)βˆ’(0,3)βˆ’(0,4)βˆ’(0,5)βˆ’(1,5)(1,2) - (0,2) - (0,3) - (0,4) - (0,5) - (1,5).

In the second query, the minimum time to reach station (2,4)(2,4) from (2,1)(2,1) is 99 minutes, e.g., by taking the path
(2,1)βˆ’(2,0)βˆ’(2,5)βˆ’(2,4)(2,1) - (2,0) - (2,5) - (2,4).

Log in or sign up to be able to send submissions!