QOJ.ac

QOJ

Time Limit: 1 s Memory Limit: 256 MB Total points: 100

#2029. Minimum Cost Paths

統計

Farmer John's pasture can be regarded as an $N\times M$ ($2\le N\le 10^9$, $2\le M\le 2\cdot 10^5$) 2D grid of square "cells" (picture a huge chessboard). The cell at the $x$-th row from the top and $y$-th column from the right is denoted by $(x,y)$ for each $x\in [1,N], y\in [1,M]$. Furthermore, for each $y\in [1,M]$, the $y$-th column is associated with the cost $c_y$ ($1\le c_y\le 10^9$).

Bessie starts at the cell $(1,1)$. If she is currently located at the cell $(x,y)$, then she may perform one of the following actions:

  • If $y<M$, Bessie may move to the next column (increasing $y$ by one) for a cost of $x^2$.
  • If $x<N$, Bessie may move to the next row (increasing $x$ by one) for a cost of $c_y$.

Given $Q$ ($1\le Q\le 2\cdot 10^5$) independent queries each of the form $(x_i,y_i)$ ($x_i\in [1,N], y_i\in [1,M]$), compute the minimum possible total cost for Bessie to move from $(1,1)$ to $(x_i,y_i)$.

INPUT FORMAT (input arrives from the terminal / stdin):

The first line contains $N$ and $M$.

The second line contains $M$ space-separated integers $c_1,c_2,\ldots,c_M$.

The third line contains $Q$.

The last $Q$ lines each contain two space-separated integers $x_i$ and $y_i$.

OUTPUT FORMAT (print output to the terminal / stdout):

$Q$ lines, containing the answers for each query.

Note that the large size of integers involved in this problem may require the use of 64-bit integer data types (e.g., a "long long" in C/C++).

SAMPLE INPUT:

5 4
1 100 100 20
20
1 1
2 1
3 1
4 1
5 1
1 2
2 2
3 2
4 2
5 2
1 3
2 3
3 3
4 3
5 3
1 4
2 4
3 4
4 4
5 4

SAMPLE OUTPUT:

0
1
2
3
4
1
5
11
19
29
2
9
20
35
54
3
13
29
49
69
The output in grid format:
    1  2  3  4
  *--*--*--*--*
1 | 0| 1| 2| 3|
  *--*--*--*--*
2 | 1| 5| 9|13|
  *--*--*--*--*
3 | 2|11|20|29|
  *--*--*--*--*
4 | 3|19|35|49|
  *--*--*--*--*
5 | 4|29|54|69|
  *--*--*--*--*

SCORING:

  • Test cases 1-3 satisfy $N,M\le 2000$.
  • Test cases 4-8 satisfy $c_2>c_3>\cdots>c_M$.
  • Test cases 9-15 satisfy $N\le 2\cdot 10^5$.
  • Test cases 16-20 satisfy no additional constraints.

Problem credits: Benjamin Qi

About Issues

We understand that our problem archive is not perfect. If you find any issues with the problem, including the statement, scoring configuration, time/memory limits, test cases, etc.

You may use this form to submit an issue regarding the problem. A problem moderator will review your issue and proceed it properly.

STOP! Before you submit an issue, please READ the following guidelines:

  1. This is not a place to publish a discussion, editorial, or requests to debug your code. Your issue will only be visible by you and problem moderators. Other users will not be able to view or reply your issues.
  2. Do not submit duplicated issues. If you have already submitted one, please wait for an moderator to review it. Submitting multiple issues will not speed up the review process and might cause your account to be banned.
  3. Issues must be filed in English or Chinese only.
  4. Be sure your issue is related to this problem. If you need to submit an issue regarding another problem, contest, category, etc., you should submit it to the corresponding page.

Active Issues 0

No issues in this category.

Closed/Resolved Issues 0

No issues in this category.