### Zlatan's blog

By Zlatan, history, 8 months ago, ,

Hi! I encounter difficulties in finding a solution to this problem: having a matrix filled with 0 and 1, size N x N with N <= 1000, output the number of squares with the border filled with 1 (we are not interested in what's inside the square). We consider valid even the squares of size 1x1. For this example, the answer is 27. Can anybody help me? (sorry for the example, I don't know how to show the lines one above the others) 0000000 0111100 0101111 0100101 0111111 0000011 0000011

• +4

 » 8 months ago, # |   0 I think a 2D BIT should be enough to solve this task.
•  » » 8 months ago, # ^ |   +5 Can you tell me more detalis how?
•  » » » 8 months ago, # ^ |   0 i m sorry i got the wrong idea from the task ... i ll think about it more.
 » 8 months ago, # |   0 The best I have is O(n3) solution with constant. I think it can pass if the time limit is two seconds (maybe even one second with powerful judge).
•  » » 8 months ago, # ^ |   +5 The time limit is 0.15 seconds
 » 8 months ago, # |   +8 Solve each diagonal separately. You need to use 2D-segment tree or 2D-BIT for O(n*n*log(n)*log(n)) complexity or persistent segment tree for O(n*n*log(n)) complexity.