#P6611. [Code+#7] 六元环

[Code+#7] 六元环

Description

qwqwq.


Given a sequence a0,a1,,an,an+1a_0, a_1, \dots, a_n, a_{n+1}, where a0=an+1=+a_0 = a_{n+1} = +\infty, and a1,a2,,ana_1, a_2, \dots, a_n are given in the input.

For 1xn1 \le x \le n, define max0i<x,aiaxi\max_{0 \le i < x, a_i \ge a_x} i and xx to be adjacent, and define minx<in+1,ai>axi\min_{x < i \le n+1, a_i > a_x} i and xx to be adjacent. If xx and yy are adjacent, then yy and xx are also adjacent.

If 0b1,b2,b3,b4,b5,b6n+10 \le b_1, b_2, b_3, b_4, b_5, b_6 \le n+1, and bib_i is adjacent to bi+1b_{i+1}, b1b_1 is adjacent to b6b_6, and all bib_i are pairwise distinct, then the set {b1,b2,b3,b4,b5,b6}\{b_1, b_2, b_3, b_4, b_5, b_6\} is called a hexagonal cycle (that is, when judging whether two hexagonal cycles are the same, the order of bib_i is not considered).

There are mm modification operations. Each operation gives x yx\ y, and changes axa_x to ax+ya_x + y. After each modification, you need to output the number of hexagonal cycles.

All values mentioned above are integers, and 1n,m5×1051 \le n, m \le 5 \times 10^5, 1xn1 \le x \le n, 1ai,y1091 \le a_i, y \le 10^9.

Input Format

The first line contains an integer nn.

The second line contains nn integers representing a1,a2,,ana_1, a_2, \dots, a_n.

The third line contains an integer mm. The next mm lines each contain two integers x yx\ y, representing one modification operation.

Output Format

Output mm lines, each containing one integer, representing the number of hexagonal cycles after each modification.

6
1 2 5 4 3 6
4
1 8
3 6
5 10
2 7
3
0
1
1

Hint

Subtask Score Constraints
11 1010 max(n,m)100\max (n,m)\le 100
22 max(n,m)2000\max (n,m)\le 2000
33 2020 max(n,m)50000\max (n,m)\le 50000
44 for each operation, x100x\le 100
55 for each operation, y10y\le 10
66

Translated by ChatGPT 5