0

The question states that we are given an empty list and 10^5 queries.For each query, we have two integers a and b.a can only be 1, 2 or 3.

if a=1, then append element b in the list.
if a=2, then add value b to every element of the list.
if a=3, then we have to print the bth smallest element in the list.
e.g. query = 5, list = []
q1 -> 1 2 -> list = [2]
q2 -> 1 4 -> list = [2,4]
q3 -> 2 5 -> list = [7,9]
q4 -> 3 1 -> print(7)

I have tried a brute force approach, which takes O(N logN) for each query. How can I improve this for large queries? My brute force link

Madfish
  • 85
  • 6

1 Answers1

0
elif a==2:
    for j in range(len(l)):
         l[j]+=b

This step can be avoided and by doing the following if a == 2 sum += b #value ToBe Added to every element at the end of all queries at once Last


l=[]
query =10**5
sum = 0
for _ in range(query):
    if a==1:
        l.append(b)
    elif a==2:
        sum += b
    else:
        l.sort()
        print(l[b-1] + **sum**)

This can be further improved as below


l=[]
query =10**5
sum = 0
for _ in range(query):
    if a==1:
        // Place the element b in proper place O(n) operation increasing order
    elif a==2:
        sum += b
    else:
        print(l[b-1] + **sum**)