report VI VI EN
Register | Login
  • HOME
  • PROBLEMSET
  • ROADMAP
  • COMPETITION
  • TOPIC
  • RANKING
  • GUIDE
  • MASHUP
  • ABOUT
  • CONTACT
  • Problem
  • Submit
  • Results
Manhattan distance - MarisaOJ: Marisa Online Judge

Manhattan distance

Time limit: 1000 ms
Memory limit: 256 MB
Given an 2D array $A$ having $n$ rows and $n$ columns and each cell $(i, j)$ has a value. For each cell $(i, j)$, calculate the sum of all cells having distance less or equal to $k$ to it. The distance between cell $(a, b)$ and cell $(c, d)$ is calculated as following: $$|a-c|+|b-d|$$ ### Input - The first line contains 2 integers $n, k$. - Next $n$ lines, each lines contains $n$ integers $A_{i, j}$. ### Output - Print $n$ lines, each line contains $n$ integers, the integer on row $i$ and column $j$ is the answer of $A_{i, j}$. ### Constraints - $1 \le n, k \le 1000$. - $1 \le A_{i, j} \le 1000$. ### Example Input: ``` 3 1 1 0 2 0 0 1 1 0 0 ``` Output: ``` 1 3 3 2 1 3 1 1 1 ```
Introduction to Prefix sum
Prefix sum
Maximum subarray sum
Balance substring
Divisible by d
Subarray sum
Game on array
2D prefix sum
Stair query
Maximum sum subarray 2
Average
Ratio Substrings
Maximum submatrix sum
Maximum subarray sum 3
Manhattan distance
Minimum distance
Topic
Prefix sum
Rating 1400
Solution (1) Solution