Submission #43887409


Source Code Expand

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

#ifndef ONLINE_JUDGE
  #include "/home/muhesh/cp/debug.h"
#endif

#define int long long
#define vt vector
#define ar array
#define nl '\n'

typedef long double ld;
typedef pair<int, int> pi;
typedef pair<ld, ld> pd;

typedef vt<int> vi;
typedef vt<ld> vd;
typedef vt<pi> vpi;
typedef vt<string> vs;
typedef vt<vi> vvi;

#define r_ep(i, a, b, s) for(int i = (a); (s) > 0 ? i < (b) : i > (b); i += (s))
#define rep1(e) r_ep(i, 0, e, 1)
#define rep2(i, e) r_ep(i, 0, e, 1)
#define rep3(i, b, e) r_ep(i, b, e, 1)
#define rep4(i, b, e, s) r_ep(i, b, e, s)
#define get5(a, b, c, d, e, ...) e
#define repc(...) get5(__VA_ARGS__, rep4, rep3, rep2, rep1)
#define rep(...) repc(__VA_ARGS__)(__VA_ARGS__)
#define each(i, a) for (auto &i: a)

#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define amin(a, b) a = min(a, b)
#define amax(a, b) a = max(a, b)
#define sz(x) (int) (x).size()
#define bg(x) (x).begin()
#define lb lower_bound
#define ub upper_bound
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define mt make_tuple
#define fr first
#define sc second
#define ft front()
#define bk back()

inline namespace IO {
  string to_string(char c) { return string(1, c); }
  string to_string(bool b) { return b ? "true" : "false"; }
  string to_string(const char * s) { return string(s); }
  string to_string(string s) { return s; }
  template<class T>
  string to_string(T x) { return std::to_string(x); }
  template<class A, class B>
  string to_string(pair<A, B> &a) { 
    return to_string(a.fr) + " " + to_string(a.sc); 
  }

  template<class T> void read(T &x) { cin >> x; }
  void read(float &d) { string t; read(t); d = stof(t); }
  void read(double &d) { string t; read(t); d = stod(t); }
  void read(ld &d) { string t; read(t); d = stold(t); }
  template<class A, class B> void read(pair<A, B> &a) {
    read(a.fr); read(a.sc);
  }
  template<class A> void read(vt<A> &a) { each(i, a) read(i); }
  template<class H, class... T> void read(H &h, T&... t) {
    read(h); read(t...);
  }

  template<class T> void write(T x) { cout << to_string(x); }
  template<class H, class... T> void write(const H &h, const T&... t) {
    write(h); write(t...); 
  }

  void print() { write("\n"); }
  template<class T> void print(vt<T> x) { 
    each(i, x) { write(i); write(" "); }
    print();
  }
  template<class H, class... T> void print(const H &h, const T&... t) {
    write(h); if (sizeof...(t)) write(' '); print(t...);
  }
}

inline namespace FileIO {
  void setIn(string s) { freopen(s.c_str(), "r", stdin); }
  void setOut(string s) { freopen(s.c_str(), "w", stdout); }
  void setIO(string s = "") {
    ios::sync_with_stdio(false);
    cin.tie(0);
    if (!s.empty()) setIn(s + ".in"), setOut(s + ".out");
  }
}

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

const int MOD1 = 1e9 + 7;
const int MOD2 = 998244353;
const int MOD = MOD1;
// const int INF = 2e18;
const double PI = acos(-1.0);

struct chash { // use most bits rather than just the lowest ones
  const uint64_t C = (long long) (2e18 * PI) + 71; // large odd number
  const int RANDOM = rng(); // random 32-bit number
  long long operator() (long long x) const {
    // https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html
    return __builtin_bswap64((x ^ RANDOM) * C);
  }
};

template<class T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, 
tree_order_statistics_node_update>;

template<class T>
using ordered_multiset = tree<pair<T, T>, null_type, less<pair<T, T>>, 
rb_tree_tag, tree_order_statistics_node_update>;

template<class K, class V> using ht = gp_hash_table<K, V, chash>;
template<class K> using hash_set = ht<K, null_type>;
template<class K, class V> using hash_table = ht<K, V>;

class PrefixSumMatrix {
 private:
  int n, m;
  vector<vector<int>> pa;
  bool is_zero_based;
  bool is_lower_left_top_right;
  
 public:
  void build(const vector<vector<int>> &a) {
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= m; j++) {
        pa[i][j] = (
          (is_zero_based ? a[i - 1][j - 1] : a[i][j]) +
          pa[i - 1][j] + 
          pa[i][j - 1] - 
          pa[i - 1][j - 1]
        );
      }
    }
  } 
  
  int get(int a, int b, int c, int d) {
    if (is_zero_based) a++; b++; c++; d++;
    if (is_lower_left_top_right) swap(b, d);
    return (
      pa[c][d] - 
      pa[a - 1][d] - 
      pa[c][b - 1] +
      pa[a - 1][b - 1]
    );
  }

  PrefixSumMatrix() { }

  PrefixSumMatrix(
    const vector<vector<int>> a, 
    bool is_zero_based = true,
    bool is_lower_left_top_right = false
  ) {
    int n = a.size(), m = a[0].size();
    this -> is_zero_based = is_zero_based;
    this -> is_lower_left_top_right = is_lower_left_top_right;
    this -> n = n;
    this -> m = m;
    pa.assign(n + 1, vector<int>(m + 1, 0));
    build(a);
  }
};

vector<int> get_prev_smaller_element_idx(const vector<int> &a) {
  int n = size(a);
  vector<int> prev_smaller_element_idx(n);
  stack<int> st;
  for (int i = 0; i < n; i++) {
    while (!st.empty() && a[st.top()] >= a[i])
      st.pop();
    prev_smaller_element_idx[i] = (st.empty() ? -1 : st.top());
    st.push(i);
  }
  return prev_smaller_element_idx;
}

vector<int> get_next_smaller_element_idx(const vector<int> &a) {
  int n = size(a);
  vector<int> next_smaller_element_idx(n);
  stack<int> st;
  for (int i = n - 1; i >= 0; i--) {
    while (!st.empty() && a[st.top()] >= a[i])
      st.pop();
    next_smaller_element_idx[i] = (st.empty() ? n : st.top());
    st.push(i);
  }
  return next_smaller_element_idx;
}

int get_maximum_sum_histogram(const vector<int>& a, PrefixSumMatrix& psm, int row) {
  auto prev_smaller_element_idx = get_prev_smaller_element_idx(a);
  auto next_smaller_element_idx = get_next_smaller_element_idx(a);

  int n = size(a);
  int maximum_area = 0, mx_sum = 0;
  for (int i = 0; i < n; i++) {
    int width = next_smaller_element_idx[i] - prev_smaller_element_idx[i] - 1;
    int height = a[i];
    int area = height * width;
    int sum = psm.get(row - height + 1, prev_smaller_element_idx[i] + 1, row, next_smaller_element_idx[i] - 1);
    maximum_area = max(area, maximum_area);
    mx_sum = max(mx_sum, sum);
  }
  return mx_sum;
}

int get_mx_area_from_rectangle(const vector<vector<int>>& matrix, int target, PrefixSumMatrix& psm) {
  int n = size(matrix);
  if (n == 0) return 0;
  
  int m = size(matrix.front());
  vector<int> histograms(m);
  for (int j = 0; j < m; j++) {
    histograms[j] = matrix[0][j] >= target;
  }
  
  int mx_area = get_maximum_sum_histogram(histograms, psm, 0);
  for (int i = 1; i < n; i++) {
    for (int j = 0; j < m; j++) {
      histograms[j] = matrix[i][j] >= target ? histograms[j] + 1 : 0;
    }
    mx_area = max(mx_area, get_maximum_sum_histogram(histograms, psm, i));
  }
  return mx_area;
}

void solve() {
  int n, m; read(n, m);
  vvi a(n, vi(m)); read(a);
  PrefixSumMatrix psm(a);
  int ans = 0;
  rep(val, 1, 301) { // O(300 * n * m)
    int sum_rectangluar_area = get_mx_area_from_rectangle(a, val, psm);
    amax(ans, sum_rectangluar_area * val);
  }
  print(ans);
}

int32_t main() {
  setIO();

  int tc = 1;
  // read(tc);
  rep(i, 1, tc + 1) {
    // write("Case #", i, ": ");
    solve();
  }

  return 0;
}

Submission Info

Submission Time
Task G - One More Grid Task
User MuheshKumar
Language C++ (GCC 9.2.1)
Score 575
Code Size 7661 Byte
Status AC
Exec Time 666 ms
Memory 5232 KiB

Compile Error

./Main.cpp: In member function ‘long long int PrefixSumMatrix::get(long long int, long long int, long long int, long long int)’:
./Main.cpp:154:5: warning: this ‘if’ clause does not guard... [-Wmisleading-indentation]
  154 |     if (is_zero_based) a++; b++; c++; d++;
      |     ^~
./Main.cpp:154:29: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the ‘if’
  154 |     if (is_zero_based) a++; b++; c++; d++;
      |                             ^
./Main.cpp: In function ‘void FileIO::setIn(std::string)’:
./Main.cpp:94:33: warning: ignoring return value of ‘FILE* freopen(const char*, const char*, FILE*)’, declared with attribute warn_unused_result [-Wunused-result]
   94 |   void setIn(string s) { freopen(s.c_str(), "r", stdin); }
      |                          ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
./Main.cpp: In function ‘void FileIO::setOut(std::string)’:
./Main.cpp:95:34: warning: ignoring return value of ‘FILE* freopen(const char*, const char*, FILE*)’, declared with attribute warn_unused_result [-Wunused-result]
   95 |   void setOut(string s) { freopen(s.c_str(), "w", stdout); }
      |                           ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 575 / 575
Status
AC × 3
AC × 94
Set Name Test Cases
Sample sample_01.txt, sample_02.txt, sample_03.txt
All hack_01.txt, hack_02.txt, hack_03.txt, hack_04.txt, hack_05.txt, hack_06.txt, hack_07.txt, hack_08.txt, hack_09.txt, hack_10.txt, hack_11.txt, hack_12.txt, hack_13.txt, hack_14.txt, hack_15.txt, hack_16.txt, hack_17.txt, hack_18.txt, hack_19.txt, hack_20.txt, hack_21.txt, hack_22.txt, hack_23.txt, hack_24.txt, sample_01.txt, sample_02.txt, sample_03.txt, test_01.txt, test_02.txt, test_03.txt, test_04.txt, test_05.txt, test_06.txt, test_07.txt, test_08.txt, test_09.txt, test_10.txt, test_11.txt, test_12.txt, test_13.txt, test_14.txt, test_15.txt, test_16.txt, test_17.txt, test_18.txt, test_19.txt, test_20.txt, test_21.txt, test_22.txt, test_23.txt, test_24.txt, test_25.txt, test_26.txt, test_27.txt, test_28.txt, test_29.txt, test_30.txt, test_31.txt, test_32.txt, test_33.txt, test_34.txt, test_35.txt, test_36.txt, test_37.txt, test_38.txt, test_39.txt, test_40.txt, test_41.txt, test_42.txt, test_43.txt, test_44.txt, test_45.txt, test_46.txt, test_47.txt, test_48.txt, test_49.txt, test_50.txt, test_51.txt, test_52.txt, test_53.txt, test_54.txt, test_55.txt, test_56.txt, test_57.txt, test_58.txt, test_59.txt, test_60.txt, test_61.txt, test_62.txt, test_63.txt, test_64.txt, test_65.txt, test_66.txt, test_67.txt
Case Name Status Exec Time Memory
hack_01.txt AC 297 ms 5152 KiB
hack_02.txt AC 294 ms 5128 KiB
hack_03.txt AC 292 ms 5160 KiB
hack_04.txt AC 296 ms 5208 KiB
hack_05.txt AC 296 ms 5196 KiB
hack_06.txt AC 304 ms 5212 KiB
hack_07.txt AC 297 ms 5192 KiB
hack_08.txt AC 295 ms 5128 KiB
hack_09.txt AC 303 ms 5128 KiB
hack_10.txt AC 292 ms 5152 KiB
hack_11.txt AC 300 ms 5148 KiB
hack_12.txt AC 341 ms 5160 KiB
hack_13.txt AC 315 ms 5212 KiB
hack_14.txt AC 311 ms 5212 KiB
hack_15.txt AC 320 ms 5164 KiB
hack_16.txt AC 301 ms 5192 KiB
hack_17.txt AC 318 ms 5156 KiB
hack_18.txt AC 299 ms 5192 KiB
hack_19.txt AC 303 ms 5228 KiB
hack_20.txt AC 308 ms 5160 KiB
hack_21.txt AC 305 ms 5140 KiB
hack_22.txt AC 304 ms 5152 KiB
hack_23.txt AC 314 ms 5092 KiB
hack_24.txt AC 322 ms 5152 KiB
sample_01.txt AC 3 ms 3552 KiB
sample_02.txt AC 5 ms 3532 KiB
sample_03.txt AC 4 ms 3628 KiB
test_01.txt AC 3 ms 3576 KiB
test_02.txt AC 3 ms 3504 KiB
test_03.txt AC 3 ms 3624 KiB
test_04.txt AC 4 ms 3532 KiB
test_05.txt AC 2 ms 3576 KiB
test_06.txt AC 4 ms 3628 KiB
test_07.txt AC 3 ms 3664 KiB
test_08.txt AC 4 ms 3576 KiB
test_09.txt AC 2 ms 3552 KiB
test_10.txt AC 653 ms 5164 KiB
test_11.txt AC 117 ms 3788 KiB
test_12.txt AC 651 ms 5160 KiB
test_13.txt AC 359 ms 4304 KiB
test_14.txt AC 656 ms 5148 KiB
test_15.txt AC 336 ms 4252 KiB
test_16.txt AC 651 ms 5192 KiB
test_17.txt AC 97 ms 3856 KiB
test_18.txt AC 652 ms 5208 KiB
test_19.txt AC 109 ms 3924 KiB
test_20.txt AC 665 ms 5216 KiB
test_21.txt AC 91 ms 3732 KiB
test_22.txt AC 664 ms 5152 KiB
test_23.txt AC 446 ms 4608 KiB
test_24.txt AC 664 ms 5128 KiB
test_25.txt AC 101 ms 3936 KiB
test_26.txt AC 663 ms 5136 KiB
test_27.txt AC 218 ms 3908 KiB
test_28.txt AC 666 ms 5212 KiB
test_29.txt AC 5 ms 3548 KiB
test_30.txt AC 634 ms 5212 KiB
test_31.txt AC 39 ms 3728 KiB
test_32.txt AC 636 ms 5096 KiB
test_33.txt AC 141 ms 3996 KiB
test_34.txt AC 633 ms 5116 KiB
test_35.txt AC 592 ms 5092 KiB
test_36.txt AC 633 ms 5128 KiB
test_37.txt AC 50 ms 3724 KiB
test_38.txt AC 631 ms 5140 KiB
test_39.txt AC 303 ms 4172 KiB
test_40.txt AC 285 ms 5168 KiB
test_41.txt AC 36 ms 3736 KiB
test_42.txt AC 312 ms 5212 KiB
test_43.txt AC 196 ms 4328 KiB
test_44.txt AC 291 ms 5152 KiB
test_45.txt AC 239 ms 4624 KiB
test_46.txt AC 294 ms 5196 KiB
test_47.txt AC 88 ms 3896 KiB
test_48.txt AC 345 ms 5208 KiB
test_49.txt AC 23 ms 3604 KiB
test_50.txt AC 309 ms 5212 KiB
test_51.txt AC 118 ms 3968 KiB
test_52.txt AC 343 ms 5160 KiB
test_53.txt AC 221 ms 4372 KiB
test_54.txt AC 363 ms 5212 KiB
test_55.txt AC 35 ms 3632 KiB
test_56.txt AC 367 ms 5192 KiB
test_57.txt AC 41 ms 3668 KiB
test_58.txt AC 422 ms 5120 KiB
test_59.txt AC 62 ms 3708 KiB
test_60.txt AC 510 ms 5136 KiB
test_61.txt AC 70 ms 3780 KiB
test_62.txt AC 300 ms 5232 KiB
test_63.txt AC 75 ms 4012 KiB
test_64.txt AC 311 ms 5164 KiB
test_65.txt AC 315 ms 5156 KiB
test_66.txt AC 291 ms 5136 KiB
test_67.txt AC 291 ms 5092 KiB