个人技术分享

矩形重叠面积公式:

p1与p2表示矩形A的左下角和右上角,用p3和p4表示矩形B的左下角和右上角。

res=!(p1.x > p4.x) || (p2.x < p3.x) || (p1.y > p4.y) || (p2.y < p3.y)  //对矩阵不重叠求反

矩阵重叠面积

    double Len = Math.min(p2_x, p4_x) - Math.max(p1_x, p3_x);
​
     double Wid = Math.min(p2_y, p4_y) - Math.max(p1_y, p3_y);
​
     return Len * Wid;
#pragma GCC optimize(2, 3, "Ofast", "inline")
#include <bits/stdc++.h>
#define endl '\n'
using namespace std;
int n, a, b;
bool is_overlap(int x1, int y1, int x2, int y2)
{
    if(x1>a||x2<0||y1>b||y2<0)
    return false;
    else
    return true;
}
void work()
{
    cin >> n >> a >> b;
    int x1, x2, y1, y2;
    int res = 0;
    for (int i = 0; i < n; i++)
    {
        cin >> x1 >> y1 >> x2 >> y2;
        if(!is_overlap(x1, y1, x2, y2))
        {
            continue;
        }
        int len=min(x2,a)-max(0,x1);
        int wid=min(y2,b)-max(0,y1);
        res+=len*wid;
    }
    cout << res;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    work();
    return 0;
}