rects ← 長方形の列 # 長方形を重ねる for rect in rects: x1 = rect.左上の頂点.x y1 = rect.左上の頂点.y x2 = rect.右下の頂点.x y2 = rect.右下の頂点.y A[x1][y1]++ A[x2][y2]++ A[x1][y2]-- A[x2][y1]-- # 水平方向の累積和 for x ← 1 to N-1: for y ← 0 to M-1: A[x][y] ← A[x][y] + A[x-1][y] # 垂直方向の累積和 for y ← 1 to M-1: for x ← 0 to N-1: A[x][y] ← A[x][y] + A[x][y-1]