結果

問題 No.2509 Beam Shateki
ユーザー OnjoujiTokiOnjoujiToki
提出日時 2023-10-24 20:52:49
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 9,015 bytes
コンパイル時間 1,547 ms
コンパイル使用メモリ 146,220 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-09-24 16:04:18
合計ジャッジ時間 3,490 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 1 ms
6,940 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 WA -
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 WA -
testcase_08 AC 3 ms
6,944 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 4 ms
6,940 KB
testcase_14 WA -
testcase_15 AC 4 ms
6,944 KB
testcase_16 AC 4 ms
6,944 KB
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 5 ms
6,940 KB
testcase_20 WA -
testcase_21 AC 4 ms
6,944 KB
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 AC 4 ms
6,944 KB
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 AC 5 ms
6,940 KB
testcase_30 AC 4 ms
6,944 KB
testcase_31 AC 4 ms
6,940 KB
testcase_32 AC 4 ms
6,944 KB
testcase_33 AC 2 ms
6,944 KB
testcase_34 AC 3 ms
6,944 KB
testcase_35 AC 4 ms
6,940 KB
testcase_36 AC 2 ms
6,944 KB
testcase_37 AC 2 ms
6,944 KB
testcase_38 AC 3 ms
6,940 KB
testcase_39 AC 2 ms
6,940 KB
testcase_40 AC 3 ms
6,940 KB
testcase_41 AC 1 ms
6,940 KB
testcase_42 AC 2 ms
6,944 KB
testcase_43 AC 4 ms
6,944 KB
testcase_44 AC 4 ms
6,940 KB
testcase_45 AC 3 ms
6,944 KB
testcase_46 AC 2 ms
6,940 KB
testcase_47 AC 4 ms
6,944 KB
testcase_48 AC 3 ms
6,944 KB
testcase_49 AC 3 ms
6,940 KB
testcase_50 AC 3 ms
6,944 KB
testcase_51 AC 2 ms
6,944 KB
testcase_52 AC 3 ms
6,940 KB
testcase_53 WA -
testcase_54 WA -
testcase_55 AC 2 ms
6,940 KB
testcase_56 WA -
testcase_57 WA -
testcase_58 AC 1 ms
6,940 KB
testcase_59 AC 1 ms
6,940 KB
testcase_60 AC 2 ms
6,944 KB
testcase_61 AC 2 ms
6,944 KB
testcase_62 WA -
testcase_63 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstring>
#include <ctime>
#include <deque>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <unordered_map>
#include <unordered_set>

template <int mod>
struct ModInt {
  int x;
  ModInt() : x(0) {}
  ModInt(long long y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}
  ModInt& operator+=(const ModInt& p) {
    if ((x += p.x) >= mod) x -= mod;
    return *this;
  }
  ModInt& operator-=(const ModInt& p) {
    if ((x += mod - p.x) >= mod) x -= mod;
    return *this;
  }
  ModInt& operator*=(const ModInt& p) {
    x = (int)(1LL * x * p.x % mod);
    return *this;
  }
  ModInt& operator/=(const ModInt& p) {
    *this *= p.inverse();
    return *this;
  }
  ModInt& operator^=(long long p) {  // quick_pow here:3
    ModInt res = 1;
    for (; p; p >>= 1) {
      if (p & 1) res *= *this;
      *this *= *this;
    }
    return *this = res;
  }
  ModInt operator-() const { return ModInt(-x); }
  ModInt operator+(const ModInt& p) const { return ModInt(*this) += p; }
  ModInt operator-(const ModInt& p) const { return ModInt(*this) -= p; }
  ModInt operator*(const ModInt& p) const { return ModInt(*this) *= p; }
  ModInt operator/(const ModInt& p) const { return ModInt(*this) /= p; }
  ModInt operator^(long long p) const { return ModInt(*this) ^= p; }
  bool operator==(const ModInt& p) const { return x == p.x; }
  bool operator!=(const ModInt& p) const { return x != p.x; }
  explicit operator int() const { return x; }  // added by QCFium
  ModInt operator=(const int p) {
    x = p;
    return ModInt(*this);
  }  // added by QCFium
  ModInt inverse() const {
    int a = x, b = mod, u = 1, v = 0, t;
    while (b > 0) {
      t = a / b;
      a -= t * b;
      std::swap(a, b);
      u -= t * v;
      std::swap(u, v);
    }
    return ModInt(u);
  }
  friend std::ostream& operator<<(std::ostream& os, const ModInt<mod>& p) {
    return os << p.x;
  }
  friend std::istream& operator>>(std::istream& is, ModInt<mod>& a) {
    long long x;
    is >> x;
    a = ModInt<mod>(x);
    return (is);
  }
};

template <typename T>
struct SegmentTree {
  using Monoid = typename T::Monoid;

  explicit SegmentTree(int n) : SegmentTree(std::vector<Monoid>(n, T::id())) {}

  explicit SegmentTree(const std::vector<Monoid>& a) : n(a.size()), sz(1) {
    while (sz < n) sz <<= 1;
    data.assign(sz << 1, T::id());
    std::copy(a.begin(), a.end(), data.begin() + sz);
    for (int i = sz - 1; i > 0; --i) {
      data[i] = T::merge(data[i << 1], data[(i << 1) + 1]);
    }
  }

  void set(int idx, const Monoid val) {
    idx += sz;
    data[idx] = val;
    while (idx >>= 1)
      data[idx] = T::merge(data[idx << 1], data[(idx << 1) + 1]);
  }

  Monoid get(int left, int right) const {
    Monoid res_l = T::id(), res_r = T::id();
    for (left += sz, right += sz; left < right; left >>= 1, right >>= 1) {
      if (left & 1) res_l = T::merge(res_l, data[left++]);
      if (right & 1) res_r = T::merge(data[--right], res_r);
    }
    return T::merge(res_l, res_r);
  }

  Monoid operator[](const int idx) const { return data[idx + sz]; }

 private:
  const int n;
  int sz;  // sz + 原数组坐标 = 线段树里的编号,1 based
  std::vector<Monoid> data;
};

namespace monoid {

template <typename T>
struct RangeMinimumQuery {
  using Monoid = T;
  static constexpr Monoid id() { return std::numeric_limits<Monoid>::max(); }
  static Monoid merge(const Monoid& a, const Monoid& b) {
    return std::min(a, b);
  }
};

template <typename T>
struct RangeMaximumQuery {
  using Monoid = T;
  static constexpr Monoid id() { return std::numeric_limits<Monoid>::lowest(); }
  static Monoid merge(const Monoid& a, const Monoid& b) {
    return std::max(a, b);
  }
};

template <typename T>
struct RangeSumQuery {
  using Monoid = T;
  static constexpr Monoid id() { return 0; }
  static Monoid merge(const Monoid& a, const Monoid& b) { return a + b; }
};

template <typename T>
struct RangeOrQuery {
  using Monoid = T;
  static constexpr Monoid id() { return 0; }
  static Monoid merge(const Monoid& a, const Monoid& b) { return a | b; }
};
}  // namespace monoid
template <typename T>
struct DSU {
  std::vector<T> f, siz;
  DSU(int n) : f(n), siz(n, 1) { std::iota(f.begin(), f.end(), 0); }
  T leader(T x) {
    while (x != f[x]) x = f[x] = f[f[x]];
    return x;
  }
  bool same(T x, T y) { return leader(x) == leader(y); }
  bool merge(T x, T y) {
    x = leader(x);
    y = leader(y);
    if (x == y) return false;
    siz[x] += siz[y];
    f[y] = x;
    return true;
  }
  T size(int x) { return siz[leader(x)]; }
};
bool isPrime(long long number) {
  if (number != 2) {
    if (number < 2 || number % 2 == 0) {
      return false;
    }
    for (int i = 3; (i * i) <= number; i += 2) {
      if (number % i == 0) {
        return false;
      }
    }
  }
  return true;
}
std::pair<std::vector<int>, std::vector<int>> get_prime_factor_with_kinds(
    int n) {
  std::vector<int> prime_factors;
  std::vector<int> cnt;  // number of i_th factor
  for (int i = 2; i <= sqrt(n); i++) {
    if (n % i == 0) {
      prime_factors.push_back(i);
      cnt.push_back(0);
      while (n % i == 0) n /= i, cnt[(int)prime_factors.size() - 1]++;
    }
  }
  if (n > 1) prime_factors.push_back(n), cnt.push_back(1);
  assert(prime_factors.size() == cnt.size());
  return {prime_factors, cnt};
}
template <typename T>
struct FenwickTree {
  std::vector<T> bit;
  int n;
  FenwickTree(int _n) : n(_n), bit(_n) {}

  T sum(int r) {
    int ret = 0;
    for (; r >= 0; r = (r & (r + 1)) - 1) ret += bit[r];
    return ret;
  }

  T sum(int l, int r) {
    assert(l <= r);
    return sum(r) - sum(l - 1);
  }  // [l, r]

  void add(int idx, int delta) {
    for (; idx < n; idx = idx | (idx + 1)) bit[idx] += delta;
  }
};

using mint = ModInt<998244353>;
const int MOD = 998244353;
struct MComb {
  std::vector<mint> fact;
  std::vector<mint> inversed;
  MComb(int n) {  // O(n+log(mod))
    fact = std::vector<mint>(n + 1, 1);
    for (int i = 1; i <= n; i++) fact[i] = fact[i - 1] * mint(i);
    inversed = std::vector<mint>(n + 1);
    inversed[n] = fact[n] ^ (MOD - 2);
    for (int i = n - 1; i >= 0; i--)
      inversed[i] = inversed[i + 1] * mint(i + 1);
  }
  mint ncr(int n, int r) {
    if (n < r) return 0;
    return (fact[n] * inversed[r] * inversed[n - r]);
  }
  mint npr(int n, int r) { return (fact[n] * inversed[n - r]); }
  mint nhr(int n, int r) {
    assert(n + r - 1 < (int)fact.size());
    return ncr(n + r - 1, r);
  }
};

mint ncr(int n, int r) {
  mint res = 1;
  for (int i = n - r + 1; i <= n; i++) res *= i;
  for (int i = 1; i <= r; i++) res /= i;
  return res;
}

void solve() {
  int h, w;
  std::cin >> h >> w;
  std::vector b(h, std::vector<int>(w));
  for (int i = 0; i < h; i++) {
    for (int& j : b[i]) {
      std::cin >> j;
    }
  }
  bool swapped = false;
  if (h < w) std::swap(h, w), swapped = true;
  std::vector g(h, std::vector<int>(w));
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (swapped) {
        g[i][j] = b[j][i];
      } else
        g[i][j] = b[i][j];
    }
  }

  long long ans = 0;
  std::vector<long long> row(h), col(w);
  for (int i = 0; i < h; i++) {
    long long sum = 0;
    for (int j = 0; j < w; j++) {
      sum += g[i][j];
    }
    row[i] = sum;
  }

  for (int j = 0; j < w; j++) {
    long long sum = 0;
    for (int i = 0; i < h; i++) {
      sum += g[i][j];
    }
    col[j] = sum;
  }
  int x = 0, y = w - 1;
  while (x < h and y >= 0) {
    int i = x, j = y;
    long long sum = 0;
    std::vector<int> a(h), b(w);
    while (i >= 0 and j >= 0) {
      sum += g[i][j];
      a[i] = g[i][j];
      b[j] = g[i][j];
      i--, j--;
    }
    if (x < h - 1) {
      x++;
    } else if (x == h - 1) {
      y--;
    }
    for (int i = 0; i < h; i++) {
      ans = std::max(ans, sum + row[i] - a[i]);
    }
    for (int j = 0; j < w; j++) {
      ans = std::max(ans, sum + col[j] - b[j]);
    }
  }
  x = h - 1, y = w - 1;
  while (x >= 0 and y >= 0) {
    int i = x, j = y;
    long long sum = 0;
    std::vector<int> a(h), b(w);
    while (i >= 0 and j < w) {
      sum += g[i][j];
      a[i] = g[i][j];
      b[j] = g[i][j];
      i--, j++;
    }
    if (y > 0) {
      y--;
    } else if (y == 0) {
      x--;
    }
    for (int i = 0; i < h; i++) {
      ans = std::max(ans, sum + row[i] - a[i]);
    }
    for (int j = 0; j < w; j++) {
      ans = std::max(ans, sum + col[j] - b[j]);
    }
  }
  for (int i = 0; i < h; i++) {
    for (int j = i + 1; i < h; i++) {
      ans = std::max(ans, row[i] + row[j]);
    }
  }
  for (int j = 0; j < w; j++) {
    for (int i = j + 1; i < w; i++) {
      ans = std::max(ans, col[i] + col[j]);
    }
  }
  std::cout << ans << '\n';
}
int main() {
  int t = 1;
  // std::cin >> t;
  while (t--) solve();
}
0