Submission #632396

# Submission time Handle Problem Language Result Execution time Memory
632396 2022-08-19T22:34:06 Z Ooops_sorry Sandcastle 2 (JOI22_ho_t5) C++14
100 / 100
1593 ms 16372 KB
#include<bits/stdc++.h>

using namespace std;

mt19937 rnd(51);

#define ll long long
#define pb push_back
#define ld long double

const int N = 50010;
int n, m, INF;
vector<vector<int>> a, any;
vector<pair<int,int>> d{{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
vector<bool> used(4);
vector<vector<vector<int>>> side, two;
int mn[N], mx[N], cnt_up[N], sum_up[N], cnt_down[N], sum_down[N], cnt[N], sum[N];

bool check(int i, int j) {
    return i >= 0 && i < n && j >= 0 && j < m;
}

int get_max(int i, int j, vector<int> ban = {}) {
    int mx = -1;
    for (auto to : ban) {
        used[to] = 1;
    }
    for (int k = 0; k < 4; k++) {
        if (used[k]) {
            continue;
        }
        int x = i + d[k].first, y = j + d[k].second;
        if (check(x, y) && a[x][y] < a[i][j]) {
            mx = max(mx, a[x][y]);
        }
    }
    for (auto to : ban) {
        used[to] = 0;
    }
    return (mx == -1 ? 0 : a[i][j] - mx);
}

void build() {
    int h = max(n, m), w = min(n, m);
    a.resize(h);
    any.resize(h);
    side.resize(h);
    two.resize(h);
    for (int i = 0; i < h; i++) {
        a[i].resize(w);
        any[i].resize(w);
        side[i].resize(w);
        two[i].resize(w);
    }
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            side[i][j].resize(4);
            two[i][j].resize(4);
        }
    }
}

// 0 -> U
// 1 -> R
// 2 -> D
// 3 -> L

signed main() {
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
#endif // LOCAL
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> n >> m;
    INF = n * m;
    build();
    if (n < m) {
        swap(n, m);
        for (int j = 0; j < m; j++) {
            for (int i = 0; i < n; i++) {
                cin >> a[i][j];
            }
        }
    } else {
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                cin >> a[i][j];
            }
        }
    }
    ll ans = 0;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            any[i][j] = get_max(i, j);
            for (int k = 0; k < 4; k++) {
                side[i][j][k] = get_max(i, j, {k});
                two[i][j][k] = get_max(i, j, {k, (k + 1) % 4});
            }
        }
    }
    for (int j1 = 0; j1 < m; j1++) {
        for (int i = 0; i < n; i++) {
            mn[i] = mx[i] = a[i][j1];

            cnt_up[i] = (two[i][j1][3] == 0);
            sum_up[i] = two[i][j1][3];

            cnt[i] = (side[i][j1][3] == 0);
            sum[i] = side[i][j1][3];

            cnt_down[i] = (two[i][j1][2] == 0);
            sum_down[i] = two[i][j1][2];
        }
        for (int j2 = j1 + 1; j2 < m; j2++) {
            for (int i = 0; i < n; i++) {
                cnt_up[i] += (two[i][j2][0] == 0);
                sum_up[i] += two[i][j2][0];

                cnt[i] += (side[i][j2][1] == 0);
                sum[i] += side[i][j2][1];

                cnt_down[i] += (two[i][j2][1] == 0);
                sum_down[i] += two[i][j2][1];
            }
            for (int i = 0; i < n; i++) {
                mn[i] = min(mn[i], a[i][j2]);
                mx[i] = max(mx[i], a[i][j2]);
                if (j1 < j2 - 1) {
                    cnt_up[i] += (side[i][j2 - 1][0] == 0);
                    sum_up[i] += side[i][j2 - 1][0];

                    cnt_down[i] += (side[i][j2 - 1][2] == 0);
                    sum_down[i] += side[i][j2 - 1][2];

                    cnt[i] += (any[i][j2 - 1] == 0);
                    sum[i] += any[i][j2 - 1];
                }
            }
            for (int i1 = 0; i1 < n; i1++) {
                int cur_mn = mn[i1], cur_mx = mx[i1];
                int cntt = cnt_up[i1], summ = sum_up[i1];
                for (int i2 = i1 + 1; i2 < n; i2++) {
                    cur_mn = min(cur_mn, mn[i2]);
                    cur_mx = max(cur_mx, mx[i2]);
                    if (i2 - 1 > i1) {
                        cntt += cnt[i2 - 1];
                        summ += sum[i2 - 1];
                    }
                    if (cntt + cnt_down[i2] == 1 && summ + sum_down[i2] == cur_mx - cur_mn) {
                        ans++;
                    }
                }
            }
            for (int i = 0; i < n; i++) {
                cnt_up[i] -= (two[i][j2][0] == 0);
                sum_up[i] -= two[i][j2][0];

                cnt[i] -= (side[i][j2][1] == 0);
                sum[i] -= side[i][j2][1];

                cnt_down[i] -= (two[i][j2][1] == 0);
                sum_down[i] -= two[i][j2][1];
            }
        }
    }
    for (int i = 0; i < n; i++) {
        int last = 2, cnt = 0;
        for (int j = 1; j < m; j++) {
            if ((last ^ (a[i][j - 1] < a[i][j])) == 1) {
                ans += 1ll * cnt * (cnt + 1) / 2;
                cnt = 1;
            } else {
                cnt++;
            }
            last = (a[i][j - 1] < a[i][j]);
        }
        ans += 1ll * cnt * (cnt + 1) / 2;
    }
    for (int j = 0; j < m; j++) {
        int last = 2, cnt = 0;
        for (int i = 1; i < n; i++) {
            if ((last ^ (a[i - 1][j] < a[i][j])) == 1) {
                ans += 1ll * cnt * (cnt + 1) / 2;
                cnt = 1;
            } else {
                cnt++;
            }
            last = (a[i - 1][j] < a[i][j]);
        }
        ans += 1ll * cnt * (cnt + 1) / 2;
    }
    ans += n * m;
    cout << ans << endl;
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 38 ms 15964 KB Output is correct
3 Correct 35 ms 15728 KB Output is correct
4 Correct 41 ms 15948 KB Output is correct
5 Correct 38 ms 15908 KB Output is correct
6 Correct 48 ms 15956 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 388 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 388 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 2 ms 724 KB Output is correct
8 Correct 2 ms 772 KB Output is correct
9 Correct 3 ms 468 KB Output is correct
10 Correct 3 ms 464 KB Output is correct
11 Correct 2 ms 596 KB Output is correct
12 Correct 3 ms 596 KB Output is correct
13 Correct 3 ms 468 KB Output is correct
14 Correct 2 ms 468 KB Output is correct
15 Correct 3 ms 468 KB Output is correct
16 Correct 3 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 388 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 2 ms 724 KB Output is correct
8 Correct 2 ms 772 KB Output is correct
9 Correct 3 ms 468 KB Output is correct
10 Correct 3 ms 464 KB Output is correct
11 Correct 2 ms 596 KB Output is correct
12 Correct 3 ms 596 KB Output is correct
13 Correct 3 ms 468 KB Output is correct
14 Correct 2 ms 468 KB Output is correct
15 Correct 3 ms 468 KB Output is correct
16 Correct 3 ms 468 KB Output is correct
17 Correct 7 ms 2528 KB Output is correct
18 Correct 30 ms 1252 KB Output is correct
19 Correct 22 ms 1236 KB Output is correct
20 Correct 41 ms 1240 KB Output is correct
21 Correct 37 ms 1236 KB Output is correct
22 Correct 33 ms 1228 KB Output is correct
23 Correct 33 ms 1112 KB Output is correct
24 Correct 23 ms 1116 KB Output is correct
25 Correct 30 ms 1236 KB Output is correct
26 Correct 29 ms 1236 KB Output is correct
27 Correct 30 ms 1212 KB Output is correct
28 Correct 29 ms 1112 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 388 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 2 ms 724 KB Output is correct
8 Correct 2 ms 772 KB Output is correct
9 Correct 3 ms 468 KB Output is correct
10 Correct 3 ms 464 KB Output is correct
11 Correct 2 ms 596 KB Output is correct
12 Correct 3 ms 596 KB Output is correct
13 Correct 3 ms 468 KB Output is correct
14 Correct 2 ms 468 KB Output is correct
15 Correct 3 ms 468 KB Output is correct
16 Correct 3 ms 468 KB Output is correct
17 Correct 7 ms 2528 KB Output is correct
18 Correct 30 ms 1252 KB Output is correct
19 Correct 22 ms 1236 KB Output is correct
20 Correct 41 ms 1240 KB Output is correct
21 Correct 37 ms 1236 KB Output is correct
22 Correct 33 ms 1228 KB Output is correct
23 Correct 33 ms 1112 KB Output is correct
24 Correct 23 ms 1116 KB Output is correct
25 Correct 30 ms 1236 KB Output is correct
26 Correct 29 ms 1236 KB Output is correct
27 Correct 30 ms 1212 KB Output is correct
28 Correct 29 ms 1112 KB Output is correct
29 Correct 42 ms 16372 KB Output is correct
30 Correct 991 ms 6860 KB Output is correct
31 Correct 1158 ms 6636 KB Output is correct
32 Correct 740 ms 11704 KB Output is correct
33 Correct 1593 ms 6604 KB Output is correct
34 Correct 1579 ms 6604 KB Output is correct
35 Correct 485 ms 4436 KB Output is correct
36 Correct 1047 ms 6628 KB Output is correct
37 Correct 1130 ms 6644 KB Output is correct
38 Correct 1117 ms 6536 KB Output is correct
39 Correct 1097 ms 6532 KB Output is correct
40 Correct 1116 ms 6604 KB Output is correct
41 Correct 1098 ms 6580 KB Output is correct
42 Correct 1113 ms 6536 KB Output is correct
43 Correct 1098 ms 6536 KB Output is correct
44 Correct 1089 ms 6544 KB Output is correct