Skip to main content
Back to problems
#1476
Medium Algorithms

Subrectangle queries

Array Design Matrix
86.3% acceptance
Feb 23, 2026
667
1461
Implement the class SubrectangleQueries which receives a rows x cols rectangle as a matrix of integers in the constructor and supports two methods: 1. updateSubrectangle(int row1, int col1, int row2, int col2, int newValue) Updates all values with newValue in the subrectangle whose upper left coordinate is (row1,col1) and bottom right coordinate is (row2,col2). 2. getValue(int row, int col) Returns the current value of the coordinate (row,col) from the rectangle.

Solution

Rust
Time O(n²)
Space O(1)
LeetCode
solution.rs
pub struct SubrectangleQueries {
  rect: Vec<Vec<i32>>,
}

impl SubrectangleQueries {
  pub fn new(rectangle: Vec<Vec<i32>>) -> Self {
    SubrectangleQueries { rect: rectangle }
  }

  pub fn update_subrectangle(&mut self, row1: i32, col1: i32, row2: i32, col2: i32, new_value: i32) {
    for r in row1..=row2 {
      for c in col1..=col2 {
        self.rect[r as usize][c as usize] = new_value;
      }
    }
  }

  pub fn get_value(&self, row: i32, col: i32) -> i32 {
    self.rect[row as usize][col as usize]
  }
}