Skip to content

Problem E: 追逐光明的拈花佛陀

📝 题目描述

🔑 算法模块

差分

💡 思路分析

二维差分板子。

🖥️ 代码实现

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <bits/stdc++.h>
using namespace std;
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, m;
    cin >> n >> m;
    vector<vector<int>> a(n + 2, vector<int>(n + 2, 0));
    for (int i = 0; i < m; i++) {
        int e, x1, y1, x2, y2;
        cin >> e >> x1 >> y1 >> x2 >> y2;
        a[x1][y1] += e;
        a[x1][y2 + 1] -= e;
        a[x2 + 1][y1] -= e;
        a[x2 + 1][y2 + 1] += e;
    }
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            a[i][j] += a[i - 1][j] + a[i][j - 1] - a[i - 1][j - 1];
        }
    }
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            cout << a[i][j] << " \n"[j == n];
        }
    }
    return 0;
}

⏱️ 复杂度分析

📚 拓展