Answered step by step
Verified Expert Solution
Question
1 Approved Answer
The operations that are performed in a data structure can be categorized into two types: query and update. A query operation usually answers some specific
The operations that are performed in a data structure can be categorized into two types: query and update. A query operation usually answers some specific questions about the stored data. An update usually adds data or changes the state of data in the structure. For example, adding an element or deleting the top element of a stack are update operations. While, accessing the top element is a query operation. In this problem, you are asked to design a data structure. This data structure will support two operations: Update(A) = Add an integer A to the collection Query(B) = Return the B'th smallest number from the collection. For example, if the existing numbers in the collection are (1,4,3,7,5), then performing Query(2) will return 3. If we perform Update(2) on the existing collection, then Query( 6 ) will return 7. You will have to perform 2 * N operations of this data structure. Each update operation will be immediately followed by a valid query operation. Please see the samples and explanation for further understanding. Input The first line contains an integer N (15NS105), the number of updates you have to perform for this dataset. You will also have to perform N query operations as well. Next line contains Nintegers AI (1SA1S106), where each Al indicates the number to be added for the i'th update operation Then follows another line with N numbers BI (15BISI). For each BI, you will have to print the Brith integer in the collection after the l'th update operation. Output For each query operation, print the answer on a line by itself. Samples Input Output 6 1 4 3752 1 2 24 36 1 4 3 7 4 7 Input Output 7 1 1 4 3 10 15 20 25 1 2 2 4 5 6 7 4 3 10 15 20 25 Explanation In the first sample, the following are the first few instructions that are performed in order: Update(1) : Adds 1 to the collection, so it becomes (1) Query(1): Returns the first element of the collection, which is 1. Update( 4): Adds 4 to the collection, so it becomes (1,4) Query(2): Returns the second element of the collection, which is 4. Update(3): Adds 3 to the collection, so it becomes (1,4,3) Query(2): Returns the second element of the collection, which is now 3. Similarly, the remaining instructions will be performed in the following order: Update(7), Query( 4), Update( 5 ). Query( 3 ), Update 2) and Query(6)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started