Quadtree Encoding
A 2^N x 2^N black-and-white image is encoded via a quadtree: Entirely black region: output 10 (2 bits). Entirely white region: output 11 (2 bits). Mixed region: output 0 followed by encodings of fo...
Problem Statement
This archive keeps the full statement, math, and original media on the page.
The quadtree encoding allows us to describe a $2^N \times 2^N$ black and white image as a sequence of bits (0 and 1). Those sequences are to be read from left to right like this:
the first bit deals with the complete $2^N \times 2^N$ region;
"0" denotes a split:
the current $2^n \times 2^n$ region is divided into $4$ sub-regions of dimension $2^{n - 1} \times 2^{n - 1}$,
the next bits contains the description of the top left, top right, bottom left and bottom right sub-regions - in that order;
"10" indicates that the current region contains only black pixels;
"11" indicates that the current region contains only white pixels.
Consider the following $4 \times 4$ image (colored marks denote places where a split can occur):
Consider the following $4 \times 4$ image (colored marks denote places where a split can occur):

This image can be described by several sequences, for example :
$"\textcolor{red}{0}\textcolor{blue}{0}10101010\textcolor{green}{0}1011111011\textcolor{orange}{0}10101010"$, of length $30$, or
$"\textcolor{red}{0}10\textcolor{green}{0}101111101110"$, of length $16$, which is the minimal sequence for this image.
For a positive integer $N$, define $D_N$ as the $2^N \times 2^N$ image with the following coloring scheme:
the pixel with coordinates $x = 0, y = 0$ corresponds to the bottom left pixel,
if $(x - 2^{N - 1})^2 + (y - 2^{N - 1})^2 \le 2^{2N - 2}$ then the pixel is black,
otherwise the pixel is white.
What is the length of the minimal sequence describing $D_{24}$?
Problem 287: Quadtree Encoding
Mathematical Foundation
Theorem (Quadtree Encoding Length Recurrence). Let be the encoding length of the square region . Then
where are the four quadrants of size .
Proof. By the encoding specification: uniform regions output 2 bits, and mixed regions output 1 bit (the 0 prefix) plus the four sub-encodings.
Lemma (Uniform Region Tests). For a square and the disk with , :
- The region is entirely black if all four corner pixels , , , satisfy the disk inequality.
- The region is entirely white if the pixel in the region closest to the center fails the disk inequality. The closest pixel is .
Proof.
(1) The disk is a convex set. Since all pixels in the square lie in the convex hull of the four corners, and the four corners are in the disk, all pixels are in the disk. (Formally: a point in the square can be written as a convex combination of the corners; since the disk is convex, any convex combination of points in the disk lies in the disk.)
(2) If the closest point to the center is outside the disk, then every point in the region is at least as far from the center (by the definition of “closest”), hence outside the disk.
Theorem (Quadtree Complexity for a Disk). The quadtree for a disk of radius on a grid has nodes.
Proof. Only squares that intersect the boundary of the disk need subdivision. At depth (squares of side ), the circle boundary passes through such squares (the boundary has length and each square has side , so the number of boundary-touching squares is ). The total number of internal nodes is .
Editorial
We check all-black: all 4 corners inside disk. We then check all-white: closest point outside disk. Finally, mixed: recurse on 4 quadrants.
Pseudocode
Check all-black: all 4 corners inside disk
Check all-white: closest point outside disk
Mixed: recurse on 4 quadrants
Complexity Analysis
- Time: where . The quadtree has nodes, each requiring work.
- Space: for the recursion stack (depth ).
Answer
Code
Each problem page includes the exact C++ and Python source files from the local archive.
#include <bits/stdc++.h>
using namespace std;
const int N = 24;
const long long C = 1LL << (N - 1); // center = 2^23
const long long R2 = 1LL << (2 * (N - 1)); // radius^2 = 2^46
bool inside(long long x, long long y) {
long long dx = x - C;
long long dy = y - C;
return dx * dx + dy * dy <= R2;
}
long long solve(long long x0, long long y0, long long s) {
if (s == 1) {
return 2; // single pixel: either 10 or 11
}
// Check all four corners of the pixel region [x0, x0+s-1] x [y0, y0+s-1]
long long x1 = x0 + s - 1;
long long y1 = y0 + s - 1;
bool c00 = inside(x0, y0);
bool c10 = inside(x1, y0);
bool c01 = inside(x0, y1);
bool c11 = inside(x1, y1);
if (c00 && c10 && c01 && c11) {
// All corners inside => all black (since disk is convex, all interior pixels are inside too)
return 2;
}
// Check if entirely outside: find closest point in the square to center
long long cx = max(x0, min(C, x1));
long long cy = max(y0, min(C, y1));
if (!inside(cx, cy)) {
// Closest point is outside the disk => all white
return 2;
}
// Mixed: subdivide
long long h = s / 2;
return 1 + solve(x0, y0, h) + solve(x0 + h, y0, h) +
solve(x0, y0 + h, h) + solve(x0 + h, y0 + h, h);
}
int main() {
long long S = 1LL << N;
cout << solve(0, 0, S) << endl;
return 0;
}
"""
Problem 287: Quadtree Encoding
Find the shortest quadtree encoding length for a 2^N x 2^N image (N=24)
where pixel (x,y) is black iff (x - 2^(N-1))^2 + (y - 2^(N-1))^2 <= 2^(2(N-1)).
"""
import sys
sys.setrecursionlimit(1000000)
N = 24
C = 1 << (N - 1) # center coordinate
R2 = 1 << (2 * (N - 1)) # radius squared
def inside(x, y):
dx = x - C
dy = y - C
return dx * dx + dy * dy <= R2
def solve(x0, y0, s):
if s == 1:
return 2
x1 = x0 + s - 1
y1 = y0 + s - 1
# Check if all corners are inside (convexity => all pixels inside)
if (inside(x0, y0) and inside(x1, y0) and
inside(x0, y1) and inside(x1, y1)):
return 2
# Check if entirely outside: closest point to center
cx = max(x0, min(C, x1))
cy = max(y0, min(C, y1))
if not inside(cx, cy):
return 2
# Mixed: subdivide
h = s // 2
return (1 + solve(x0, y0, h) + solve(x0 + h, y0, h) +
solve(x0, y0 + h, h) + solve(x0 + h, y0 + h, h))
def main():
S = 1 << N
result = solve(0, 0, S)
print(result)
if __name__ == "__main__":
main()