Open In App

Processing three type of queries

Last Updated : 23 Aug, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

 Given Q queries in array A[][2] of three types of the form {type, X} and an empty array B[], the task for this problem is to process these queries in order.

  • Type 1: Number X will be given insert that element into array B[].
  • Type 2: Number X will be given add that number to all elements present in array B[].
  • Type 3: Erase the largest element of array B[] and print it.  

Examples:

Input: A[][2] = {{1, 3}, {1, 5}, {3, -1}, {2, 2}, {3, -1}}
Output: 5 5
Explanation: Queries are processed in following order:

  • Query – {1, 3}: This is query of type 1 it says insert element 3 in array B[]
  • Query – {1, 5}: This is query of  type 1 it says insert element 5 in array B[]
  • Query – {3, -1}: This is query of type 3. The array B[] now contains a  element of value 3 and another with 5. Pick up the ball with the largest of them that is 5 delete it from array B[].
  • Query – {2, 2}:  The array B[] now contains just a element with value 3. Replace this integer with 3 + 2 = 5.
  • Query – {3, -1}: The array B[] now contains just a element with value 5. delete this element, record 5.

Therefore, print 5 and 5, in the order they are recorded.

Input: A[][2] = {{1, 1000}, {2, 1000}, {2, 1000}, {2, 1000}, {2, 1000}, {3,  -1}}
Output: 5000

Approach: To solve the problem follow the below idea:

Priority Queue can be used to solve this problem.

Below are the steps for the above approach:

  • Create a priority queue.
  • Create variable netInc  = 0 that will keep track of the increase in elements of the array by query 2. 
  • Iterate through all queries from 0 to N – 1.
  • If the type of query is 1 then insert that element in the priority queue after subtracting it from netInc.
  • If the type of query is 2 then increase the value of netInc by the respective amount.
  • If the type of query is 3 then print the largest element of the priority queue after adding it with netInc and then delete that element.

Below is the implementation of the above approach: 

C++




// C++ code to implement the approach:
#include <bits/stdc++.h>
using namespace std;
 
// Function to process given
// queries in order
void processQueries(int A[][2], int N)
{
 
    // Multiset
    multiset<int> ms;
 
    // Variable that helps to keeps
    // track of query 2
    int netInc = 0;
 
    // Iterating for all queries
    for (int i = 0; i < N; i++) {
 
        // Type 1 query
        if (A[i][0] == 1) {
 
            // Inserting in priority queue
            ms.insert(A[i][1] - netInc);
        }
 
        // Type 2 query
        else if (A[i][0] == 2) {
 
            // Increasing value of all
            // elements from priority
            // queue by X
            netInc += A[i][1];
        }
 
        // Type 3 query
        else {
 
            // Printing largest element
            cout << *(--ms.end()) + netInc << " ";
 
            // Deleting last element
            ms.erase(--ms.end());
        }
    }
 
    // Ending on new line
    cout << endl;
}
 
// Driver Code
int32_t main()
{
 
    // Input 1
    int N = 5;
    int A[][2] = {
        { 1, 3 }, { 1, 5 }, { 3, -1 }, { 2, 2 }, { 3, -1 }
    };
 
    // Function Call
    processQueries(A, N);
 
    // Input 2
    int N1 = 6;
    int A1[][2] = { { 1, 1000 }, { 2, 1000 }, { 2, 1000 }, { 2, 1000 }, { 2, 1000 }, { 3, -1 } };
 
    // Function Call
    processQueries(A1, N1);
 
    return 0;
}


Java




import java.util.*;
 
class GFG {
    // Function to process given queries in order
    static void processQueries(int[][] A, int N) {
 
        // Multiset
        TreeSet<Integer> ms = new TreeSet<>();
 
        // Variable that helps to keep track of query 2
        int netInc = 0;
        // Nikunj Sonigara
        // Iterating for all queries
        for (int i = 0; i < N; i++) {
 
            // Type 1 query
            if (A[i][0] == 1) {
 
                // Inserting in multiset
                ms.add(A[i][1] - netInc);
            }
 
            // Type 2 query
            else if (A[i][0] == 2) {
 
                // Increasing value of all elements in the multiset by X
                netInc += A[i][1];
            }
 
            // Type 3 query
            else {
 
                // Printing largest element
                System.out.print(ms.last() + netInc + " ");
 
                // Deleting last element
                ms.remove(ms.last());
            }
        }
 
        // Ending on a new line
        System.out.println();
    }
 
    public static void main(String[] args) {
        // Input 1
        int N = 5;
        int[][] A = {
            {1, 3}, {1, 5}, {3, -1}, {2, 2}, {3, -1}
        };
 
        // Function Call
        processQueries(A, N);
 
        // Input 2
        int N1 = 6;
        int[][] A1 = {
            {1, 1000}, {2, 1000}, {2, 1000}, {2, 1000}, {2, 1000}, {3, -1}
        };
 
        // Function Call
        processQueries(A1, N1);
    }
}


Python3




from sortedcontainers import SortedList
 
def processQueries(A, N):
    # Create a SortedList data structure to store the elements
    ms = SortedList()
 
    # Variable that keeps track of the net increment
    netInc = 0
 
    # Iterate over all the queries
    for i in range(N):
        # Type 1 query
        if A[i][0] == 1:
            # Insert the element into the SortedList,
            # subtracting the net increment
            ms.add(A[i][1] - netInc)
 
        # Type 2 query
        elif A[i][0] == 2:
            # Increase the value of all elements in the
            # SortedList by X
            netInc += A[i][1]
 
        # Type 3 query
        else:
            # Print the largest element in the SortedList,
            # adding the net increment
            print(ms[-1] + netInc, end=" ")
 
            # Delete the last element from the SortedList
            ms.remove(ms[-1])
 
    # Print a new line after processing all queries
    print()
 
# Driver Code
if __name__ == '__main__':
    # Input 1
    N = 5
    A = [
        [1, 3], [1, 5], [3, -1], [2, 2], [3, -1]
    ]
 
    # Function Call
    processQueries(A, N)
 
    # Input 2
    N1 = 6
    A1 = [
        [1, 1000], [2, 1000], [2, 1000], [2, 1000], [2, 1000], [3, -1]
    ]
 
    # Function Call
    processQueries(A1, N1)


C#




using System;
using System.Collections.Generic;
 
public class GFG {
    // Function to process given
    // queries in order
    public static void ProcessQueries(int[, ] A, int N)
    {
 
        // Multiset
        SortedSet<int> ms = new SortedSet<int>();
 
        // Variable that helps to keeps
        // track of query 2
        int netInc = 0;
 
        // Iterating for all queries
        for (int i = 0; i < N; i++) {
 
            // Type 1 query
            if (A[i, 0] == 1) {
 
                // Inserting in sorted set
                ms.Add(A[i, 1] - netInc);
            }
 
            // Type 2 query
            else if (A[i, 0] == 2) {
 
                // Increasing value of all
                // elements from sorted set
                // by X
                netInc += A[i, 1];
            }
 
            // Type 3 query
            else {
 
                // Printing largest element
                Console.Write(ms.Max + netInc + " ");
 
                // Deleting last element
                ms.Remove(ms.Max);
            }
        }
 
        // Ending on new line
        Console.WriteLine();
    }
 
    // Driver Code
    public static void Main(string[] args)
    {
 
        // Input 1
        int N = 5;
        int[, ] A = { { 1, 3 },
                      { 1, 5 },
                      { 3, -1 },
                      { 2, 2 },
                      { 3, -1 } };
 
        // Function Call
        ProcessQueries(A, N);
 
        // Input 2
        int N1 = 6;
        int[, ] A1
            = { { 1, 1000 }, { 2, 1000 }, { 2, 1000 },
                { 2, 1000 }, { 2, 1000 }, { 3, -1 } };
 
        // Function Call
        ProcessQueries(A1, N1);
    }
}


Javascript




// Function to process given
// queries in order
function processQueries(A, N) {
    // Multiset
    let ms = new Set();
    // Variable that helps to keep
    // track of query 2
    let netInc = 0;
 
    // Iterating for all queries
    for (let i = 0; i < N; i++) {
        // Type 1 query
        if (A[i][0] === 1) {
            // Inserting in multiset
            ms.add(A[i][1] - netInc);
        }
        // Type 2 query
        else if (A[i][0] === 2) {
            // Increasing value of all
            // elements from multiset by X
            netInc += A[i][1];
        }
        // Type 3 query
        else {
            // Printing largest element
            let largest = Array.from(ms).reduce((max, val) => val > max ? val : max, -Infinity) + netInc;
            console.log(largest + ' ');
 
            // Deleting last element
            let lastElement = Math.max(...ms);
            ms.delete(lastElement);
        }
    }
    // Ending on new line
    console.log('');
}
 
// Driver Code
 
    // Input 1
    let N = 5;
    let A = [
        [1, 3], [1, 5], [3, -1], [2, 2], [3, -1]
    ];
 
    // Function Call
    processQueries(A, N);
 
    // Input 2
    let N1 = 6;
    let A1 = [
        [1, 1000], [2, 1000], [2, 1000], [2, 1000], [2, 1000], [3, -1]
    ];
 
    // Function Call
    processQueries(A1, N1);


Output

5 5 
5000 







Time Complexity: O(N*logN)  
Auxiliary Space: O(N)

Related Articles:



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads