#P7408. [JOI 2021 Final] 地牢 3 / Dungeon 3

[JOI 2021 Final] 地牢 3 / Dungeon 3

Description

There is a building with N+1N+1 floors, numbered 1N+11 \sim N+1. There are MM people, numbered 1M1 \sim M. Moving from floor ii to floor i+1i+1 costs AiA_i energy, and you can only move from floor ii to floor i+1i+1, not the other way around.

From floor 11 to floor NN, there is a shop on each floor. At the shop on floor ii, you can pay BiB_i coins to increase your energy by 11. You may use the shop multiple times, but you cannot make your energy exceed your energy cap. The energy cap of person jj is UjU_j. Everyone starts with energy 00.

Person jj starts on floor SjS_j, and they want to reach floor TjT_j.

For each person, answer the minimum number of coins needed to reach their destination, or state that it is impossible.

Input Format

The first line contains two integers N,MN, M, representing the number of floors and the number of people.

The second line contains NN integers AiA_i, representing the energy required to move.

The third line contains NN integers BiB_i, representing the number of coins needed to buy energy at each floor’s shop.

The next MM lines each contain three integers Sj,Tj,UjS_j, T_j, U_j, describing one person.

Output Format

Output MM lines. Each line contains one integer, representing the minimum number of coins person jj needs to reach floor TjT_j.

If they cannot move to floor TjT_j, output 1-1.

5 4
3 4 1 1 4
2 5 1 2 1
1 6 3
1 6 4
3 5 1
2 5 9
-1
29
3
22
10 10
1 8 9 8 1 5 7 10 6 6
10 10 2 8 10 3 9 8 3 7
2 11 28
5 11 28
7 11 28
1 11 18
3 11 18
8 11 18
4 11 11
6 11 11
10 11 11
9 11 5
208
112
179
248
158
116
234
162
42
-1
20 20
2 3 2 11 4 6 9 15 17 14 8 17 3 12 20 4 19 8 4 5
19 3 18 2 13 7 5 19 10 1 12 8 1 15 20 1 13 2 18 6
12 15 67
7 15 18
16 17 14
9 21 97
1 19 43
3 18 31
16 20 70
7 20 28
1 16 61
3 5 69
9 10 15
2 13 134
11 19 23
16 20 14
5 21 16
15 20 11
7 11 54
7 16 16
13 17 10
3 15 135
151
591
4
284
339
517
35
581
254
58
-1
178
519
-1
-1
-1
219
-1
-1
214

Hint

Explanation of Sample 1

Person 11 cannot reach floor 33.

Person 22 can reach floor 66 in the following way:

  • On floor 11, spend 88 coins to make their energy 44.
  • Move to floor 22, and the energy becomes 11.
  • On floor 22, spend 1515 coins to make their energy 44.
  • Move to floor 33, and the energy becomes 00.
  • On floor 33, spend 44 coins to make their energy 44.
  • Move to floor 44, and the energy becomes 33.
  • Move to floor 55, and the energy becomes 22.
  • On floor 55, spend 22 coins to make their energy 44.
  • Move to floor 66.

In total, 2929 coins are needed.

Constraints

This problem uses bundled testdata.

  • Subtask 1 (11 pts): N,M3000N, M \le 3000.
  • Subtask 2 (14 pts): All UjU_j are equal.
  • Subtask 3 (31 pts): Tj=N+1T_j = N+1.
  • Subtask 4 (44 pts): No special constraints.

For 100%100\% of the testdata, 1N,M,Ai,Bi2×1051 \le N, M, A_i, B_i \le 2 \times 10^5, 1Sj<TjN+11 \le S_j < T_j \le N+1, 1Uj1091 \le U_j \le 10^9.

Notes

Translated from The 20th Japanese Olympiad in Informatics Final Round E ダンジョン 3 English translation Dungeon 3.

Translated by ChatGPT 5