Perform given queries on Queue according to the given rules | C ++ program for the above approach ; 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; void solve ( int n , int m , int * * queries ) { int countE1 = 0 ; set < int > removed ; for ( int i = 0 ; i < m ; i ++ ) { if ( queries [ i ] [ 0 ] == 1 ) ++ countE1 ; else if ( queries [ i ] [ 0 ] == 2 ) removed . insert ( queries [ i ] [ 1 ] ) ; else { int position = queries [ i ] [ 1 ] - countE1 ; if ( removed . find ( queries [ i ] [ 1 ] ) != removed . end ( ) position <= 0 ) cout << " - 1 STRNEWLINE " ; else { for ( int it : removed ) { if ( it > queries [ i ] [ 1 ] ) break ; position -- ; } cout << position << " STRNEWLINE " ; } } } } int main ( ) { int N = 5 , Q = 3 ; int * * queries = new int * [ Q ] ; for ( int i = 0 ; i < Q ; i ++ ) { queries [ i ] = new int [ 2 ] ; } queries [ 0 ] [ 0 ] = 1 ; queries [ 0 ] [ 1 ] = 0 ; queries [ 1 ] [ 0 ] = 3 ; queries [ 1 ] [ 1 ] = 3 ; queries [ 2 ] [ 0 ] = 2 ; queries [ 2 ] [ 1 ] = 2 ; solve ( N , Q , queries ) ; return 0 ; }