Convoluted Operations

2.9

7 votes
Approved, Data Structures, Data compression, Easy, Fenwick Tree, Fenwick tree
Problem

Mishki is quite interested in playing games, and recently she found an empty stack. Now she wants to perform 3 types of operations on the stack:

1 1A : push element A in the stack.
2 0 : pop one element from stack.
3 2KX : find how many elements were less than X present in the stack, after performing Kth operation on the stack.

Can you help her in performing the above operations ?

Input:
The first line contains an integer N, denoting the number of operations.
Next N line contains, any of the 3 types of operations mentioned above, where ith line contains the ith operation.

Output
Print the required answer for each of the 3rd type of operation, in new line.

Constraints:
1N5×105
0A,X109
1KN

Notes:
1) No pop operation will be given for an empty stack.
2) Let's say, you are performing ith operation on the stack and it is of 3rd type, then the given K will always be less than i.
3) There will be at least one 3rd type of operation, in the input.

Sample Input
7
1 9
1 1
2 2 10    
1 2
2 4 5
0
2 6 5
Sample Output
2
2
1
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

enter image description here

In 3rd operation you need to find the values present in the stack which were less than 10 , after performing 2nd operation. So the answer is 2.

enter image description here

In 5th operation you need to find the values present in the stack which were less than 5 , after performing 4th operation. So the answer is 2.

enter image description here

In 7th operation you need to find the values present in the stack which were less than 5 , after performing 6th operation. So the answer is 1.

Editor Image

?