Submission #632398

# Submission time Handle Problem Language Result Execution time Memory
632398 2022-08-19T22:38:05 Z Ooops_sorry Sandcastle 2 (JOI22_ho_t5) C++14
100 / 100
2016 ms 16080 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);
        }
    }
}

void upd(vector<vector<int>> &a) {
    vector<int> u;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            u.pb(a[i][j]);
        }
    }
    sort(u.begin(), u.end());
    u.erase(unique(u.begin(), u.end()), u.end());
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            a[i][j] = lower_bound(u.begin(), u.end(), a[i][j]) - u.begin();
        }
    }
}

// 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];
            }
        }
    }
    upd(a);
    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 && cntt <= 1; 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 39 ms 16068 KB Output is correct
3 Correct 48 ms 15812 KB Output is correct
4 Correct 41 ms 16080 KB Output is correct
5 Correct 45 ms 16076 KB Output is correct
6 Correct 48 ms 16020 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 2 ms 724 KB Output is correct
8 Correct 2 ms 724 KB Output is correct
9 Correct 2 ms 468 KB Output is correct
10 Correct 4 ms 468 KB Output is correct
11 Correct 3 ms 596 KB Output is correct
12 Correct 2 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 2 ms 468 KB Output is correct
16 Correct 2 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 2 ms 724 KB Output is correct
8 Correct 2 ms 724 KB Output is correct
9 Correct 2 ms 468 KB Output is correct
10 Correct 4 ms 468 KB Output is correct
11 Correct 3 ms 596 KB Output is correct
12 Correct 2 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 2 ms 468 KB Output is correct
16 Correct 2 ms 468 KB Output is correct
17 Correct 5 ms 2644 KB Output is correct
18 Correct 11 ms 1268 KB Output is correct
19 Correct 7 ms 1236 KB Output is correct
20 Correct 77 ms 1236 KB Output is correct
21 Correct 45 ms 1236 KB Output is correct
22 Correct 39 ms 1236 KB Output is correct
23 Correct 36 ms 1108 KB Output is correct
24 Correct 11 ms 1108 KB Output is correct
25 Correct 12 ms 1260 KB Output is correct
26 Correct 11 ms 1248 KB Output is correct
27 Correct 11 ms 1236 KB Output is correct
28 Correct 11 ms 1236 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 2 ms 724 KB Output is correct
8 Correct 2 ms 724 KB Output is correct
9 Correct 2 ms 468 KB Output is correct
10 Correct 4 ms 468 KB Output is correct
11 Correct 3 ms 596 KB Output is correct
12 Correct 2 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 2 ms 468 KB Output is correct
16 Correct 2 ms 468 KB Output is correct
17 Correct 5 ms 2644 KB Output is correct
18 Correct 11 ms 1268 KB Output is correct
19 Correct 7 ms 1236 KB Output is correct
20 Correct 77 ms 1236 KB Output is correct
21 Correct 45 ms 1236 KB Output is correct
22 Correct 39 ms 1236 KB Output is correct
23 Correct 36 ms 1108 KB Output is correct
24 Correct 11 ms 1108 KB Output is correct
25 Correct 12 ms 1260 KB Output is correct
26 Correct 11 ms 1248 KB Output is correct
27 Correct 11 ms 1236 KB Output is correct
28 Correct 11 ms 1236 KB Output is correct
29 Correct 45 ms 16076 KB Output is correct
30 Correct 93 ms 6740 KB Output is correct
31 Correct 211 ms 6608 KB Output is correct
32 Correct 955 ms 11440 KB Output is correct
33 Correct 2000 ms 6484 KB Output is correct
34 Correct 2016 ms 6484 KB Output is correct
35 Correct 90 ms 4436 KB Output is correct
36 Correct 174 ms 6500 KB Output is correct
37 Correct 261 ms 6488 KB Output is correct
38 Correct 222 ms 6488 KB Output is correct
39 Correct 175 ms 6488 KB Output is correct
40 Correct 205 ms 6488 KB Output is correct
41 Correct 201 ms 6564 KB Output is correct
42 Correct 175 ms 6488 KB Output is correct
43 Correct 173 ms 6488 KB Output is correct
44 Correct 194 ms 6556 KB Output is correct