Video Solution
By Varun Ragunath
Video Solution Code
Info/Tips
Especially with rectangle geometry, make a good habit of drawing patterns and observations on a scratch sheet of paper or whiteboard. It helps a lot!
Also, if you don't understand the explanation, draw each possibility (Top, Bottom, Left and Right) on a sheet of paper and try to see the connections.
Explanation
There are many ways to solve the problem, but the easiest way is to imagine cutting the white sheet.
Everytime the white sheet is covered by a black sheet, we can imagine cutting the intersection between the black and white sheets. Then, if the area of the final white sheet after being cut by the two rectangles is greater than zero, there is a portion of the white sheet that is visible.
However, when considering cutting the sheet, the portion that is being cut must cut from entirely -> or -> or else there will be a portion of the white sheet still shown. We only cut when a black sheet completely covers the entire width or length of the white sheet, otherwise there will still be bits and pieces shown.
There are 4 instances of which a white sheet can cover a black sheet.
From the top, if the white sheet is completely covered from -> or -> and intersects some part of a black sheet, then change the (top of the rectangle) to the bottom of the black rectangle.
From the bottom, if the white sheet is completely covered from -> and intersects some part of a black sheet, then change the (bottom of the rectangle) to the top of the black rectangle.
From the left, if the white sheet is completely covered from to and intersects some part of a black sheet, then change the (left edge of the rectangle) to the right edge of the black rectangle.
From the right, if the white sheet is completely covered from to and intersects some part of a black sheet, then change the (right edge of the rectangle) to the left edge of the black rectangle.
Also, keep in mind to make sure that is always and the same with and
Since there are no for loops or any sort of repetitions, the time complexity is
Implementation
Time Complexity:
C++
#include <bits/stdc++.h>using namespace std;struct Rect {int x1, y1, x2, y2;int area() { return (x2 - x1) * (y2 - y1); }};/*Main Idea: If B intersects entirely in the x or y direction, cut it.
Python
class Rect:def __init__(self, a: int, b: int, c: int, d: int):self.x1, self.y1, self.x2, self.y2 = a, b, c, d"""Main Idea: If B intersects entirely in the x or y direction, cut it.This method cuts rectangle A based on rectangle B. (A - white sheet, B - black sheet)We can cut rectangle A if B covers all of x1->x2 or y1->y2."""
Java
import java.io.*;import java.util.*;public class Main {public static void main(String[] args) throws IOException {Kattio io = new Kattio();Rect A = new Rect(io.nextInt(), io.nextInt(), io.nextInt(), io.nextInt());Rect B = new Rect(io.nextInt(), io.nextInt(), io.nextInt(), io.nextInt());Rect C = new Rect(io.nextInt(), io.nextInt(), io.nextInt(), io.nextInt());
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!