Problem:
MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

Union of Rectangles

Given a set of $N$ axis-aligned rectangles in the plane, find the area of regions which are covered by at least one rectangle.

Input

The input is given in the following format.

$N$

$x1_1$ $y1_1$ $x2_1$ $y2_1$

$x1_2$ $y1_2$ $x2_2$ $y2_2$

:

$x1_N$ $y1_N$ $x2_N$ $y2_N$

($x1_i, y1_i$) and ($x2_i, y2_i$) are the coordinates of the top-left corner and the bottom-right corner of the $i$-th rectangle respectively.

Constraints

$ 1 \leq N \leq 2000 $

$ −10^9 \leq x1_i < x2_i\leq 10^9 $

$ −10^9 \leq y1_i < y2_i\leq 10^9 $

Output

Print the area of the regions.

Sample Input 1

2
0 0 3 4
1 2 4 3

Sample Output 1

13

Sample Input 2

3
1 1 2 5
2 1 5 2
1 2 2 5

Sample Output 2

7

Sample Input 3

4
0 0 3 1
0 0 1 3
0 2 3 3
2 0 3 3

Sample Output 3

8

Solution in Go (judge verdict: Accepted)

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
	"strconv"
)

func configure(scanner *bufio.Scanner) {
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 1000005), 1000005)
}
func getNextString(scanner *bufio.Scanner) string {
	scanned := scanner.Scan()
	if !scanned {
		panic("scan failed")
	}
	return scanner.Text()
}
func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}
func getNextInt64(scanner *bufio.Scanner) int64 {
	i, _ := strconv.ParseInt(getNextString(scanner), 10, 64)
	return i
}
func getNextFloat64(scanner *bufio.Scanner) float64 {
	i, _ := strconv.ParseFloat(getNextString(scanner), 64)
	return i
}
func main() {
	fp := os.Stdin
	wfp := os.Stdout
	extra := 0
	if os.Getenv("I") == "IronMan" {
		fp, _ = os.Open(os.Getenv("END_GAME"))
		extra = 100
	}
	scanner := bufio.NewScanner(fp)
	configure(scanner)
	writer := bufio.NewWriter(wfp)
	defer func() {
		r := recover()
		if r != nil {
			fmt.Fprintln(writer, r)
		}
		writer.Flush()
	}()
	solve(scanner, writer)
	for i := 0; i < extra; i++ {
		fmt.Fprintln(writer, "-----------------------------------")
		solve(scanner, writer)
	}
}
func solve(scanner *bufio.Scanner, writer *bufio.Writer) {
	n := getNextInt(scanner)
	xx := make([]int, 0)
	yy := make([]int, 0)
	sx := make([]int, n)
	tx := make([]int, n)
	sy := make([]int, n)
	ty := make([]int, n)
	for i := 0; i < n; i++ {
		sx[i] = getNextInt(scanner)
		sy[i] = getNextInt(scanner)
		tx[i] = getNextInt(scanner)
		ty[i] = getNextInt(scanner)
		xx = append(xx, sx[i])
		xx = append(xx, tx[i])
		yy = append(yy, sy[i])
		yy = append(yy, ty[i])
	}
	xm, rxm := compress(xx)
	ym, rym := compress(yy)
	imos := makeGrid(len(ym), len(xm))
	for i := 0; i < n; i++ {
		imos[ym[sy[i]]][xm[sx[i]]]++
		imos[ym[ty[i]]][xm[sx[i]]]--
		imos[ym[sy[i]]][xm[tx[i]]]--
		imos[ym[ty[i]]][xm[tx[i]]]++
	}
	for y := 0; y < len(ym); y++ {
		for x := 0; x < len(xm)-1; x++ {
			imos[y][x+1] += imos[y][x]
		}
	}
	for x := 0; x < len(xm); x++ {
		for y := 0; y < len(ym)-1; y++ {
			imos[y+1][x] += imos[y][x]
		}
	}
	var ans int64
	for y := 0; y < len(ym)-1; y++ {
		for x := 0; x < len(xm)-1; x++ {
			if imos[y][x] == 0 {
				continue
			}
			ans += int64(rym[y+1]-rym[y]) * int64(rxm[x+1]-rxm[x])
		}
	}
	fmt.Fprintln(writer, ans)

}
func makeGrid(h, w int) [][]int {
	index := make([][]int, h, h)
	data := make([]int, h*w, h*w)
	for i := 0; i < h; i++ {
		index[i] = data[i*w : (i+1)*w]
	}
	return index
}
func compress(xx []int) (map[int]int, []int) {
	m := map[int]int{}

	for _, x := range xx {
		m[x] = 1
	}
	yy := make([]int, 0)
	for x := range m {
		yy = append(yy, x)
	}
	sort.Ints(yy)
	for i := 0; i < len(yy); i++ {
		m[yy[i]] = i
	}
	return m, yy
}