2024-07-12
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
Table of contents
【Template】One-dimensional prefix sum
【Template】Two-dimensional prefix sum
Find the center index of an array
Product of arrays other than itself
If we use brute force solution,The array must be traversed once each time, for a total of q times,sotime complexityToo high, then we construct a prefix sum arrayThe sum of each interval in the 1 - n interval is stored in, we need the first n items and directly access the dp prefix and the subscript position of the array. The code is as follows:
- #include<iostream>
- #include<vector>
- using namespace std;
-
- int main()
- {
- // 读入数据
- int n, q; cin >> n >> q;
- // n + 1 添加了虚拟节点0
- vector<int> arr(n + 1); // 默认全部为0
- for (int i = 1; i <= n; i++)
- cin >> arr[i];
-
- // 预处理出一个前缀和数组
- vector<long long> dp(n + 1); // 防止溢出
- for (int i = 1; i <= n; i++)
- dp[i] = dp[i - 1] + arr[i];
-
- // 使用前缀和数组
- int l = 0, r = 0;
- while (q--)
- {
- cin >> l >> r;
- cout << dp[r] - dp[l - 1] << endl;
- }
- return 0;
- }
PreprocessingA prefix sum matrix,All elements from (1, 1) to (i, j) andExisting in this dp array, throughArea calculation method, find the final answer, the code is as follows:
- int main()
- {
- // 读入数据
- int n, m, q; cin >> n >> m >> q;
- vector<vector<int>> arr(n + 1, vector<int>(m + 1));
- for (int i = 1; i <= n; i++)
- for (int j = 1; j <= m; j++)
- cin >> arr[i][j];
-
- // 预处理一个前缀和数组
- vector<vector<long long>> dp(n + 1, vector<long long>(m + 1)); // 防止溢出
- for (int i = 1; i <= n; i++)
- for (int j = 1; j <= m; j++)
- dp[i][j] = dp[i - 1][j] + dp[i][j - 1] + arr[i][j] - dp[i - 1][j - 1];
-
- // 使用前缀和数组
- while (q--)
- {
- int x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2;
- cout << dp[x2][y2] - dp[x2][y1 - 1] - dp[x1 - 1][y2] + dp[x1 - 1][y1 - 1] << endl;
- }
- return 0;
- }
Note the boundary conditions here.No need to open n+1 spaces for prefix and array, because there is an element in the original array to be used as the center subscript of this question, the code is as follows:
- class Solution {
- public:
- int pivotIndex(vector<int>& nums) {
- int n = nums.size();
- vector<int> f(n), g(n);
- // 预处理前缀和数组 从左向右
- for (int i = 1; i < n; i++)
- f[i] = f[i - 1] + nums[i - 1];
- // 预处理后缀和数组 从右向左
- for (int i = n - 2; i >= 0; i--)
- g[i] = g[i + 1] + nums[i + 1];
- for (int i = 0; i < n; i++)
- {
- if (g[i] == f[i])
- return i;
- }
- return -1;
- }
- };
The meaning is similar to the previous question, but it should be noted that the boundary cases f(0) and g(n-1) should be initialized to 1 instead of 0. The code is as follows:
- class Solution {
- public:
- vector<int> productExceptSelf(vector<int>& nums) {
- int n = nums.size();
- vector<int> f(n), g(n), ret(n);
- // 处理边界情况
- f[0] = 1; g[n - 1] = 1;
- // 预处理前缀积数组 从左向右
- for (int i = 1; i < n; i++)
- f[i] = f[i - 1] * nums[i - 1];
- // 预处理后缀积数组 从右向左
- for (int i = n - 2; i >= 0; i--)
- g[i] = g[i + 1] * nums[i + 1];
- for (int i = 0; i < n; i++)
- ret[i] = f[i] * g[i];
- return ret;
- }
- };
Note: The two-dimensional prefix and array must have one more row and one more column, otherwise out-of-bounds access will occur. In addition, the subscripts between the dp array and the ans array need to be adjusted to match the positions. ans[0][0] corresponds to the position dp[1][1]. The code is as follows:
- class Solution {
- public:
- vector<vector<int>> matrixBlockSum(vector<vector<int>>& mat, int k) {
- int m = mat.size(), n = mat[0].size(); // m 为行 n 为列
- // 预处理一个二维前缀和数组 dp
- vector<vector<int>> dp(m + 1, vector<int>(n + 1));
- for (int i = 1; i <= m; i++)
- for (int j = 1; j <= n; j++)
- dp[i][j] = dp[i - 1][j] + dp[i][j - 1] + mat[i - 1][j - 1] - dp[i - 1][j - 1];
- // 存放答案的二维数组 ans
- vector<vector<int>> ans(m, vector<int>(n));
- for (int i = 0; i < m; i++)
- {
- for (int j = 0; j < n; j++)
- {
- int x1 = max(0, i - k) + 1, y1 = max(0, j - k) + 1;
- int x2 = min(m - 1, i + k) + 1, y2 = min(n - 1, j + k) + 1;
- ans[i][j] = dp[x2][y2] - dp[x1 - 1][y2] - dp[x2][y1 - 1] + dp[x1 - 1][y1 - 1];
- }
- }
- return ans;
- }
- };