Perform given queries on Queue according to the given rules | Function to perform all the queries operations on the given queue ; Stores the count query of type 1 ; Event E1 : increase countE1 ; Event E2 : add the X in set ; Event E3 : Find position of X ; Initial position is ( position - countE1 ) ; If X is already removed or popped out ; Finding the position of X in queue ; Traverse set to decrease position of X for all the number removed in front ; Print the position of X ; Driver Code
def solve ( n , m , queries ) : NEW_LINE INDENT countE1 = 0 NEW_LINE removed = set ( ) NEW_LINE for i in range ( 0 , m ) : NEW_LINE INDENT if ( queries [ i ] [ 0 ] == 1 ) : NEW_LINE INDENT countE1 += 1 NEW_LINE DEDENT elif ( queries [ i ] [ 0 ] == 2 ) : NEW_LINE INDENT removed . add ( queries [ i ] [ 1 ] ) NEW_LINE DEDENT else : NEW_LINE INDENT position = queries [ i ] [ 1 ] - countE1 NEW_LINE if ( queries [ i ] [ 1 ] in removed or position <= 0 ) : NEW_LINE INDENT print ( " - 1" ) NEW_LINE DEDENT else : NEW_LINE INDENT for it in removed : NEW_LINE INDENT if ( it > queries [ i ] [ 1 ] ) : NEW_LINE INDENT break NEW_LINE DEDENT position -= 1 NEW_LINE DEDENT print ( position ) NEW_LINE DEDENT DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 5 NEW_LINE Q = 3 NEW_LINE queries = [ [ 0 for _ in range ( 2 ) ] for _ in range ( Q ) ] NEW_LINE queries [ 0 ] [ 0 ] = 1 NEW_LINE queries [ 0 ] [ 1 ] = 0 NEW_LINE queries [ 1 ] [ 0 ] = 3 NEW_LINE queries [ 1 ] [ 1 ] = 3 NEW_LINE queries [ 2 ] [ 0 ] = 2 NEW_LINE queries [ 2 ] [ 1 ] = 2 NEW_LINE solve ( N , Q , queries ) NEW_LINE DEDENT