Table of Contents

ExplanationImplementation

Official Analysis (C++)

Explanation

For each of the three dimensions, we create an array that represents how many blocks of cheese are left in the respective axis after fixing the other two dimensions.

For example, we can create an array for the x-dimension that represents the number of blocks of cheese left for all y and z. Once the count of a given (y,z)(y, z) reaches 00, we know a new brick configuration has formed, and we can add one to the answer.

So, maintain three arrays for the xx, yy, and zz dimensions, and for each query, update all three dimensions to count how many new distinct configurations of bricks can be stuck through the block of cheese.

Implementation

C++

#include <bits/stdc++.h>
using namespace std;
int main() {
int n, q;
cin >> n >> q;
vector<vector<vector<int>>> a(3, vector<vector<int>>(n, vector<int>(n, n)));
int ans = 0;
while (q--) {

Python

import sys
input = sys.stdin.readline
n, q = map(int, input().split())
ct = [[[n for _ in range(n)] for _ in range(n)] for _ in range(3)]
ans = 0
for _ in range(q):

Java

import java.io.*;
import java.util.StringTokenizer;
public class FJCheeseBlock {
public static void main(String[] args) throws IOException {
BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(System.out);
StringTokenizer st = new StringTokenizer(r.readLine());
int n = Integer.parseInt(st.nextToken());

Join the USACO Forum!

Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!