Skip to content

USACO 2018 January Contest Bronze Division - Blocked Billboard II#

Problem link: here

Solution Author: Stefan Dascalescu

Problem Solution#

We want to intersect the two rectangles and see if at least one sides of the intersection is equal to the entire rectangle's side, and from that point we have some casework we need to do.

Source codes#

The source codes in C++ and Python can be seen below.

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

int main()
{
    ifstream cin("billboard.in");
    ofstream cout("billboard.out");

    int xa, ya, xb, yb;
    cin >> xa >> ya >> xb >> yb;
    int xc, yc, xd, yd;
    cin >> xc >> yc >> xd >> yd;

    if(xd >= xb && xc <= xa && yd >= yb && yc <= ya)
    {
        cout << 0;
        return 0;
    }
    if(xc <= xa && yc <= ya && yd > ya && xd >= xb)
    {
        cout << (xb - xa) * (yd - yb);
        return 0;
    }
    if(yc < yb && xc <= xa && yd >= yb && xd >= xb)
    {
        cout << (xb - xa) * (yc - ya);
        return 0;
    }
    if(xd > xa && xc <= xa && yd >= yb && yc <= ya)
    {
        cout << (xb - xd) * (yb - ya);
        return 0;
    }
    if(xc < xb && xd >= xb && yd >= yb && yc <= ya)
    {
        cout << (xc - xa) * (yb - ya);
        return 0;
    }
    cout << (xb - xa) * (yb - ya);
    return 0;
}
with open("billboard.in", "r") as fin:
    tokens = fin.read().split()

xa, ya, xb, yb = map(int, tokens[:4])
xc, yc, xd, yd = map(int, tokens[4:8])

if xd >= xb and xc <= xa and yd >= yb and yc <= ya:
    ans = 0
elif xc <= xa and yc <= ya and yd > ya and xd >= xb:
    ans = (xb - xa) * (yd - yb)
elif yc < yb and xc <= xa and yd >= yb and xd >= xb:
    ans = (xb - xa) * (yc - ya)
elif xd > xa and xc <= xa and yd >= yb and yc <= ya:
    ans = (xb - xd) * (yb - ya)
elif xc < xb and xd >= xb and yd >= yb and yc <= ya:
    ans = (xc - xa) * (yb - ya)
else:
    ans = (xb - xa) * (yb - ya)

with open("billboard.out", "w") as fout:
    fout.write(str(ans))