The task is to write a Java program that calculates the total area of the union of N given rectangles, where each rectangle's edges are parallel to the axis. The rectangles may intersect or be inside each other. The complexity should not be O(n^2). The input format consists of the number of rectangles followed by the coordinates of each rectangle (x1, x2, y1, y2) in the format , , etc. The output is a single number representing the total area of the union.