Rooms
Time limit3.5sMemory limit1024 MB
Given a letter grid, count the same-letter 4-connected regions that have at least one cell inside each query rectangle.
- Level
Medium7 of 10
- Topics
- Graph, Matrix, Union-find
- Solved
- No attempts yet
Problem
You are given a matrix with rows and columns. Each cell contains one lowercase English letter.
A room is a maximal set of cells that contain the same letter and are connected in 4 directions: up, down, left, and right.
Answer queries. Each query gives a rectangular submatrix, and you must count the rooms that are completely or partly inside it.
Input
The first line contains two integers and . Each of the next lines contains lowercase letters. The next line contains the integer , the number of queries. Each of the following lines contains four integers , , , and , which describe the rectangle whose diagonally opposite corners are and .
Output
Print lines. The -th line contains the answer to the -th query.
Constraints