Short on three

Time limit: 2sMemory limit: 256MBInput: Output:

The team's NN players stand in a line, numbered from 00 to N−1N-1, and player ii has skill viv_i. The coach stands at the left end of the line, takes any ball passed to him and passes nothing back, because the coach doesn't play. Romanian football has an old touchline call, short on two, which means a short pass to your nearest teammate. The coach considers it far too generous to whoever happens to stand nearby, so at his club the ball goes short on three, still short, but always to someone strictly better.

A group consists of the players at some consecutive positions. At the start of a training exercise, every player in the group has a ball at his feet, and the coach moves immediately to the left of the group. Then each player passes the ball he started with and keeps any ball he receives: his ball goes to the closest player on his left, within the group, whose skill is strictly greater than his own, and if no such player exists, it goes to the coach.

Formally, number the players of the group from 00 to m−1m-1, from left to right, let bkb_k be the skill of the kk-th of them, and let the coach stand at position −1-1. For every ii from 00 to m−1m-1, the position within the group to which player ii passes his ball is

f(i)={max⁡{ j∣0≤j<i and bj>bi },if such a j exists,−1,otherwise.f(i) = \begin{cases} \max \{\, j \mid 0 \leq j < i \text{ and } b_j > b_i \,\}, & \text{if such a } j \text{ exists},\\ -1, & \text{otherwise}. \end{cases}


For example, for skills 5 1 3 25\ 1\ 3\ 2 we get f=−1,0,0,2f = -1, 0, 0, 2: the third player skips the second, whose skill is not better than his.

The coach calls a group disciplined if the sequence f(0),f(1),…,f(m−1)f(0), f(1), \ldots, f(m-1) is a mountain, meaning that there is a position pp with 0≤p≤m−10 \leq p \leq m-1 such that f(0)≤f(1)≤…≤f(p)≥f(p+1)≥…≥f(m−1)f(0) \leq f(1) \leq \ldots \leq f(p) \geq f(p+1) \geq \ldots \geq f(m-1). He has never explained why, and nobody has asked twice, because the coach doesn't play around either.

Over the season there are QQ transfers, applied one after another. A transfer is described by two integers pospos and valval, and it permanently replaces the player at position pospos of the line with a new player of skill valval, who stands on the same spot.

Task

After every transfer, the coach wants to know the maximum number of players in a disciplined group.

Implementation details

This is a function-implementation task. Your program must not read from standard input or write to standard output. You must include the header shortonthree.h and implement the following two functions, without implementing a main function:

void init(int N, std::vector<int> v);
int transfer(int pos, int val);

The function init is called exactly once in the beginning.

  • NN: the number of players.
  • vv: a vector of length NN, where v[i]v[i] is the skill of player ii, for 0≤i<N0 \leq i < N.

After the initial call to init, the function transfer is called exactly QQ times, once for each transfer, in the order in which the transfers happen.

  • pospos, valval: the player at position pospos (numbered from 00) is replaced with a new player of skill valval.
  • The function must return the maximum number of players in a disciplined group after this transfer.

Restrictions

  • 1≤N,Q≤200 0001 \leq N, Q \leq 200 \ 000.
  • 1≤vi, val≤1 000 000 0001 \leq v_i, \ val \leq 1 \ 000 \ 000 \ 000.
  • 0≤pos≤N−10 \leq pos \leq N-1.
# Points Restrictions
0 0 Examples.
1 9 N,Q≤50N, Q \leq 50
2 15 N,Q≤400N, Q \leq 400
3 18 Every viv_i and every valval is 11 or 22.
4 26 N,Q≤ 10 000N, Q \leq \ 10 \ 000
5 32 No additional constraints.

Examples

Sample grader input

6 2
1 2 1 3 2 5
1 3
3 1

Sample grader output

4
6

Explanation

The sample grader makes the following calls:

init(6, {1, 2, 1, 3, 2, 5});
transfer(1, 3);
transfer(3, 1);
  • After the first transfer, the skills are 1 3 1 3 2 51\ 3\ 1\ 3\ 2\ 5. The group on positions 22 to 55, with skills 1 3 2 51\ 3\ 2\ 5, gives f=−1,−1,1,−1f = -1, -1, 1, -1, which is a mountain. It can be shown that no group of 55 or 66 players is disciplined; the whole line, for instance, gives f=−1,−1,1,−1,3,−1f = -1, -1, 1, -1, 3, -1, which is not a mountain. So transfer(1, 3) returns 44.
  • After the second transfer, the skills are 1 3 1 1 2 51\ 3\ 1\ 1\ 2\ 5, and the whole line is one disciplined group, with f=−1,−1,1,1,1,−1f = -1, -1, 1, 1, 1, -1. So transfer(3, 1) returns 66.

Log In or Sign Up to be able to send submissions!