Iamtui1010

prefixsum2d.cpp

Oct 1st, 2022
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.13 KB | None | 0 0
  1. #include<iostream>
  2. #include<vector>
  3. #include<climits>
  4.  
  5. #define long long long
  6. #define nln '\n'
  7.  
  8. const long INF = 1e3;
  9.  
  10. using namespace std;
  11.  
  12. int main()
  13. {
  14.     cin.tie(0)->sync_with_stdio(0);
  15.     cout.tie(0)->sync_with_stdio(0);
  16.     //freopen("thuedat.inp", "r", stdin);
  17.     //freopen("thuedat.out", "w", stdout);
  18.     long m, n;
  19.     cin >> m >> n;
  20.     long a, b;
  21.     cin >> b >> a;
  22.     vector<vector<long>> mtx(n+1);
  23.     mtx[0].resize(m+1, 0);
  24.     for (long i = 1; i <= n; ++i){
  25.         mtx[i].resize(m+1, 0);
  26.         for (long j = 1; j <= m; ++j){
  27.             cin >> mtx[i][j];
  28.             if (mtx[i][j] == -1)
  29.                 mtx[i][j] = INF;
  30.         }
  31.     }
  32.  
  33.     vector<vector<long>> pfs(n+1);
  34.     pfs[0].resize(m+1, 0);
  35.     for (long i = 1; i <= n; ++i){
  36.         pfs[i].resize(m+1, 0);
  37.         for (long j = 1; j <= m; ++j)
  38.             pfs[i][j] = pfs[i-1][j] + pfs[i][j-1] - pfs[i-1][j-1] + mtx[i][j];
  39.     }
  40.  
  41.     long miv = LLONG_MAX;
  42.     for (long i = 1; i <= n-a+1; ++i)
  43.         for (long j = 1; j <= m-b+1; ++j){
  44.             long rw1 = i, rw2 = i+a-1, cl1 = j, cl2 = j+b-1;
  45.             long val = pfs[rw1-1][cl1-1] + pfs[rw2][cl2] - pfs[rw2][cl1-1] - pfs[rw1-1][cl2];
  46.             if (val < miv)
  47.                 miv = val;
  48.         }
  49.  
  50.     cout << miv << nln;
  51.    
  52.     return 0;
  53. }
Advertisement
Add Comment
Please, Sign In to add comment