#P13864. [SWERC 2020] Figurines

[SWERC 2020] Figurines

题目描述

:::align{center}

:::

Bob has a lot of mini figurines. He likes to display some of them on a shelf above his computer screen and he likes to regularly change which figurines appear. This ever-changing decoration is really enjoyable. Bob takes care of never adding the same mini figurine more than once. Bob has only NN mini figurines and after NN days he arrives at the point where each of the NN figurines have been added and then removed from the shelf (which is thus empty).

Bob has a very good memory. He is able to remember which mini figurines were displayed on each of the past days. So Bob wants to run a little mental exercise to test its memory and computation ability. For this purpose, Bob numbers his figurines with the numbers 0,,N10, \dots, N-1 and selects a sequence of NN integers d0dN1d_0 \dots d_{N-1} all in the range [0;N][0;N]. Then, Bob computes a sequence x0,,xNx_0,\dots, x_N in the following way: x0=0x_0=0 and xi+1=(xi+yi) mod Nx_{i+1}=(x_i+y_i)\text{ mod } N where mod\text{mod} is the modulo operation and yiy_i is the number of figurines displayed on day did_i that have a number higher or equal to xix_i. The result of Bob's computation is xNx_N.

More formally, if we note S(i)S(i) the subset of {0,,N1}\{0,\dots,N-1\} corresponding to figurines displayed on the shelf on day ii, we have:

  • S(0)S(0) is the empty set;
  • S(i)S(i) is obtained from S(i1)S(i-1) by inserting and removing some elements.

Each element 0j<N0 \le j < N is inserted and removed exactly once and thus, the last set S(N)S(N) is also the empty set. The computation that Bob performs corresponds to the following program:

$$\begin{array}{l} x_0 \leftarrow 0 \\ \text{for } i \in [0;N-1] \\ \;\;\;\;\;\;\; x_{i+1} \leftarrow (x_i + \#\{y \in S(d_i) ~\text{ such that } ~ y \ge x_i\}) \mod N \\ \text{output } x_N \end{array} $$

Bob asks you to verify his computation. For that he gives you the numbers he used during its computation (the d0,,dN1d_0, \dots, d_{N-1}) as well as the log of which figurines he added or removed every day. Note that a mini figurine added on day ii and removed on day jj is present on a day kk when ik<ji\leq k < j. You should tell him the number that you found at the end of the computation.

输入格式

The input is composed of 2N+12N+1 lines.

  • The first line contains the integer NN.
  • Lines 22 to N+1N+1 describe the figurines added and removed. Line i+1i+1 contains space-separated +j+j or j-j, with 0j<N0 \le j < N, to indicate that jj is added or removed on day ii. This line may be empty. A line may contain both +j+j and j-j, in that order.
  • Lines N+2N+2 to 2N+12N+1 describe the sequence d0,,dN1d_0,\dots, d_{N-1}. Line N+2+iN+2+i contains the integer did_i with 0diN0 \le d_i \le N.

Limits

  • 1N1000001 \le N \le 100\,000

输出格式

The output should contain a single line with a single integer which is xNx_N.

3
+0 +2
-0 +1
-1 -2
1
2
2
2

提示

Sample Explanation

The output is 22 since

  • first, x2x \leftarrow 2 since S(1)={0,2}S(1) = \{ 0, 2 \} and #{yS(1) such that y0}=2\#\{y \in S(1) ~\text{such that}~ y \ge 0\} = 2;
  • then, x0x \leftarrow 0 since S(2)={1,2}S(2) = \{ 1, 2 \} and #{yS(2) such that y2}=1\#\{y \in S(2) ~\text{such that}~ y \ge 2\} = 1;
  • and finally, x2x \leftarrow 2 since S(2)={1,2}S(2) = \{ 1, 2 \} and #{yS(2) such that y0}=2\#\{y \in S(2) ~\text{such that}~ y \ge 0\} = 2.