답안 #632399

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
632399 2022-08-19T22:38:43 Z Ooops_sorry Sandcastle 2 (JOI22_ho_t5) C++14
100 / 100
2099 ms 16144 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 && summ <= INF; 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;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 42 ms 16088 KB Output is correct
3 Correct 57 ms 15828 KB Output is correct
4 Correct 43 ms 16028 KB Output is correct
5 Correct 41 ms 15996 KB Output is correct
6 Correct 46 ms 16144 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 3 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 4 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
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 3 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 4 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 6 ms 2528 KB Output is correct
18 Correct 10 ms 1264 KB Output is correct
19 Correct 6 ms 1236 KB Output is correct
20 Correct 48 ms 1236 KB Output is correct
21 Correct 44 ms 1236 KB Output is correct
22 Correct 38 ms 1236 KB Output is correct
23 Correct 35 ms 1216 KB Output is correct
24 Correct 11 ms 1152 KB Output is correct
25 Correct 15 ms 1236 KB Output is correct
26 Correct 13 ms 1256 KB Output is correct
27 Correct 11 ms 1224 KB Output is correct
28 Correct 11 ms 1236 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 3 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 4 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 6 ms 2528 KB Output is correct
18 Correct 10 ms 1264 KB Output is correct
19 Correct 6 ms 1236 KB Output is correct
20 Correct 48 ms 1236 KB Output is correct
21 Correct 44 ms 1236 KB Output is correct
22 Correct 38 ms 1236 KB Output is correct
23 Correct 35 ms 1216 KB Output is correct
24 Correct 11 ms 1152 KB Output is correct
25 Correct 15 ms 1236 KB Output is correct
26 Correct 13 ms 1256 KB Output is correct
27 Correct 11 ms 1224 KB Output is correct
28 Correct 11 ms 1236 KB Output is correct
29 Correct 38 ms 16092 KB Output is correct
30 Correct 78 ms 6724 KB Output is correct
31 Correct 199 ms 6612 KB Output is correct
32 Correct 1014 ms 11440 KB Output is correct
33 Correct 2099 ms 6588 KB Output is correct
34 Correct 1976 ms 6484 KB Output is correct
35 Correct 90 ms 4436 KB Output is correct
36 Correct 167 ms 6488 KB Output is correct
37 Correct 248 ms 6476 KB Output is correct
38 Correct 187 ms 6488 KB Output is correct
39 Correct 176 ms 6488 KB Output is correct
40 Correct 235 ms 6488 KB Output is correct
41 Correct 176 ms 6488 KB Output is correct
42 Correct 183 ms 6488 KB Output is correct
43 Correct 184 ms 6488 KB Output is correct
44 Correct 188 ms 6488 KB Output is correct