Submission #594572

# Submission time Handle Problem Language Result Execution time Memory
594572 2022-07-12T17:03:04 Z piOOE Sandcastle 2 (JOI22_ho_t5) C++17
100 / 100
1208 ms 4748 KB
#include <bits/stdc++.h>

using namespace std;

using ll = long long;

//O(min(H, W) * H * W)
//I also came up with O((H * W)^2) solution, but didn't know how to optimize it to O(min(H, W) * H * W)
//so now this is Radewoosh's solution

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, m;
    cin >> n >> m;

    bool swapped = false;
    if (n > m) {
        swapped = true;
        swap(n, m);
    }

    vector<vector<int>> a(n, vector<int>(m));
    vector<int> yy;
    for (int i = 0; i < (swapped ? m : n); ++i) {
        for (int j = 0; j < (swapped ? n : m); ++j) {
            if (!swapped) {
                cin >> a[i][j];
                yy.push_back(a[i][j]);
            } else {
                cin >> a[j][i];
                yy.push_back(a[j][i]);
            }
        }
    }

    sort(yy.begin(), yy.end());
    yy.resize(unique(yy.begin(), yy.end()) - yy.begin());

    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            a[i][j] = lower_bound(yy.begin(), yy.end(), a[i][j]) - yy.begin() + 1;
        }
    }

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

    int ans = 0;
    int diff[16][n][m];

    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            for (int mask = 0; mask < 16; ++mask) {
                int lower = 0;
                bool big = false;
                for (int k = 0; k < 4; ++k) {
                    if (mask >> k & 1) {
                        int nx = i + dx[k];
                        int ny = j + dy[k];
                        if (nx >= 0 && nx < n && ny >= 0 && ny < m) {
                            if (a[nx][ny] < a[i][j]) {
                                lower = max(lower, a[nx][ny]);
                            } else {
                                big = true;
                            }
                        }
                    }
                }
                diff[mask][i][j] = a[i][j] - lower + (big ? 0 : n * m + 1 - a[i][j]);
            }
        }
    }

    //now handle rectangles with width or length = 1
    for (int k = 0; k < n; ++k) {
        for (int i = 0; i < m;) {
            int j = i + 1;
            while (j < m && a[k][j] < a[k][j - 1]) {
                j += 1;
            }
            int len = j - i;
            ans += len * ((ll) len - 1) / 2;
            i = j;
        }

        for (int i = m - 1; i > -1;) {
            int j = i - 1;
            while (j > -1 && a[k][j] < a[k][j + 1]) {
                j -= 1;
            }
            int len = i - j;
            ans += len * ((ll) len - 1) / 2;
            i = j;
        }
    }
    for (int k = 0; k < m; ++k) {
        for (int i = 0; i < n;) {
            int j = i + 1;
            while (j < n && a[j][k] < a[j - 1][k]) {
                j += 1;
            }
            int len = j - i;
            ans += len * ((ll) len - 1) / 2;
            i = j;
        }

        for (int i = n - 1; i > -1;) {
            int j = i - 1;
            while (j > -1 && a[j][k] < a[j + 1][k]) {
                j -= 1;
            }
            int len = i - j;
            ans += len * ((ll) len - 1) / 2;
            i = j;
        }
    }
    ans += n * m;

    int mid[n][m], L[n][m], R[n][m];

    for (int y = 0; y < m; ++y) {
        for (int x = 0; x < n; ++x) {
            mid[x][y] = (x ? mid[x - 1][y] : 0) + diff[15][x][y];
            L[x][y] = (x ? L[x - 1][y] : 0) + diff[7][x][y];
            R[x][y] = (x ? R[x - 1][y] : 0) + diff[13][x][y];
        }
    }

    //main part!
    for (int x1 = 0; x1 < n; ++x1) {
        for (int x2 = x1 + 1; x2 < n; ++x2) {
            vector<int> valMid(m), valR(m), valL(m);
            for (int y = 0; y < m; ++y) {
                valMid[y] = mid[x2 - 1][y] - mid[x1][y] + diff[14][x1][y] + diff[11][x2][y];
                valR[y] = R[x2 - 1][y] - R[x1][y] + diff[12][x1][y] + diff[9][x2][y];
                valL[y] = L[x2 - 1][y] - L[x1][y] + diff[6][x1][y] + diff[3][x2][y];
            }
            map<int, int> cnt;
            int prefix = 0;
            for (int y1 = 0; y1 < m; ++y1) {
                if (y1 > 0) {
                    prefix += valMid[y1 - 1];
                    ++cnt[prefix - valL[y1 - 1]];
                }
                ans += cnt[prefix + valR[y1] - (n * m + 1)];
            }
        }
    }
    cout << ans;
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 18 ms 4620 KB Output is correct
3 Correct 21 ms 4556 KB Output is correct
4 Correct 19 ms 4620 KB Output is correct
5 Correct 19 ms 4572 KB Output is correct
6 Correct 23 ms 4624 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 4 ms 448 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 5 ms 340 KB Output is correct
14 Correct 3 ms 340 KB Output is correct
15 Correct 4 ms 340 KB Output is correct
16 Correct 5 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 4 ms 448 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 5 ms 340 KB Output is correct
14 Correct 3 ms 340 KB Output is correct
15 Correct 4 ms 340 KB Output is correct
16 Correct 5 ms 340 KB Output is correct
17 Correct 3 ms 852 KB Output is correct
18 Correct 28 ms 912 KB Output is correct
19 Correct 11 ms 852 KB Output is correct
20 Correct 5 ms 852 KB Output is correct
21 Correct 24 ms 920 KB Output is correct
22 Correct 37 ms 852 KB Output is correct
23 Correct 39 ms 908 KB Output is correct
24 Correct 36 ms 852 KB Output is correct
25 Correct 52 ms 852 KB Output is correct
26 Correct 51 ms 852 KB Output is correct
27 Correct 50 ms 852 KB Output is correct
28 Correct 44 ms 852 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 4 ms 448 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 5 ms 340 KB Output is correct
14 Correct 3 ms 340 KB Output is correct
15 Correct 4 ms 340 KB Output is correct
16 Correct 5 ms 340 KB Output is correct
17 Correct 3 ms 852 KB Output is correct
18 Correct 28 ms 912 KB Output is correct
19 Correct 11 ms 852 KB Output is correct
20 Correct 5 ms 852 KB Output is correct
21 Correct 24 ms 920 KB Output is correct
22 Correct 37 ms 852 KB Output is correct
23 Correct 39 ms 908 KB Output is correct
24 Correct 36 ms 852 KB Output is correct
25 Correct 52 ms 852 KB Output is correct
26 Correct 51 ms 852 KB Output is correct
27 Correct 50 ms 852 KB Output is correct
28 Correct 44 ms 852 KB Output is correct
29 Correct 25 ms 4604 KB Output is correct
30 Correct 261 ms 4568 KB Output is correct
31 Correct 791 ms 4452 KB Output is correct
32 Correct 25 ms 4748 KB Output is correct
33 Correct 70 ms 4484 KB Output is correct
34 Correct 533 ms 4532 KB Output is correct
35 Correct 472 ms 3028 KB Output is correct
36 Correct 812 ms 4432 KB Output is correct
37 Correct 1081 ms 4448 KB Output is correct
38 Correct 1049 ms 4448 KB Output is correct
39 Correct 1032 ms 4444 KB Output is correct
40 Correct 1076 ms 4424 KB Output is correct
41 Correct 1163 ms 4444 KB Output is correct
42 Correct 994 ms 4424 KB Output is correct
43 Correct 1208 ms 4420 KB Output is correct
44 Correct 986 ms 4432 KB Output is correct