陣列 1476 子矩形查詢

2021-10-11 01:31:31 字數 1933 閱讀 2936

1.暴力法

class subrectanglequeries(object):

def __init__(self, rectangle):

""" :type rectangle: list[list[int]]

""" self.data=rectangle

def updatesubrectangle(self, row1, col1, row2, col2, newvalue):

""" :type row1: int

:type col1: int

:type row2: int

:type col2: int

:type newvalue: int

:rtype: none

"""for i in range(row1,row2+1):

for j in range(col1,col2+1):

self.data[i]

[j]=newvalue

def getvalue(self, row, col):

""" :type row: int

:type col: int

:rtype: int

"""return self.data[row]

[col]

# your subrectanglequeries object will be instantiated and called as such:

# obj = subrectanglequeries(rectangle)

# obj.updatesubrectangle(row1,col1,row2,col2,newvalue)

# param_2 = obj.getvalue(row,col)

2.陣列變numpy形式的陣列,利用numpy的切片賦值,一行語句完成賦值

3.儲存更新值:不改變原陣列中的資料,以空間換時間

class subrectanglequeries:

def __init__(self, rectangle: list[list[int]]):

self.rectangle = rectangle

self.hash_map =

def updatesubrectangle(self, row1: int, col1: int, row2: int, col2: int, newvalue: int) -> none:

self.hash_map[

(row1, col1, row2, col2)

]= newvalue

def getvalue(self, row: int, col: int) -> int:

result = self.rectangle[row]

[col]

for row1, col1, row2, col2 in self.hash_map.keys(

): if row1 <= row <= row2 and col1 <= col <= col2:

result = self.hash_map[

(row1, col1, row2, col2)

]return result

# your subrectanglequeries object will be instantiated and called as such:

# obj = subrectanglequeries(rectangle)

# obj.updatesubrectangle(row1,col1,row2,col2,newvalue)

# param_2 = obj.getvalue(row,col)

還可以用列表來代替雜湊表,列表中可以是有序的,找到之後可以直接退出迴圈避免不必要的迭代

1476 子矩形查詢

請你實現乙個類 subrectanglequeries 它的建構函式的引數是乙個 rows x cols 的矩形 這裡用整數矩陣表示 並支援以下兩種操作 1.updatesubrectangle int row1,int col1,int row2,int col2,int newvalue 用 n...

1476 子矩形查詢 2020 9 23

請你實現乙個類 subrectanglequeries 它的建構函式的引數是乙個 rows x cols 的矩形 這裡用整數矩陣表示 並支援以下兩種操作 updatesubrectangle int row1,int col1,int row2,int col2,int newvalue 用 new...

hihocoder 1476 矩形計數

顯然就是容斥原理了。先算出所有的矩陣一共有多少個 ll sum n n 1 2 m m 1 2 然後考慮對於任取x個黑色方框,他們組成乙個新的矩形,然後計算有多少個矩陣會覆蓋整個矩形,也即,兩條邊所夾住的對頂兩個小正方形的所有點的乘積 奇減偶加即可 include using namespace s...