Blocked Billboard II - Only One Wrong Test Case

I’m struggling a lot to figure out why only one test case isn’t working when I submit my code for this problem: USACO
I would greatly appreciate if someone could give me a hint or explanation as to what case could come up that would cause my code to be incorrect for the problem; thanks!

#include <bits/stdc++.h>
using namespace std;

int main() {
    freopen("billboard.in", "r", stdin);
    freopen("billboard.out", "w", stdout);
    int x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2;
    int x3, y3, x4, y4; cin >> x3 >> y3 >> x4 >> y4;
    int area;
    int xo = max(0, min(x2, x4) - max(x1, x3)); // x length of intersection
    int yo = max(0, min(y2, y4) - max(y1, y3)); // y height of intersection
    int xlen = x2 - x1;
    int ylen = y2 - y1;
    area = xlen * ylen; // initially assume that the whole area needs to be covered
    if ((x3 <= x1 && x2 <= x4) || (y3 <= y1 && y2 <= y4)) { // if the entire first rectangle is covered in at least one direction, subtract the area of overlap
        area -= xo*yo;
    }
    cout << area;
}

Have you tried downloading the test cases?

I didn’t realize that was an option until you mentioned it, but I found the test cases and now see where I went wrong; thanks!

My mistake was that I failed to consider cases where the overlap essentially sliced the rectangle into two. Example:
-5 -5 5 5
-7 -1 8 2

1 Like