#P6107. [Ynoi2010] Worst Case Top Tree

[Ynoi2010] Worst Case Top Tree

Problem Description

Given a sequence a0,a1,a2,,an,an+1a_0,a_1,a_2,\dots,a_n,a_{n+1}.

It satisfies 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 that max0i<x,aiaxi\max_{0 \le i < x, a_i \ge a_x} i and xx are adjacent, and minx<in+1,ai>axi\min_{x < i \le n+1, a_i > a_x} i and xx are 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 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 6-cycle (that is, when judging whether two 6-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 6-cycles.

Input Format

The first line contains an integer nn.

The second line contains nn integers 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, describing one modification operation.

Output Format

Output mm lines. Each line contains one integer, the number of 6-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

Idea: ccz181078, Solution: ccz181078, Code: ccz181078&zx2003, Data: nzhtl1477&zx2003.

For 100%100\% of the testdata, all values mentioned above are integers, and $1 \le n,m \le 5 \cdot 10^5;\;1 \le x \le n;\;1 \le a_i,y \le 10^9$.

Translated by ChatGPT 5