1476 子矩形查詢

2022-05-13 08:40:01 字數 3151 閱讀 4703

請你實現乙個類 subrectanglequeries ,它的建構函式的引數是乙個 rows x cols 的矩形(這裡用整數矩陣表示),並支援以下兩種操作:

1. updatesubrectangle(int row1, int col1, int row2, int col2, int newvalue)

用 newvalue 更新以 (row1,col1) 為左上角且以 (row2,col2) 為右下角的子矩形。

2. getvalue(int row, int col)

返回矩形中座標 (row,col) 的當前值。

示例 1:

輸入:["subrectanglequeries","getvalue","updatesubrectangle","getvalue","getvalue","updatesubrectangle","getvalue","getvalue"]

[[[[1,2,1],[4,3,4],[3,2,1],[1,1,1]]],[0,2],[0,0,3,2,5],[0,2],[3,1],[3,0,3,2,10],[3,1],[0,2]]

輸出:[null,1,null,5,5,null,10,5]

解釋:subrectanglequeries subrectanglequeries = new subrectanglequeries([[1,2,1],[4,3,4],[3,2,1],[1,1,1]]);

// 初始的 (4x3) 矩形如下:

// 1 2 1

// 4 3 4

// 3 2 1

// 1 1 1

subrectanglequeries.getvalue(0, 2); // 返回 1

subrectanglequeries.updatesubrectangle(0, 0, 3, 2, 5);

// 此次更新後矩形變為:

// 5 5 5

// 5 5 5

// 5 5 5

// 5 5 5

subrectanglequeries.getvalue(0, 2); // 返回 5

subrectanglequeries.getvalue(3, 1); // 返回 5

subrectanglequeries.updatesubrectangle(3, 0, 3, 2, 10);

// 此次更新後矩形變為:

// 5 5 5

// 5 5 5

// 5 5 5

// 10 10 10

subrectanglequeries.getvalue(3, 1); // 返回 10

subrectanglequeries.getvalue(0, 2); // 返回 5

示例 2:

輸入:["subrectanglequeries","getvalue","updatesubrectangle","getvalue","getvalue","updatesubrectangle","getvalue"]

[[[[1,1,1],[2,2,2],[3,3,3]]],[0,0],[0,0,2,2,100],[0,0],[2,2],[1,1,2,2,20],[2,2]]

輸出:[null,1,null,100,100,null,20]

解釋:subrectanglequeries subrectanglequeries = new subrectanglequeries([[1,1,1],[2,2,2],[3,3,3]]);

subrectanglequeries.getvalue(0, 0); // 返回 1

subrectanglequeries.updatesubrectangle(0, 0, 2, 2, 100);

subrectanglequeries.getvalue(0, 0); // 返回 100

subrectanglequeries.getvalue(2, 2); // 返回 100

subrectanglequeries.updatesubrectangle(1, 1, 2, 2, 20);

subrectanglequeries.getvalue(2, 2); // 返回 20

最多有 500 次updatesubrectangle 和 getvalue 操作。

1 <= rows, cols <= 100

rows == rectangle.length

cols == rectangle[i].length

0 <= row1 <= row2 < rows

0 <= col1 <= col2 < cols

1 <= newvalue, rectangle[i][j] <= 10^9

0 <= row < rows

0 <= col < cols

class

subrectanglequeries:

def__init__

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

self.data=rectangle

self.update=

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

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

res=none

for i in range(len(self.update)-1,-1,-1):

row1,col1,row2,col2,value=self.update[i]

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

res=value

break

return res if res else

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)

1476 子矩形查詢 2020 9 23

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

陣列 1476 子矩形查詢

1.暴力法 class subrectanglequeries object def init self,rectangle type rectangle list list int self.data rectangle def updatesubrectangle self,row1,col1,...

hihocoder 1476 矩形計數

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