测试用例范围比较大,所以全部用long类型,如果用int类型只能通过60%,建议在内存和运行时间允许的情况下,比赛题都用long。
重点在于计算相交的面积,这里找的两个相交点是左上角(m1,n1)和右下角(m2,n2)
java
import java.util.*;
// 1:无需package
// 2: 类名必须Main, 不可修改
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
long x1 = scan.nextInt();
long y1 = scan.nextInt();
long x2 = scan.nextInt();
long y2 = scan.nextInt();
long x3 = scan.nextInt();
long y3 = scan.nextInt();
long x4 = scan.nextInt();
long y4 = scan.nextInt();
long m1 = Math.max(Math.min(x1, x2), Math.min(x3, x4));
long n1 = Math.min(Math.max(y1, y2), Math.max(y3, y4));
long m2 = Math.min(Math.max(x1, x2), Math.max(x3, x4));
long n2 = Math.max(Math.min(y1, y2), Math.min(y3, y4));
long sum = 0;
if (m1 < m2 && n1 > n2)
sum = (m1 - m2) * (n1 - n2);
long res = Math.abs((y2 - y1) * (x2 - x1)) + Math.abs((y3 - y4) * (x3 - x4)) + sum;
System.out.println(res);
}
}