A cube of side length n holds hats arranged in a 3D grid. Each hat has an uppercase letter color, initially G for green. Implementation uses an integer representation where 'A' maps to 1 and 'G' maps to 7 via the formula int(letter)-64.
Supported operations within axis-aligned rectangular regions:
- Paint all hats within a specified sub‑cube with a new color.
- Repaint specific colored hats inside a region back to green, identified by the integer
7. - Count green hats located inside the region.
Constraints allow a direct brute-force approach with a triple nested loop, since n and queries q keep the worst-case product max(n)^3 * max(q) around 12.8 million operations.
#include <iostream>
#include <cstring>
using namespace std;
int n, m, type, grid[45][45][45];
int xl, yl, zl, xr, yr, zr;
inline int countGreens(int sx, int sy, int sz, int ex, int ey, int ez) {
int cnt = 0;
for (int i = sx; i <= ex; ++i)
for (int j = sy; j <= ey; ++j)
for (int k = sz; k <= ez; ++k)
if (grid[i][j][k] == 7) ++cnt;
return cnt;
}
inline void fillRegion(int sx, int sy, int sz, int ex, int ey, int ez, int color) {
for (int i = sx; i <= ex; ++i)
for (int j = sy; j <= ey; ++j)
for (int k = sz; k <= ez; ++k)
grid[i][j][k] = color;
}
inline void revertColor(int sx, int sy, int sz, int ex, int ey, int ez, int targetColor) {
for (int i = sx; i <= ex; ++i)
for (int j = sy; j <= ey; ++j)
for (int k = sz; k <= ez; ++k)
if (grid[i][j][k] == targetColor)
grid[i][j][k] = 7;
}
int main() {
cin >> n >> m;
fillRegion(1, 1, 1, n, n, n, 7);
while (m--) {
cin >> type;
if (type == 0) {
cin >> xl >> yl >> zl >> xr >> yr >> zr;
cout << countGreens(xl, yl, zl, xr, yr, zr) << endl;
} else if (type == 1) {
char newCol;
cin >> xl >> yl >> zl >> xr >> yr >> zr >> newCol;
fillRegion(xl, yl, zl, xr, yr, zr, newCol - 64);
} else if (type == 2) {
char oldCol;
cin >> xl >> yl >> zl >> xr >> yr >> zr >> oldCol;
revertColor(xl, yl, zl, xr, yr, zr, oldCol - 64);
}
}
return 0;
}
isha treat each hat coordinate within inclusive ranges. The mapping from chraacter to integer uses ASCII distance to 'A' plus one. Allocate the 3D array slightly larger than the maximum n to avoid boundary errors.