题目
思路
开一个全为0的数组,对于每个地毯,将左上角的坐标+1, 右下角右边和下边的坐标-1,最后再做一次前缀和。
代码
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
int ground[1003][1003];
int main()
{
memset(ground, 0, sizeof(ground));
int n, m;
int x1,y1,x2,y2;
scanf("%d %d", &n, &m);
while(m--)
{
scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
ground[x1][y1]++;
ground[x1][y2+1]--;
ground[x2+1][y1]--;
ground[x2+1][y2+1]++;
}
for(int i = 1; i <=n; i++)
{
for(int j = 1; j <= n; j++)
{
ground[i][j] += ground[i-1][j];
ground[i][j] += ground[i][j-1];
ground[i][j] -= ground[i-1][j-1];
}
}
for(int i = 1; i <=n ;i++)
{
for(int j = 1; j <= n; j++)
{
printf("%d ", ground[i][j]);
}
cout << endl;
}
return 0;
}