human
Denis was very sad after Nastya rejected him. So he decided to walk through the gateways to have some fun. And luck smiled at him! When he entered the first courtyard, he met a strange man who was selling something. 

Denis bought a mysterious item and it was... Random permutation generator! Denis could not believed his luck.

When he arrived home, he began to study how his generator works and learned the algorithm. The process of generating a permutation consists of n steps. At the i-th step, a place is chosen for the number i (1 ≤ i ≤ n). The position for the number i is defined as follows:

  * For all j from 1 to n, we calculate r_j — the minimum index such that j ≤ r_j ≤ n, and the position r_j is not yet occupied in the permutation. If there are no such positions, then we assume that the value of r_j is not defined. 
  * For all t from 1 to n, we calculate count_t — the number of positions 1 ≤ j ≤ n such that r_j is defined and r_j = t. 
  * Consider the positions that are still not occupied by permutation and among those we consider the positions for which the value in the count array is maximum. 
  * The generator selects one of these positions for the number i. The generator can choose any position. 


Let's have a look at the operation of the algorithm in the following example:


Let n = 5 and the algorithm has already arranged the numbers 1, 2, 3 in the permutation. Consider how the generator will choose a position for the number 4:

  * The values of r will be r = [3, 3, 3, 4, ×], where × means an indefinite value. 
  * Then the count values will be count = [0, 0, 3, 1, 0]. 
  * There are only two unoccupied positions in the permutation: 3 and 4. The value in the count array for position 3 is 3, for position 4 it is 1. 
  * The maximum value is reached only for position 3, so the algorithm will uniquely select this position for number 4. 


Satisfied with his purchase, Denis went home. For several days without a break, he generated permutations. He believes that he can come up with...
Solve the task in PYTHON3.
assistant
#Here is Python program to calculate r_j — the minimum index such that j ≤ r_j ≤ n, and the position r_j is not yet occupied in the permutation.

# @author 

import sys
class CNastyaAndStrangeGenerator:
    def solve(self, tc=0):

        from sys import maxsize
        INT_MIN = -maxsize

        def construct_segment_tree(a: list, n: int):
            # assign values to leaves of the segment tree
            for i in range(n):
                segtree[n + i] = a[i]

                # assign values to internal nodes
            # to compute maximum in a given range */
            for i in range(n - 1, 0, -1):
                segtree[i] = max(segtree[2 * i], segtree[2 * i + 1])

        def update(pos: int, value: int, n: int):
            # change the index to leaf node first
            pos += n

            # update the value at the leaf node
            # at the exact index
            segtree[pos] = value

            while pos > 1:
                # move up one level at a time in the tree
                pos //= 2

                # update the values in the nodes in
                # the next higher level
                segtree[pos] = max(segtree[2 * pos], segtree[2 * pos + 1])

        def range_query(left: int, right: int, n: int) -> int:
            # Basically the left and right indices will move
            # towards right and left respectively and with
            # every each next higher level and compute the
            # maximum at each height.
            # change the index to leaf node first
            left += n
            right += n

            # initialize maximum to a very low value
            ma = INT_MIN
            while left < right:

                # if left index in odd
                if left & 1:
                    ma = max(ma, segtree[left])

                    # make left index even
                    left += 1

                # if right index in odd
                if right & 1:
                    # make right index even
                    right -= 1

                    ma = max(ma, segtree[right])

                    # move to the next higher level
                left //= 2
                right //= 2
     ...