mr_dot_convict

10755-Garbage_heap-UVa-mr.convict

Jun 20th, 2019
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.83 KB | None | 0 0
  1. /*author* Priyanshu Shrivastav (from IIT Palakkad) *
  2.  * *_ __ ___  _ ______ ___  _ ____   ___  ___| |_  *
  3.  * | '_ ` _ \| '__/ __/ _ \| '_ \ \ / / |/ __| __| *
  4.  * | | | | | | | | (_| (_) | | | \ V /| | (__| |_  *
  5.  * |_| |_| |_|_|(_)___\___/|_| |_|\_/ |_|\___|\__| *
  6. When I wrote this, only God and I understood what I was doing
  7.  ** * * * * * * * Now, only God knows * * * * * * */
  8. #include         <bits/stdc++.h>
  9. using namespace std;
  10. #pragma GCC      optimize ("Ofast")
  11. #pragma GCC      optimize ("unroll-loops")
  12. #pragma GCC      target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
  13.  
  14. #define IOS      ios_base::sync_with_stdio(false); cin.tie (nullptr)
  15. #define PREC     cout.precision (10); cout << fixed
  16. #define bg(x)    " [ " << #x << " : " << (x) << " ]"
  17. #define x        first
  18. #define y        second
  19. using ll = long long;
  20.  
  21. #define debug(args...) { \
  22.    string _s = #args; replace(_s.begin(), _s.end(), ',', ' ');\
  23.    stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); \
  24. }
  25. void err(istream_iterator<string> it) { it->empty();
  26.    cerr << " (Line : " << __LINE__ << ")" << '\n';
  27. }
  28. template<typename T, typename... Args>
  29. void err(istream_iterator<string> it, T a, Args... args) {
  30.     cerr << " [ " <<  *it << " : " << a  << " ] "<< ' ';
  31.     err(++it, args...);
  32. }
  33. const int N = 21;
  34. ll mat[N][N][N];
  35. ll cum[N][N][N];
  36. int n, m, h; // row col height
  37.  
  38. ll sum_rect(int ax, int ay, int bx, int by, int k) {
  39.    ll ans = cum[bx][by][k];
  40.    if (ax > 0) ans -= cum[ax-1][by][k];
  41.    if (ay > 0) ans -= cum[bx][ay-1][k];
  42.    if (ax > 0 && ay > 0) ans += cum[ax-1][ay-1][k];
  43.    return ans;
  44. }
  45.  
  46. void rect_cum() {
  47.    // find height wise cumulative
  48.    for (int k = 0; k < h; ++k)
  49.       for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j)
  50.       {
  51.          ll tmp = 1ll*mat[i][j][k];
  52.          if (i > 0) tmp += cum[i-1][j][k];
  53.          if (j > 0) tmp += cum[i][j-1][k];
  54.          if (i > 0 && j > 0) tmp -= cum[i-1][j-1][k];
  55.          cum[i][j][k] = tmp;
  56.       }
  57. }
  58.  
  59. void solve() {
  60.    rect_cum();
  61.    //apply kadane's method
  62.    ll mx_sum = LLONG_MIN;
  63.    for (int il = 0; il < n; ++il) for (int ih = il; ih < n; ++ih)  // o(N^4 * N)
  64.       for (int jl = 0; jl < m; ++jl) for (int jh = jl; jh < m; ++jh)
  65.       {
  66.          ll cur_sum = 0;
  67.          for (int k = 0; k < h; ++k) {
  68.             cur_sum += sum_rect(il, jl, ih, jh, k);
  69.             mx_sum = max(cur_sum, mx_sum);
  70.             if (cur_sum < 0) cur_sum = 0;
  71.          }
  72.       }
  73.  
  74.    cout << mx_sum << '\n';
  75. }
  76.  
  77. void read() {
  78.    cin >> n >> m >> h;
  79.    for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j)
  80.       for (int k = 0; k < h; ++k) cin >> mat[i][j][k];
  81. }
  82.  
  83. signed main() {
  84.    IOS; PREC;
  85.    int tc;
  86.    cin >> tc;
  87.    for (int i = 0; i < tc; ++i) {
  88.       if (i) cout << '\n';
  89.       read();
  90.       solve();
  91.    }
  92.  
  93.    return EXIT_SUCCESS;
  94. }
Add Comment
Please, Sign In to add comment