• + 1 comment

    The code below solves the first four test cases but timeout for the rest. Any idea about improving the speed?

        for i in range(q):
            operation=list(map(int, input().rstrip().split()))
            if operation[0]==3:
                print(min(box[operation[1]:operation[2]+1]))
            elif operation[0]==4:
                print(sum(box[operation[1]:operation[2]+1]))
            else:
                for z in range (operation[1], operation[2]+1):
                    if operation[0]==1:
                        box[z]+=operation[3] 
                    else:
                        box[z]=math.floor(box[z]/operation[3])
    
    • + 0 comments
      def boxops(box, oplist):
      for op in oplist:
          if len(op)==4:
              num, l, r, a = op
              if num == 1:box = [item+a if ind in range(l, r+1) else item for ind, item in enumerate(box)]
      
              else:
      
                  box = [math.floor(item/a) if ind in range(l,r+1) else item for ind, item in enumerate(box)]
      
          else:
              num, l, r = op
              if num == 3:
                  print(min(box[l:r+1]))
      
              else:
                  print(sum(box[l:r+1]))
      if __name__ == '__main__':
      nq = input().split()
      
      n = int(nq[0])
      
      q = int(nq[1])
      
      box = list(map(int, input().rstrip().split()))
      oplist = []
      for _ in range(q):
          oplist.append(list(map(int, input().strip().split())))
      boxops(box, oplist)
      

      mine timesout after testcase 4 too.. any idea how to solve it?