我想添加一個常量num_to_add給列表I中的每個列表元素,在一個拼接l [i:j(including)]中我給了很多表單的查詢[i,j,num_to_add]。Python - 添加到列表多次拼接,優化,算法
其中t的格式爲[[i,j,num_to_add],[i,j,num_to_add]等] 我將t的每個元素添加到完全由零構成的列表I中。
我當前的代碼是這樣的: 我循環遍歷t中的子列表,我的查詢,並將num_to_add添加到每個列表拼接元素。我輸出列表I中小於整數n的元素數量。
我怎樣才能從嵌套for循環優化這個?這是針對可能適用於其他項目的一小部分問題。
EDIT 4: 樣品輸入:
4
1
3
1 3 1
2 3 2
3 3 2
EDIT 5: 全碼:
I = [0] * int(input())
n = int(input())
j = int(input())
t = [list(map(int, input().split())) for i in range(j)]
tree = []
for i in range(1, 2*len(I)+1):
tree.append([0,0])
def update(pos , left , right , i , j , val):
####updating segment tree to add value val in list l from index i to j
i=max(i , left)
j=min(j , right)
if i > j:
return
if i==left and j==right:
tree[pos][0] = tree[pos][0] + val*(j - i + 1)
tree[pos][1] = tree[pos][1] + val
return
mid = (left + right)/2
### the range breaks down into two parts left to mid and mid+1 to right
### at positions 2*pos and 2*pos+1 in tree respectively
update(2*pos , left , mid , i , j , val)
update(2*pos + 1 , mid+1 , right , i , j , val)
tree[pos][0] = tree[2*pos][0] + tree[2*pos + 1][0] + tree[pos][1]*(right - left +1)
def getvalue(pos , left , right , i , j):
###gets sum of elements in list from index i to j in our case
### i will be equal to j , will see below
i = max(i , left)
j= min(j , right)
if i > j:
return 0
if i==left and j==right:
return tree[pos][0]
mid = (left + right)/2
return getvalue(2*pos , left , mid , i , j) + getvalue(2*pos+1 , mid+1 , right , i , j) + tree[pos][1]*(j - i + 1)
###Remember l is 1 based indexed
for i in range(len(t)):
update(1 , 1 , len(I) , t[i][0] , t[i][1] , t[i][2])
ans = 0
###Remember l is 1 based indexed
for i in range(1 , len(I)+1):
###see we only use getvalue where i and j parameters of getvalue are same
value = getvalue(1 , 1 , len(I) , i , i)
if value < n:
ans = ans + 1
print(ans)
錯誤:
Traceback (most recent call last):
File, line 58, in <module>
value = getvalue(1 , 1 , len(I) , i , i)
# A lot of these:
File "/home/max3/Documents/Python/DMOJ TCE Battle Positions.py", line 47, in getvalue
return getvalue(2*pos , left , mid , i , j) + getvalue(2*pos+1 , mid+1 , right , i , j) + tree[pos][1]*(j - i + 1)
# And one of this.
File "/home/max3/Documents/Python/DMOJ TCE Battle Positions.py", line 44, in getvalue
return tree[pos][0]
IndexError: list index out of range
是否num_to_add在所有查詢中保持不變? – sashas
你應該查找段樹或二叉樹索引,你會發現很多類似的問題在stackoverflow本身 – sashas
不,它是有區別的某些查詢。你有任何特定的鏈接? – bobhob314