1

· · 个人记录

Farmer John's pasture can be regarded as an N×M (2≤N≤109, 2≤M≤2⋅105) 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∈[1,N],y∈[1,M]. Furthermore, for each y∈[1,M], the y-th column is associated with the cost cy (1≤cy≤109).
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 x2.
If x<N, Bessie may move to the next row (increasing x by one) for a cost of cy.
Given Q (1≤Q≤2⋅105) independent queries each of the form (xi,yi) (xi∈[1,N],yi∈[1,M]), compute the minimum possible total cost for Bessie to move from (1,1) to (xi,yi).

INPUT FORMAT (input arrives from the terminal / stdin):
The first line contains N and M.
The second line contains M space-separated integers c1,c2,…,cM.

The third line contains Q.

The last Q lines each contain two space-separated integers xi and yi.

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≤2000.
Test cases 4-8 satisfy c2>c3>⋯>cM.
Test cases 9-15 satisfy N≤2⋅105.
Test cases 16-20 satisfy no additional constraints.
Problem credits: Benjamin Qi

Gem Island 2

令 g_{i,j} 表示钦定 i 个位置 \ge j 的方案数,则 g_{i,j}=\binom{n}{i}\binom{d-ij+n-1}{n-1}

令 f_{i,j} 表示恰好 i 个位置 \ge j 的方案数,则 f_{i,j}=\sum_{k=i}^{d}(-1)^{k-i}\binom{k}{i}g_{k,j}

ans=\sum_{i,j}\min(i,r)\times f_{i,j} =\sum_{i,j}\min(i,r)\sum_{k}(-1)^{k-i}\binom{k}{i}\binom{n}{k}\binom{d-jk+n-1}{n-1} \sum_{k=1}^{d}\frac{(-1)^k}{(n-k)!}\sum_{i=1}^{d}\frac{\min(i,r)}{i!(-1)^i(k-i)!}\sum_{j=1}^{d/k}\frac{(d-jk+n-1)!}{(d-jk)!}

记 f[k]=\sum_{i=1}^{d}\frac{\min(i,r)}{i!(-1)^i(k-i)!} , g[k]=\sum_{j=1}^{d/k}\frac{(d-jk+n-1)!}{(d-jk)!} ,不难发现 f 可 O(d) 处理, g 可 O(dloglogd) 处理。

总时间复杂度 O(d\log\log d) 。