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

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

题目传送门

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 ] ; vis[i][j]+=vis[i-1][j]+vis[i][j-1]-vis[i-1][j-1]; vis[i][j]+=vis[i1][j]+vis[i][j1]vis[i1][j1];

二维差分

vis[x][y]++, vis[x2][y2]++;
vis[x][y2]--, vis[x2][y]--;

AC Code

#include
#include
#include
#include
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;
}

你可能感兴趣的:(二维前缀和&差分,牛客假日团队赛)