#P12503. 「CEOI2025」最高层

「CEOI2025」最高层

Description

In an alternate universe, Vlad is stuck inside a futuristic version of the Poenari Fortress, now spanning nn floors, numbered 00 through n1n-1. From each floor ii (0in10 \leq i \leq n-1), he can only go up, either by taking the stairs and paying 11 drop of blood (this is the currency that vampires use to pay in Romania), or by turning into a bat and traversing the vents, for which he has to pay 22 drops of blood. The stairs can take him up to v[i]v[i] floors upwards, while the vents span up to w[i]w[i] floors upwards, where vv and ww are two given arrays: v=v[0],v[1],,v[n1]v = v[0], v[1], \ldots, v[n-1] and w=w[0],w[1],,w[n1]w = w[0], w[1], \ldots, w[n-1].

Formally, from floor ii (0in10 \leq i \leq n-1), Vlad can go:

  • anywhere from floor i+1i+1 to floor i+v[i]i+v[i] without exceeding n1n-1, for a cost of 11
  • anywhere from floor i+1i+1 to floor i+w[i]i+w[i] without exceeding n1n-1, for a cost of 22

Furthermore, his brothers Radu and Mircea proposed mm scenarios for Vlad, each one consisting of two floors AA and BB (ABA \leq B). Vlad has to answer their mm questions: what is the least amount of blood that he has to sacrifice to get from floor AA to floor BB?

Implementation Details

You will have to implement the function solve:

std::vector<int> solve(std::vector<int> &v, std::vector<int> &w, std::vector<std::pair<int,int>> &queries);
  • Receives the vectors vv, the heights of the flights of stairs, and ww, the heights of the vent systems, starting at each floor, both of them of size nn.
  • Also receives the queries, a vector of pairs of size mm. Each pair contains AA and BB as described in the statement.
  • Returns a vector of size mm, consisting of the answers to the mm queries.

Constraints

  • 1n,m5000001 \leq n, m \leq 500\,000
  • 1v[i],w[i]n1 \leq v[i], w[i] \leq n for all 0in10 \leq i \leq n-1
  • 0ABn10 \leq A \leq B \leq n-1 for all queries.

Subtasks

  1. (5 points) 1n3001 \leq n \leq 300, 1m5000001 \leq m \leq 500\,000
  2. (7 points) 1n30001 \leq n \leq 3000, 1m30001 \leq m \leq 3000
  3. (11 points) 1n200001 \leq n \leq 20\,000, 1m200001 \leq m \leq 20\,000
  4. (44 points) 1n2000001 \leq n \leq 200\,000, 1m2000001 \leq m \leq 200\,000
  5. (8 points) 1n5000001 \leq n \leq 500\,000, 1m5000001 \leq m \leq 500\,000, v[i]v[j]v[i] \leq v[j] and w[i]w[j]w[i] \leq w[j] for all 0i<jn10 \leq i < j \leq n-1
  6. (25 points) No further restrictions.

Example 1

Consider the following call:

solve({2, 3, 1, 1, 1, 1, 2}, {3, 4, 1, 2, 1, 2, 2}, {{0, 4}, {0, 5}, {0, 6}})

Here we have n=7n = 7 and 33 queries, v=[2,3,1,1,1,1,2]v = [2, 3, 1, 1, 1, 1, 2] and w=[3,4,1,2,1,2,2]w = [3, 4, 1, 2, 1, 2, 2].

For the first query (0,4)(0, 4), Vlad has to make two 1-cost jumps: 010 \to 1 (even though he can jump to 22, floor 11 will then take him further), then 141 \to 4. Total cost: 1+1=21 + 1 = 2.

For the second query (0,5)(0, 5), there are 22 optimal paths: 010 \to 1 (cost 11), 141 \to 4 (cost 11), 454 \to 5 (cost 11); the second path is 010 \to 1 (cost 11), 151 \to 5 (cost 22). Total cost: 1+1+1=1+2=31+1+1 = 1+2 = 3.

For the third query (0,6)(0, 6), one example path of cost 44 is 010 \to 1 (cost 11), 151 \to 5 (cost 22), 565 \to 6 (cost 11). Total cost: 1+2+1=41+2+1=4

So the vector that the function will return must be:

{2, 3, 4}

Example 2

Consider the following call:

solve({1, 1, 1, 2, 3, 2, 1, 1, 2, 3}, {2, 4, 1, 4, 1, 4, 1, 3, 2, 3}, {{3, 9}, {0, 9}, {0, 7}, {0, 4}, {3, 5}})

These are the optimal paths for the queries:

  • (3,9)(3,9): 353 \to 5 (cost 11), 595 \to 9 (cost 22)     \implies total: 33
  • (0,9)(0,9): 010 \to 1 (cost 11), 151 \to 5 (cost 22), 595 \to 9 (cost 22)     \implies total: 55
  • (0,7)(0,7): 010 \to 1 (cost 11), 151 \to 5 (cost 22), 575 \to 7 (cost 11)     \implies total: 44
  • (0,4)(0,4): 010 \to 1 (cost 11), 141 \to 4 (cost 22)     \implies total: 33
  • (3,5)(3,5): 353 \to 5 (cost 11)     \implies total: 11

So the vector that the function will return must be:

{3, 5, 4, 3, 1}

Sample grader

The sample grader reads the input in the following format:

  • line 11: nn
  • line 22: v[0] v[1]  v[n1]v[0]\ v[1]\ \ldots\ v[n-1]
  • line 33: w[0] w[1]  w[n1]w[0]\ w[1]\ \ldots\ w[n-1]
  • line 44: mm
  • line 5+i5 + i (0im1)(0 \leq i \leq m-1): A BA\ B

and outputs mm lines, the result of the call to solve.