Codeforces White Sheet

This is the problem. Problem - C - Codeforces
Here is my code:

import java.util.;

public class codeforcesWhiteSheet {
public static void main(String[] args) throws IOException{

    // We want to find the total area covered by the black rectangles and see if that area is less than the area of the white sheet. 
    // Remember finding the area of a Venn diagram. First add up the primary areas, then add up the areas of their intersections, and then subtract the areas
    // of the intersections of the intersections of the primary areas. 
    BufferedReader br = new BufferedReader(new InputStreamReader(;
    PrintWriter pw = new PrintWriter(System.out);
    StringTokenizer st = new StringTokenizer(br.readLine());
    int wx1 = Integer.parseInt(st.nextToken());
    int wy1 = Integer.parseInt(st.nextToken());
    int wx2 = Integer.parseInt(st.nextToken());
    int wy2 = Integer.parseInt(st.nextToken());
    st = new StringTokenizer(br.readLine());
    int b1x1 = Integer.parseInt(st.nextToken());
    int b1y1 = Integer.parseInt(st.nextToken());
    int b1x2 = Integer.parseInt(st.nextToken());
    int b1y2 = Integer.parseInt(st.nextToken());
    st = new StringTokenizer(br.readLine());
    int b2x1 = Integer.parseInt(st.nextToken());
    int b2y1 = Integer.parseInt(st.nextToken());
    int b2x2 = Integer.parseInt(st.nextToken());
    int b2y2 = Integer.parseInt(st.nextToken());
    int areaWhite = (wx2 - wx1) * (wy2 - wy1);
    int areaInterWB1 = INTER(wx1, wy1, wx2, wy2, b1x1, b1y1, b1x2, b1y2);
    int areaInterWB2 = INTER(wx1, wy1, wx2, wy2, b2x1, b2y1, b2x2, b2y2);
    // Now we need to find the area of the intersection of areaInterWB1 and areaInterWB2. Call this areaTertiary
    // To do that, we need to find the coordinates of the rectangles they make.
    // Let's find their coordinates first. But, if any of these rectangles is nonexistent, we don't have to worry about this.
    // If they exist, their coordinates are: For WB1, BL=(Math.max(wx1, b1x1), Math.max(wy1, b1y1)) and TR=(Math.min(wx2, b1x2), Math.min(wy2, b1y2)) 
    // For WB2, BL=(Math.max(wx1, b2x1), Math.max(wy1, b2y1)) and TR=(Math.min(wx2, b2x2), Math.min(wy2, b2y2))
    int areaTertiary;
    if (areaInterWB1 == 0 || areaInterWB2 == 0) {
        areaTertiary = 0;
    else {
        areaTertiary = INTER(Math.max(wx1, b1x1), Math.max(wy1, b1y1), Math.min(wx2, b1x2), Math.min(wy2, b1y2), Math.max(wx1, b2x1), Math.max(wy1, b2y1), Math.min(wx2, b2x2), Math.min(wy2, b2y2));
    if (areaWhite > (areaInterWB1 + areaInterWB2 - areaTertiary)) {
    else {

public static int INTER(int ax1, int ay1, int ax2, int ay2, int bx1, int by1, int bx2, int by2){ 
    // We need to find the area of intersection of rectangle a and b. When one of the bottoms is more than the other's top, NO INTERSECTION.
    // The rectangle formed by their intersection has a width of Math.min(ax2, bx2) - Math.max(ax1, bx1). 
    // The length is Math.min(ay2, by2) - Math.max(ay1, by1). The area is just length times width.
    if (ax1 >= bx2 || bx1 >= ax2){
        return 0;
    else if (ay1 >= by2 || by1 >= ax2){
        return 0;
    else {
        int width = Math.min(ax2, bx2) - Math.max(ax1, bx1);
        int length = Math.min(ay2, by2) - Math.max(ay1, by1);
        return width * length;


The problem says that my solution was wrong on test 8. My solution was to find the white area and see if the total black area covering the sheet is greater than the white area. I first found the area of the intersection of the white sheet and black sheet 1 and the area of the intersection of the white sheet and black sheet 2. Then I found the area of the intersection of the three papers. The total black area covering the white sheet would be areaInterWB1 + areaInterWB2 - areaTertiary.

Please see what’s lacking in my solution. Your help is much appreciated. Thank you!