#P15283. [MCO 2024] Knights

[MCO 2024] Knights

Description

There are NN knights labeled 11 to NN standing in a row. The ii-th knight has power PiP_i.

The knights are competing in a jousting tournament. To do so, each knight will hold a sword pointing either to the left or to the right. The ii-th knight will point their sword to the left if Si=0S_i=0 and to the right if Si=1S_i=1 where SS is a binary string of length NN.

A joust is defined as the following process:

  1. Initially all knights are alive.
  2. Let AA be the list of knights still alive arranged in increasing order of label and mm be the size of AA.
  3. For each ii from 11 to mm. If the AiA_i-th knight has an adjacent knight with bigger power pointing their sword at the AiA_i-th knight's direction, then mark the AiA_i knight as dead.\textbf{dead.} Formally the AiA_i-th knight will be marked as dead if one of the following condition is true.
    • i1>0i-1 > 0 and PAi1>PAiP_{A_{i-1}} > P_{A_{i}} and SAi1=1S_{A_{i-1}} = 1
    • i+1mi+1 \leq m and PAi+1>PAiP_{A_{i+1}} > P_{A_{i}} and SAi+1=0S_{A_{i+1}} = 0
  4. Return to step 2 if there is at least one knight in AA marked as dead.

Now you are given QQ queries, each query are of the form:

  • Given integer xx (1xn1 \leq x \leq n), change SxS_x to 1Sx1-S_x.

After each query, find the number of knights alive after a joust is performed.

Do note that knights do not stay dead after a joust.

Input Format

The first line contains two space-separated integers NN and QQ (1N,Q1061 \le N,Q\le 10^6) -- the size of PP and the number of queries.

The second line contains NN space-separated integers P1,P2,,PnP_1, P_2, \ldots, P_n (1PiN1 \le P_i \le N).

The third line contains a binary string SS of length NN.

Then QQ lines follow. Each of the QQ lines contains a single integer xx (1xN1 \le x \le N) denoting a query to change SxS_x to 1Sx1-S_x.

Output Format

Output QQ integers q1,q2,,qnq_1, q_2, \ldots, q_n in order on QQ lines, where qiq_i is the number of knights alive after performing a joust after the ii-th query.

5 3
2 1 3 4 3
11011
1
3
4
2
4
2
10 5
10 1 2 3 8 9 4 5 7 6
0111100110
10
5
6
5
1
5
5
3
6
1

Hint

Note

In the first sample input, after the first query, SS becomes 01011.

Initially, the (labels of) the knights alive are {1,2,3,4,5}\{1, 2, 3, 4, 5\}. After one iteration, the knights remaining are {1,3,4}\{1,3,4\}. After the second iteration, the knights remaining are {3,4}\{3, 4\}. After the third iteration, no new knight dies, so 2 knights remain after a joust.

After the second query, SS becomes 01111.

After one iteration, the knights remaining are {2,3,4,5}\{2,3,4,5\}. After the second iteration, no new knight dies, so 4 knights remain after a joust.

Scoring

Subtask 1 (6 points): N,Q500N,Q\le 500

Subtask 2 (9 points): PiPi+1P_i \leq P_{i+1} for 1i<N 1 \leq i < N

Subtask 3 (17 points): PiPjP_i \neq P_j if iji \neq j and the answer for each query is guaranteed to be at most 5050.

Subtask 4 (19 points): PiPjP_i \neq P_j if iji \neq j

Subtask 5 (19 points): N,Q10000N,Q \leq 10000

Subtask 6 (30 points): No additional constraints