Search for a command to run...
Progressive hints first, then the full explanation and implementation when you're ready to cash out.
Review status
AI-generated and still unreviewed. Double-check the details before internalizing them.
Hints
Open only as much as you need to keep the solve alive.
The two lasers move together, so the vector between them never changes. Think of the pair as one rigid shape you slide around the board.
Fix and . A cell can be occupied by the first laser iff the cell shifted by is also inside the grid. Same idea for the second laser with the opposite shift.
How many cells satisfy that condition? The first laser can stand in exactly cells. The second laser has the same count. Geometrically, each laser can melt one axis-aligned rectangle.
Donβt add those two rectangle areas blindly β they may overlap. A cell is in the overlap iff both shifts by and stay inside the board. That gives overlap size .
Let , . Then So the answer is Thatβs it. No BFS on a grid, thank god.
The lasers always move by the same vector, so the offset between them is constant:
So the whole setup is just a rigid 2-point figure sliding around the board.
Also, any valid translated position is reachable: the set of translations that keep both lasers inside the board is a rectangle in translation-space, so you can move horizontally and vertically inside it without ever leaving the field. Nothing sneaky here.
Take some cell . The first laser can be at iff the second one, which must stay at
is also inside the board.
That means:
The number of valid is , and the number of valid is . So the first laser can melt exactly
cells.
Same for the second laser. Symmetry doing its job, for once.
Let
Then each laser can reach
cells.
We need the number of cells reachable by at least one laser, so use inclusion-exclusion.
A cell lies in the overlap iff it can be occupied by both lasers. That means both shifted cells exist:
up to signs, and only the absolute differences matter.
So horizontally, the cell must have room for both directions, which gives
possible columns. Similarly vertically:
Hence the overlap size is
The number of meltable cells is
Therefore the number of cells that cannot be melted is
The statement mentions moving continuously, with lasers active all the time. Sounds scary, but it changes nothing.
If a laser sweeps through some cell during a horizontal/vertical move, then the center of that cell is also crossed at some intermediate moment. Since all intermediate integer positions are valid stopping points as well, counting reachable cell centers is enough. No half-cell black magic needed.
Each test case is just a few arithmetic operations:
time and
memory.
Given , this is hilariously safe.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
int t;
cin >> t;
while (t--) {
ll n, m, x1, y1, x2, y2;
cin >> n >> m >> x1 >> y1 >> x2 >> y2;
ll dx = llabs(x1 - x2);
ll dy = llabs(y1 - y2);
ll one = (n - dx) * (m - dy); // cells reachable by one fixed laser
ll inter = max(0LL, n - 2 * dx) * max(0LL, m - 2 * dy);
ll melted = 2 * one - inter;
ll ans = n * m - melted;
cout << ans << '\n';
}
return 0;
}#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
int t;
cin >> t;
while (t--) {
ll n, m, x1, y1, x2, y2;
cin >> n >> m >> x1 >> y1 >> x2 >> y2;
ll dx = llabs(x1 - x2);
ll dy = llabs(y1 - y2);
ll one = (n - dx) * (m - dy); // cells reachable by one fixed laser
ll inter = max(0LL, n - 2 * dx) * max(0LL, m - 2 * dy);
ll melted = 2 * one - inter;
ll ans = n * m - melted;
cout << ans << '\n';
}
return 0;
}