Under construction

Still being built. Things may change

Street Lamps

Time limit4sMemory limit1024 MB

Summary
Given N lamps with heights and Q single-lamp height updates, count after each update the pairs of equal-height lamps where every lamp between them is strictly shorter.
Level

Hard8 of 10

Topics
Segment tree, Array
Solved
No attempts yet

Problem

Along a straight road, NN street lamps are installed. The initial height of the ii-th lamp is AiA_i (1≤i≤N1 \le i \le N). You want to string electric wires between the lamps.

To hang a wire between lamp ii and lamp jj (where i<ji < j), both of the following conditions must hold.

  • Ai=AjA_i = A_j (the two lamps have the same height).
  • For every kk with i<k<ji < k < j, Ak<AiA_k < A_i (every lamp between them is shorter than both).

The administrator changes the heights of some lamps, and each change affects which wires can be hung. There are QQ height changes in total. Each change sets the height of the xx-th lamp to hh. After each change, count the pairs of lamps between which a wire can be hung.

Input

The first line contains two integers NN and QQ (2≤N≤100 0002 \le N \le 100\,000, 1≤Q≤250 0001 \le Q \le 250\,000).

The second line contains NN integers A1,A2,…,ANA_1, A_2, \ldots, A_N (1≤Ai≤1091 \le A_i \le 10^9).

Each of the next QQ lines contains two integers xx and hh (1≤x≤N1 \le x \le N, 1≤h≤1091 \le h \le 10^9), meaning AxA_x is set to hh. The height of lamp xx just before the change is guaranteed to differ from hh.

Output

Print the number of wires that can be hung in the initial state on the first line.

Then print, on each of the next QQ lines, the number of wires that can be hung after the corresponding height change.

Examples1

  1. Example 1

    Input
    6 2
    4 2 2 2 4 6
    4 6
    6 4
    
    Expected output
    3
    2
    2