Clothes Arrangement
Practice
3.6 (7 votes)
Advanced data structures
Bit manipulation
Data structures
Medium
Offline processing
Segment trees
Problem
24% Success 1169 Attempts 50 Points 2s Time Limit 256MB Memory 1024 KB Max Code

There is an arrangement of clothes in the form of a stack. There are N clothes with you and each cloth has a color Col[i] associated with it.1st cloth is at the bottom and Nth cloth at the top.

Now you have to answer Q queries:

Each query can be of 2 types:

Type-1 query indicated that you place a cloth of color C on top of the stack.

Type 2 query gives you a color C a number K which means you need to pick the Kth cloth from top having color C.If its not possible print -1.If you find the Kth cloth then you take that cloth out of the stack and put all other clothes back in their original order.For type 2 query if you get a cloth then print the number of clothes you had to pop from the stack before you see your desired cloth.

Input Format:

First line contains an integer N containing the initial number of clothes.

Next line contains N space separated integers denoting the colors of the ith Cloth.

Next line contains an integer Q.

Each query has a type (ty) .

For type 1 query you get another integer C.

For type 2 query you get 2 integers C and K.

Output Format

Print the answers for the type 2 queries in new line

Constraints

\(1 \le N,Q \le 5*10^5\)

\(1 \le C,Col[i] \le 10^5 \)

\(1 \le K \le 10^6\)

\(1 \le ty \le 2\)

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:50
10 votes
Tags:
Advanced Data StructuresData StructuresMediumSegment Trees
Points:50
3 votes
Tags:
Advanced Data StructuresSegment TreesBasics of ImplementationData StructuresImplementationSegment treeMath
Points:50
Tags:
ApprovedData StructuresMatrixMediumSegment Trees