Painting the Barn(二维前缀和,二维差分)

发布于 2020-08-23  0 次阅读


题目大意:

Painting the Barn
在一个二维平面上,给你n个矩阵,每个矩阵给你左下角和右上角的点,代表矩阵覆盖的面
求被覆盖了k次的面积

思路:

二维前缀和,
v i s [ i ] [ j ] + = v i s [ i − 1 ] [ j ] + v i s [ i ] [ j − 1 ] − v i s [ i − 1 ] [ j − 1 ]
二维差分

AC Code:

#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
using namespace std;
#define endl '\n'
#define INF 0x3f3f3f3f
#define int long long
// #define TDS_ACM_LOCAL
const int N=1009;
int n, k, x, y, x2, y2;
int vis[N][N];
void solve(){
    cin>>n>>k;
    for(int i=1; i<=n; i++){
        cin>>x>>y>>x2>>y2;
        x++, y++, x2++, y2++;
        vis[x][y]++, vis[x2][y2]++;
        vis[x][y2]--, vis[x2][y]--;
    }
    int ans=0;
    for(int i=1; i<=1000; i++){
        for(int j=1; j<=1000; j++){
            vis[i][j]+=vis[i-1][j]+vis[i][j-1]-vis[i-1][j-1];
            if(vis[i][j]==k)    ans++;
        }
    }
    cout<<ans<<endl;
    return ;
}

signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
#ifdef TDS_ACM_LOCAL
    freopen("D:\\VS code\\.vscode\\testall\\in.txt", "r", stdin);
    freopen("D:\\VS code\\.vscode\\testall\\out.txt", "w", stdout);
#endif
    solve();
    return 0;
}

平平无奇的在校大学生