HDU 6514 Monitor (二维前缀和 差分)
二维前缀和 差分
#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <map> #include <set> #include <vector> #include <string> #include <cmath> #include <queue> #define rep(i, s, e) for(int i = s; i < e; ++i) #define P pair<int, int> #define INF 0x3f3f3f3f using namespace std; typedef long long ll; static const int N = 2020; static const int MAX_N = 1e4 + 5; static const ll Mod = 1e9 + 7; void add(int n, int m, vector<vector<int> >&vec){ for(int i = 1; i <= n; ++i){ for(int j = 1; j <= m; ++j){ vec[i][j] += vec[i - 1][j] + vec[i][j - 1] - vec[i - 1][j - 1]; } } } void solve(){ // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); //ios::sync_with_stdio(false); int n, m; while(scanf("%d%d", &n, &m) != EOF){ vector<vector<int> >vec(n + 5, vector<int>(m + 5)); int p; scanf("%d", &p); while(p--){ int x1, x2, y1, y2; scanf("%d%d%d%d", &x1, &y1, &x2, &y2); vec[x1][y1]++; vec[x1][y2 + 1]--; vec[x2 + 1][y1]--; vec[x2 + 1][y2 + 1]++; } add(n, m, vec); for(int i = 1; i <= n; ++i){ for(int j = 1; j <= m; ++j){ if(vec[i][j]) vec[i][j] = 1; } } add(n, m, vec); int q; scanf("%d", &q); while(q--){ int x1, x2, y1, y2; scanf("%d%d%d%d", &x1, &y1, &x2, &y2); int rec = vec[x2][y2] - vec[x1 - 1][y2] - vec[x2][y1 - 1] + vec[x1 - 1][y1 - 1]; if(rec == (x2 - x1 + 1) * (y2 - y1 + 1)) puts("YES"); else puts("NO"); } } } int main() { solve(); return 0; }