1 条题解

  • 0
    @ 2024-9-14 16:37:16

    暴力:复杂度 O(mn2)O(mn^2)

    二维差分+前缀和:复杂度 O(m+n2)O(m+n^2)

    #include <bits/stdc++.h>
    using namespace std;
    typedef long long LL;
    const int N = 1e3 + 10, INF = 0x3f3f3f3f;
    int n, m, d[N][N];
    
    int brute_force() {
        cin >> n >> m;
        int x1, y1, x2, y2;
        while (m--) {
            cin >> x1 >> y1 >> x2 >> y2;
            for (int i = x1; i <= x2; i++)
                for (int j = y1; j <= y2; j++)
                    d[i][j]++;
        }
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= n; j++)
                cout << d[i][j] << " \n"[j == n];
        return 0;
    }
    int solve_ac() {
        cin >> n >> m;
        int x1, y1, x2, y2;
        while (m--) {
            cin >> x1 >> y1 >> x2 >> y2;
            d[x1][y1]++;
            d[x1][y2 + 1]--;
            d[x2 + 1][y1]--;
            d[x2 + 1][y2 + 1]++;
        }
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= n; j++) {
                d[i][j] += d[i - 1][j] + d[i][j - 1] - d[i - 1][j - 1];
                cout << d[i][j] << " \n"[j == n];
            }
        return 0;
    }
    int main() {
        brute_force();
        solve_ac();
    }
    
    • 1

    信息

    ID
    326
    时间
    1000ms
    内存
    512MiB
    难度
    3
    标签
    递交数
    138
    已通过
    71
    上传者