How to reduce the execution time of this program (Python)?

There is one uninteresting task, which I kind of managed to solve (the conditions below, the code in the end).

Put and multiply.
Time limit 2 seconds
Memory limit 64Mb
Input standard input or input.txt
Output standard output or output.txt
You are given two arrays of same lengths A and B.
You should perform 3 types of queries:
“* l r x”: add integer x to all Ai where l≤i≤r.
“. l r x”: add integer x to all Bi, where l≤i≤r.
“? l r”: compute the sum Al⋅Bl + ... + Ar⋅Br.
Arrays are numbered starting with one. Initially both arrays are filled with zeros.
The input format
The first input line contains two integers n and m separated with a space — length of arrays and the number of queries, respectively (1≤n,m≤100000).
Following m contain queries in the format described in the problem statement. For each query 1≤l≤r≤n and 1≤x<10**9+7.
The format of the output
For each query of the third type, print the remainder from dividing the corresponding amount to the 10**9 + 7.
5 4
* 1 4 10
. 2 5 8
? 1 3
? 2 5
All like anything, but one of tectoy gives TL. Work - 2,081 C. instead of 2 with..
I would like to redo the program so that this error did not occur. Code of my solution:
# Functions
def func_1(l,r,x,S):
 for i in range(l-1,r):
 S[i] += x
 return S
def func_2(l,r,A,B):
 s = 0
 for i in range(l-1,r):
 s += A[i] * B[i]
 return s % (10**9 + 7)
# End of functions
In_0 = list(map(int,input().split()))
L = In_0[0]
N = In_0[1]
A = [0] * L
B = [0] * L
for i in range(N):
 In = list(input().split())
 if In[0] == '*':
 A = func_1(int(In[1]),int(In[2]),int(In[3]),A)
 elif In[0] == '.':
 B = func_1(int(In[1]),int(In[2]),int(In[3]),B)
 elif In[0] == '?':
June 10th 19 at 14:30
3 answers
June 10th 19 at 14:32
It is of course all fine, but:
There are no functions that can be specified using lambda.
The function zip combines the list, there is no need.
- The map function performs operations with list items, either in the slice, but in the 2nd
the option function will return the same slice. Would make sense to add to the slice to
full source list if you can just use another function (func_1 in my case)? - torrance commented on June 10th 19 at 14:35
there is a pythonic way, and there are paapa Gee,
paape much easier, sometimes so much so that well, it nafig a Python

sum([a*b for a,b in zip(lista,listb)])
- this is your third question, for example - Juanita17 commented on June 10th 19 at 14:38
June 10th 19 at 14:34
No need to reassign A and B every time, just submit to feature and to change there.
Altered, still gives time limit.
# instead of an assignment
- torrance commented on June 10th 19 at 14:37
return also removed? - Juanita17 commented on June 10th 19 at 14:40
In func_1 Yes - torrance commented on June 10th 19 at 14:43
June 10th 19 at 14:36
* If python version 2.X replace all range to xrange.
* "return S" in func_1 does not make sense, because the link to the sheet is not changed.
* can try numpy, migrate set of customization possibilities is there, ready f-AI for this, will work much faster or choose pypy if possible.
> If Python ...
Another version
> "return S" in func_1 does not make sense ...
Do not quite understand. If you do not make sense, then how else?
> you can try numpy ...
It is impossible, the program is tested on Yandex.Contest. - torrance commented on June 10th 19 at 14:39
, Query test case if you will not, generate samostoyatelno great, something to test optimizations.
Can try lazy computation will be much faster for certain cases. - Juanita17 commented on June 10th 19 at 14:42

Find more questions by tags Python