Planet Exploration
InterviewTime limit1sMemory limit256 MB
Given a grid of jungle, sea, and ice cells, answer many rectangle queries by counting how many cells of each terrain type fall inside.
- Level
Medium4 of 10
- Topics
- Prefix sum, Array, Implementation, Matrix
- Solved
- No attempts yet
Problem
An expedition traveling by spaceship searched for a habitable planet and finally found one. The planet is covered entirely by jungle, sea, and ice. The team drew a map of the habitable region and sent it back to Earth.
The map is a rectangle cm wide and cm tall, divided into square cells 1 cm on each side. The terrain of each cell is written as a letter: jungle is J, sea is O, and ice is I.
Back on Earth, survey regions were chosen. For each region, write a program that counts how many cells of jungle, sea, and ice it contains.
Input
The first line contains the map size and . ()
The second line contains the number of survey regions . ()
Each of the next lines contains the map: a string of length made up of J, O, and I.
Each of the following lines describes one survey region as four integers , , , . The region is a rectangle whose top-left cell is and whose bottom-right cell is , where the first value is the row counted from the top and the second value is the column counted from the left. (, )
Output
For each survey region, print the number of jungle, sea, and ice cells inside it, separated by spaces, one region per line. In other words, print the counts of J, O, and I in that order.
Hint
The figure below illustrates counting terrain inside one survey region. This region contains 3 jungle (J) cells, 5 sea (O) cells, and 2 ice (I) cells.
