Problem statement is from CSES
This is a standard question where we are given a list of numbers and a number of queries. For each query, you have to give the sum of numbers in the given range.
My implementation in python:
def solve(): n,q = (int(a) for a in input().split(' ')) arr = (int(a) for a in input().split(' ')) cumulativeArray = (0) # Initial entry added to make 1 based indexing sum = 0 for i in range(len(arr)): sum += arr(i) cumulativeArray.append(sum) for i in range(q): x,y = (int(a) for a in input().split(' ')) print(cumulativeArray(y) - cumulativeArray(x-1)) solve()
This gives the required answers correctly for all my custom test cases. But gives TLE for huge test cases.
I believe this is happening due to
split() method in my code.
Any leads are welcome.