Official
B - Deconstruct Chocolate Editorial by en_translator
Process the query one by one in order.
Suppose that the chocolate is currently a rectangle with \(h\) rows and \(w\) columns.
For a type-\(1\) query, the bottom \(R\) rows have \(Rw\) blocks of chocolate, and the chocolate becomes a rectangle with \((h - R)\) rows and \(w\) columns.
For a type-\(2\) query, the leftmost \(C\) rows have \(hC\) blocks of chocolate, and the chocolate becomes a rectangle with \(h\) rows and \((w - C)\) columns.
Therefore, the answers to the query can be found by managing the number of rows and columns of the chocolate while performing the simulation.
Sample code
#include <bits/stdc++.h>
using namespace std;
int main() {
int h, w, q;
cin >> h >> w >> q;
while (q--) {
int t;
cin >> t;
if (t == 1) {
int r;
cin >> r;
cout << r * w << '\n';
h -= r;
}
else {
int c;
cin >> c;
cout << h * c << '\n';
w -= c;
}
}
}
posted:
last update: