Submission #617301


Source Code Expand

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <cfloat>
#include <ctime>
#include <cassert>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <numeric>
#include <list>
#include <iomanip>
#include <fstream>
#include <bitset>

using namespace std;

#define foreach(it, c) for (__typeof__((c).begin()) it=(c).begin(); it != (c).end(); ++it)
template <typename T> void print_container(ostream& os, const T& c) { const char* _s = " "; if (!c.empty()) { __typeof__(c.begin()) last = --c.end(); foreach (it, c) { os << *it; if (it != last) os << _s; } } }
template <typename T> ostream& operator<<(ostream& os, const vector<T>& c) { print_container(os, c); return os; }
template <typename T> ostream& operator<<(ostream& os, const set<T>& c) { print_container(os, c); return os; }
template <typename T> ostream& operator<<(ostream& os, const multiset<T>& c) { print_container(os, c); return os; }
template <typename T> ostream& operator<<(ostream& os, const deque<T>& c) { print_container(os, c); return os; }
template <typename T, typename U> ostream& operator<<(ostream& os, const map<T, U>& c) { print_container(os, c); return os; }
template <typename T, typename U> ostream& operator<<(ostream& os, const pair<T, U>& p) { os << "(" << p.first << ", " << p.second << ")"; return os; }

template <typename T> void print(T a, int n, const string& split = " ") { for (int i = 0; i < n; i++) { cout << a[i]; if (i + 1 != n) cout << split; } cout << endl; }
template <typename T> void print2d(T a, int w, int h, int width = -1, int br = 0) { for (int i = 0; i < h; ++i) { for (int j = 0; j < w; ++j) { if (width != -1) cout.width(width); cout << a[i][j] << ' '; } cout << endl; } while (br--) cout << endl; }
template <typename T> void input(T& a, int n) { for (int i = 0; i < n; ++i) cin >> a[i]; }
#define dump(v) (cout << #v << ": " << v << endl)

#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define erep(i, n) for (int i = 0; i <= (int)(n); ++i)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define clr(a, x) memset(a, x, sizeof(a))
#define sz(a) ((int)(a).size())
#define mp(a, b) make_pair(a, b)
#define ten(n) ((long long)(1e##n))

template <typename T, typename U> void upmin(T& a, const U& b) { a = min<T>(a, b); }
template <typename T, typename U> void upmax(T& a, const U& b) { a = max<T>(a, b); }
template <typename T> void uniq(T& a) { sort(a.begin(), a.end()); a.erase(unique(a.begin(), a.end()), a.end()); }
template <class T> string to_s(const T& a) { ostringstream os; os << a; return os.str(); }
template <class T> T to_T(const string& s) { istringstream is(s); T res; is >> res; return res; }
void fast_io() { cin.tie(0); ios::sync_with_stdio(false); }
bool in_rect(int x, int y, int w, int h) { return 0 <= x && x < w && 0 <= y && y < h; }

typedef long long ll;
typedef pair<int, int> pint;

const int dx[] = { 0, 1, 0, -1 };
const int dy[] = { 1, 0, -1, 0 };


template <typename T>
class Sum2d 
{
public:
    int width, height;
    vector<vector<T>> cum;
    Sum2d(const vector<vector<T>>& a)
        : width(a[0].size()), height(a.size()), cum(height + 1, vector<T>(width + 1))
    {
        for (int y = 0; y < height; ++y)
            for (int x = 0; x < width; ++x)
                cum[y + 1][x + 1] = cum[y][x + 1] + cum[y + 1][x] - cum[y][x] + a[y][x];
    }
    Sum2d() {}

    T get_sum(int x1, int x2, int y1, int y2) const
    {
        assert(0 <= x1 && x1 <= x2 && x2 <= width);
        assert(0 <= y1 && y1 <= y2 && y2 <= height);
        return cum[y2][x2] - cum[y1][x2] - cum[y2][x1] + cum[y1][x1];
    }

    T get_sum_by_len(int x, int y, int w, int h) const
    {
        assert(0 <= x && x + w <= width);
        assert(0 <= y && y + h <= height);
        return cum[y + h][x + w] - cum[y][x + w] - cum[y + h][x] + cum[y][x];
    }
};
int main()
{
    fast_io();

    int h, w;
    cin >> h >> w;
    vector<vector<ll>> b(h, vector<ll>(w));
    rep(y, h) rep(x, w)
        cin >> b[y][x];

    Sum2d<ll> sum(b);
    ll max_left[333], max_right[333], max_down[333], max_up[333];

    {
        max_left[0] = -ten(18);
        for (int high_x = 1; high_x <= w; ++high_x)
        {
            max_left[high_x] = max_left[high_x - 1];
            rep(low_x, high_x)
            {
                ll max_b = -ten(18);
                ll min_b = ten(18);
                rep(high_y, h + 1)
                {
                    ll cur = sum.get_sum(low_x, high_x, 0, high_y);
                    upmax(max_b, cur - min_b);
                    upmin(min_b, cur);
                }
                upmax(max_left[high_x], max_b);
            }
        }
    }
    {
        max_right[w] = -ten(18);
        for (int low_x = w - 1; low_x >= 0; --low_x)
        {
            max_right[low_x] = max_right[low_x + 1];
            for (int high_x = low_x + 1; high_x <= w; ++high_x)
            {
                ll max_b = -ten(18);
                ll min_b = ten(18);
                rep(high_y, h + 1)
                {
                    ll cur = sum.get_sum(low_x, high_x, 0, high_y);
                    upmax(max_b, cur - min_b);
                    upmin(min_b, cur);
                }
                upmax(max_right[low_x], max_b);
            }
        }
    }
    {
        max_down[0] = -ten(18);
        for (int high_y = 1; high_y <= h; ++high_y)
        {
            max_down[high_y] = max_down[high_y - 1];
            rep(low_y, high_y)
            {
                ll max_b = -ten(18);
                ll min_b = ten(18);
                rep(high_x, w + 1)
                {
                    ll cur = sum.get_sum(0, high_x, low_y, high_y);
                    upmax(max_b, cur - min_b);
                    upmin(min_b, cur);
                }
                upmax(max_down[high_y], max_b);
            }
        }
    }
    {
        max_up[h] = -ten(18);
        for (int low_y = h - 1; low_y >= 0; --low_y)
        {
            max_up[low_y] = max_up[low_y + 1];
            for (int high_y = low_y + 1; high_y <= h; ++high_y)
            {
                ll max_b = -ten(18);
                ll min_b = ten(18);
                rep(high_x, w + 1)
                {
                    ll cur = sum.get_sum(0, high_x, low_y, high_y);
                    upmax(max_b, cur - min_b);
                    upmin(min_b, cur);
                }
                upmax(max_up[low_y], max_b);
            }
        }
    }
//     print(max_left, w + 1);
//     print(max_right, w + 1);
//     print(max_down, h + 1);
//     print(max_up, h + 1);

    ll res = -ten(18);
    {
        for (int high_x = 1; high_x <= w; ++high_x)
        {
            rep(low_x, high_x)
            {
                ll min_b = ten(18);
                rep(high_y, h + 1)
                {
                    ll cur = sum.get_sum(low_x, high_x, 0, high_y);
                    upmax(res, cur - min_b + max(max(max_left[low_x], max_right[high_x]), max_up[high_y]));
                    upmin(min_b, cur);
                }
            }
        }
    }
    cout << res << endl;
}

Submission Info

Submission Time
Task D - 庭園
User takapt
Language C++11 (GCC 4.9.2)
Score 100
Code Size 7507 Byte
Status AC
Exec Time 367 ms
Memory 2084 KB

Judge Result

Set Name Sample Subtask1 All
Score / Max Score 0 / 0 50 / 50 50 / 50
Status
AC × 4
AC × 15
AC × 30
Set Name Test Cases
Sample sample0.txt, sample1.txt, sample2.txt, sample3.txt
Subtask1 subtask0_0.txt, subtask0_1.txt, subtask0_10.txt, subtask0_11.txt, subtask0_12.txt, subtask0_13.txt, subtask0_14.txt, subtask0_2.txt, subtask0_3.txt, subtask0_4.txt, subtask0_5.txt, subtask0_6.txt, subtask0_7.txt, subtask0_8.txt, subtask0_9.txt
All subtask0_0.txt, subtask0_1.txt, subtask0_10.txt, subtask0_11.txt, subtask0_12.txt, subtask0_13.txt, subtask0_14.txt, subtask0_2.txt, subtask0_3.txt, subtask0_4.txt, subtask0_5.txt, subtask0_6.txt, subtask0_7.txt, subtask0_8.txt, subtask0_9.txt, subtask1_0.txt, subtask1_1.txt, subtask1_10.txt, subtask1_11.txt, subtask1_12.txt, subtask1_13.txt, subtask1_14.txt, subtask1_2.txt, subtask1_3.txt, subtask1_4.txt, subtask1_5.txt, subtask1_6.txt, subtask1_7.txt, subtask1_8.txt, subtask1_9.txt
Case Name Status Exec Time Memory
sample0.txt AC 26 ms 924 KB
sample1.txt AC 26 ms 928 KB
sample2.txt AC 25 ms 916 KB
sample3.txt AC 25 ms 924 KB
subtask0_0.txt AC 29 ms 868 KB
subtask0_1.txt AC 29 ms 808 KB
subtask0_10.txt AC 28 ms 920 KB
subtask0_11.txt AC 28 ms 804 KB
subtask0_12.txt AC 26 ms 796 KB
subtask0_13.txt AC 26 ms 800 KB
subtask0_14.txt AC 26 ms 920 KB
subtask0_2.txt AC 28 ms 800 KB
subtask0_3.txt AC 26 ms 800 KB
subtask0_4.txt AC 27 ms 916 KB
subtask0_5.txt AC 28 ms 764 KB
subtask0_6.txt AC 27 ms 920 KB
subtask0_7.txt AC 28 ms 796 KB
subtask0_8.txt AC 28 ms 804 KB
subtask0_9.txt AC 28 ms 808 KB
subtask1_0.txt AC 283 ms 1832 KB
subtask1_1.txt AC 335 ms 1948 KB
subtask1_10.txt AC 266 ms 1824 KB
subtask1_11.txt AC 351 ms 2080 KB
subtask1_12.txt AC 295 ms 1884 KB
subtask1_13.txt AC 367 ms 2076 KB
subtask1_14.txt AC 281 ms 1952 KB
subtask1_2.txt AC 335 ms 2084 KB
subtask1_3.txt AC 277 ms 1836 KB
subtask1_4.txt AC 308 ms 1884 KB
subtask1_5.txt AC 335 ms 1960 KB
subtask1_6.txt AC 313 ms 1956 KB
subtask1_7.txt AC 307 ms 1968 KB
subtask1_8.txt AC 353 ms 2080 KB
subtask1_9.txt AC 314 ms 1960 KB