Python是IO慢还是运算慢?为什么 3e5 次 int 类型读入,加上极小常数线性的动态规划就超了 2s。
还能不能优化了?
from itertools import accumulate
n, q = map(int, input().split())
a = [0] + list(map(int, input().split()))
sum = list(accumulate(a))
pre = [0, ]
for i in range(1, n):
pre.append(max(pre[i - 1] + a[i], a[i]))
suf = [0 for i in range(n + 2)]
for i in range(n, 0, -1):
suf[i] = max(suf[i + 1] + a[i], a[i])
# for x in suf:
# print(x, end=' ')
for i in range(q):
L, R = map(int, input().split())
ans = sum[R - 1] - sum[L] + pre[L] + suf[R]
print(ans)