luogu#P8904. [USACO22DEC] Mountains G

[USACO22DEC] Mountains G

Problem Description

Along the edge of Farmer John’s farm, there are N(1N2000)N(1 \le N \le 2000) mountains arranged in a line at equal spacing. These mountains can be represented by a height array h1,h2,,hNh_1,h_2,\cdots,h_N. For mountain ii, mountain jj is visible if there is no mountain strictly higher than the line of sight connecting the peaks of mountains jj and ii. Formally, for two mountains i<ji<j, if there does not exist kk such that i<k<ji<k<j and the point (k,hk)(k,h_k) is above the line segment connecting (i,hi)(i,h_i) and (j,hj)(j,h_j), then these two mountains can see each other.

You are given Q(1Q2000)Q(1 \le Q \le 2000) update operations, each of which increases the height of one mountain. After each update, output the number of unordered pairs of mountains that can see each other.

Input Format

The first line contains NN.

The second line contains NN heights h1,h2,,hNh_1,h_2,\cdots,h_N (for each ii, 0hi1090 \le h_i \le 10^9).

The third line contains QQ.

Lines 44 to 3+Q3+Q each contain x,y(1xN,1y)x,y(1 \le x \le N,1 \le y), where xx is the index of the mountain and yy is the amount by which its height increases. The input guarantees that the height of this mountain after the update does not exceed 10910^9.

Output Format

Output QQ lines, each containing the number of unordered pairs of mountains that can see each other after the corresponding update.

5
2 4 3 1 5
3
4 3
1 3
3 2
7
10
7

Hint

Sample 1 Explanation

Initially, the following pairs of mountains can see each other: (1,2)(1,2), (2,3)(2,3), (2,5)(2,5), (3,4)(3,4), (3,5)(3,5), (4,5)(4,5), for a total of 66 pairs.

After the first update, the height of mountain 44 becomes 44. This does not block any existing visibility, but it makes mountain 44 able to see mountain 22, so the answer becomes 77.

After the second update, the height of mountain 11 becomes 55. This does not block any existing visibility, but it makes mountain 11 able to see mountains 33, 44, and 55, so the answer becomes 1010.

After the third update, the height of mountain 33 becomes 55. This blocks mountain 11 from seeing mountain 44, and blocks mountain 22 from seeing mountains 44 and 55. At the same time, since this mountain could already see all other mountains, it does not make it see more mountains, so the answer becomes 77.

Test Point Properties

  • Test points 252-5 satisfy N,Q100N,Q \le 100.
  • Test points 6116-11 satisfy Q10Q \le 10.
  • Test points 122112-21 have no additional properties.

Translated by ChatGPT 5