题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6514
题意:给定n*m的矩阵,给定监视器可以搜索的范围,再给定小偷的路径。询问是否能够检测到小偷的全部路径。
题解:二维差分前缀和,因为数据范围很大,无法开二维的数组,因此使用一维数组模拟二维数组。
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 3e7 + 10;
int b[MAXN]; //一维数组模拟二维
int main()
{
int n, m, q, z; //n 行 m 列 q 询问 z 修改
while (~scanf("%d%d", &n, &m))
{
memset(b, 0, sizeof(b));
scanf("%d", &z);
for (int i = 0; i < z; i++) //二维差分 m是修改操作次数
{
int x1, y1, x2, y2, p = 1;
scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
b[x1*m + y1] += p; b[(x2 + 1)*m + (y2 + 1)] += p;
b[(x2 + 1)*m + y1] -= p; b[x1*m + (y2 + 1)] -= p;
}
for (int i = 1; i <= n; i++) //前缀和 求修改后的数组
{
for (int j = 1; j <= m; j++)
b[i*m + j] += b[i*m + (j - 1)] + b[(i - 1)*m + j] - b[(i - 1)*m + (j - 1)];
}
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
b[i*m + j] = min(b[i*m + j], 1); //大于1的部分设置为1,表示可以探测到
}
for (int i = 1; i <= n; i++) //前缀和 求二维数组前缀和
{
for (int j = 1; j <= m; j++)
b[i*m + j] += b[i*m + (j - 1)] + b[(i - 1)*m + j] - b[(i - 1)*m + (j - 1)];
}
scanf("%d", &q);
for (int i = 1; i <= q; i++)
{
int x1, y1, x2, y2;
scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
int ans = b[x2*m + y2] - b[(x1 - 1)*m + y2] - b[x2*m + (y1 - 1)] + b[(x1 - 1)*m + (y1 - 1)];
if (ans == (x2 - x1 + 1)*(y2 - y1 + 1)) cout << "YES" << endl;
else cout << "NO" << endl;
}
}
return 0;
}