给出矩形的 对角线上的两个点的坐标 (x11, y11), (x12, y12), (x21, y21), (x22, y22)。

1
2
3
4
5
6
7
8
9
10
11
int x01 = max(min(x11, x12), min(x21, x22));
int x02 = min(max(x11, x12), max(x21, x22));
int y01 = max(min(y11, y12), min(y21, y22));
int y02 = min(max(y11, y12), max(y21, y22));
if (x01 < x02 && y01 < y02){
// 说明这两个矩形相交
int s = (x02 - x01) * (y02 - y01); // 相交的面积
}
else{ // 这两个不相交

}

例题 :

题目页面1
题目页面2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int INF = 0x3f3f3f3f;
const int N = 1e5 + 5;
const int mod = 998244353;
struct node{
int x1, y1, x2, y2;
ll sum, s;
}arc[5005];
int x01, x02, y01, y02;
int main(){
int t, n;
cin >> t;
while(t--){
cin >> n;
for (int i = 1; i <= n; i++){
arc[i].sum= 0;
arc[i].s = 0;
cin >> arc[i].x1 >> arc[i].y1 >> arc[i].x2 >> arc[i].y2;
for (int j = 1; j < i; j++){
x01 = max(min(arc[i].x1, arc[i].x2), min(arc[j].x1, arc[j].x2));
x02 = min(max(arc[i].x1, arc[i].x2), max(arc[j].x1, arc[j].x2));
y01 = max(min(arc[i].y1, arc[i].y2), min(arc[j].y1, arc[j].y2));
y02 = min(max(arc[i].y1, arc[i].y2), max(arc[j].y1, arc[j].y2));
if (x01 < x02 && y01 < y02){
arc[i].sum++;
arc[i].s += (x02 - x01) * (y02 - y01);
arc[j].sum++;
arc[j].s += (x02 - x01) * (y02 - y01);
}
}
}
for (int i = 1; i <= n; i++){
cout << arc[i].sum << ' ' << arc[i].s << "\n";
}

}
return 0;
}