답안 #527529

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
527529 2022-02-17T14:46:44 Z eecs Sandcastle 2 (JOI22_ho_t5) C++17
100 / 100
469 ms 35920 KB
#include <bits/stdc++.h>
using namespace std;

const int maxn = 52000;
const int dx[] = {0, 0, -1, 1}, dy[] = {-1, 1, 0, 0};
int h, w, ans, a[maxn], sa[maxn], sb[maxn], cnt[maxn];
int num[9][9][maxn], tmp[9][6][maxn], res[6][6][maxn], pre[6][maxn];

int id(int x, int y) {
    return (x - 1) * w + y;
}

int main() {
    scanf("%d %d", &h, &w);
    int _h = h, _w = w;
    if (h < w) swap(h, w);
    for (int i = 1; i <= _h; i++) {
        for (int j = 1; j <= _w; j++) {
            scanf("%d", &a[_h < _w ? id(j, i) : id(i, j)]);
        }
    }
    for (int i = 1; i <= h; i++) for (int j = 1; j <= w; j++) {
        for (int lx : {i - 2, i - 1, i}) for (int rx : {i, i + 1, i + 2}) {
            for (int ly : {j - 2, j - 1, j}) for (int ry : {j, j + 1, j + 2}) {
                if (lx < 0 || rx > h || ly < 0 || ry > w) continue;
                int d = 0;
                for (int k = 0; k < 4; k++) {
                    int x = i + dx[k], y = j + dy[k];
                    if (x < lx || x > rx || y < ly || y > ry) continue;
                    int mx = -1, foo = 0, bar;
                    for (int _k = 0; _k < 4; _k++) {
                        int p = x + dx[_k], q = y + dy[_k];
                        if (p < lx || p > rx || q < ly || q > ry) continue;
                        if (a[id(p, q)] < a[id(x, y)] && a[id(p, q)] > mx) mx = a[id(p, q)], foo = p, bar = q;
                    }
                    if (foo == i && bar == j) { d = 1; break; }
                }
                if (!d) num[(i - lx) * 3 + rx - i][(j - ly) * 3 + ry - j][id(i, j)]++;
            }
        }
    }
    for (int i = 0; i < 9; i++) {
        for (int j = 1; j <= h * w; j++) {
            tmp[i][0][j] = num[i][0][j];
            tmp[i][1][j] = num[i][1][j] + num[i][3][j + 1];
            tmp[i][2][j] = num[i][2][j] + num[i][4][j + 1] + num[i][6][j + 2];
            tmp[i][3][j] = num[i][2][j] + num[i][5][j + 1];
            tmp[i][4][j] = num[i][7][j] + num[i][6][j + 1];
            tmp[i][5][j] = num[i][8][j];
        }
    }
    for (int i = 0; i < 6; i++) {
        for (int j = 1; j <= h * w; j++) {
            res[0][i][j] = tmp[0][i][j];
            res[1][i][j] = tmp[1][i][j] + tmp[3][i][j + w];
            res[2][i][j] = tmp[2][i][j] + tmp[4][i][j + w] + tmp[6][i][j + 2 * w];
            res[3][i][j] = tmp[2][i][j] + tmp[5][i][j + w];
            res[4][i][j] = tmp[7][i][j] + tmp[6][i][j + w];
            res[5][i][j] = tmp[8][i][j];
        }
    }
    for (int i = 0; i < 6; i++) {
        for (int j = 1; j <= h * w; j++) {
            pre[i][j] = pre[i][j - 1] + res[i][5][j];
        }
    }
    auto calc = [&](int k, int x, int ly, int ry) {
        if (ry <= ly + 2) return res[k][ry - ly][id(x, ly)];
        if (ry == ly + 3) return res[k][3][id(x, ly)] + res[k][4][id(x, ly + 2)];
        int t = res[k][3][id(x, ly)] + res[k][4][id(x, ry - 1)];
        return t + pre[k][id(x, ry - 2)] - pre[k][id(x, ly + 1)];
    };
    for (int ly = 1; ly <= w; ly++) {
        for (int ry = ly; ry <= w; ry++) {
            fill(sa, sa + h + 1, 0), fill(sb, sb + h + 1, 0);
            for (int i = 1, v8 = 0; i < h; i++) {
                if (i > 2) sa[i - 2] += v8, sb[i + 1] += v8;
                sa[i] -= calc(3, i, ly, ry);
                sb[i + 1] += calc(4, i, ly, ry);
                v8 += calc(5, i, ly, ry);
            }
            for (int i = 1; i <= h; i++) {
                if (i > 3) {
                    cnt[sa[i - 3] + 2 * w]++;
                    ans += cnt[sb[i] + 2 * w - 1];
                }
                for (int j = 0; j <= min(i - 1, 2); j++) {
                    if (calc(j, i - j, ly, ry) == 1) ans++;
                }
            }
            for (int i = 4; i <= h; i++) {
                cnt[sa[i - 3] + 2 * w]--;
            }
        }
    }
    printf("%d\n", ans);
    return 0;
}

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:14:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   14 |     scanf("%d %d", &h, &w);
      |     ~~~~~^~~~~~~~~~~~~~~~~
Main.cpp:19:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   19 |             scanf("%d", &a[_h < _w ? id(j, i) : id(i, j)]);
      |             ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:36:41: warning: 'bar' may be used uninitialized in this function [-Wmaybe-uninitialized]
   36 |                     if (foo == i && bar == j) { d = 1; break; }
      |                                     ~~~~^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 716 KB Output is correct
2 Correct 40 ms 20644 KB Output is correct
3 Correct 49 ms 23280 KB Output is correct
4 Correct 54 ms 20936 KB Output is correct
5 Correct 48 ms 22844 KB Output is correct
6 Correct 54 ms 23712 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 1100 KB Output is correct
2 Correct 1 ms 1100 KB Output is correct
3 Correct 1 ms 844 KB Output is correct
4 Correct 1 ms 972 KB Output is correct
5 Correct 1 ms 1100 KB Output is correct
6 Correct 1 ms 1100 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 1100 KB Output is correct
2 Correct 1 ms 1100 KB Output is correct
3 Correct 1 ms 844 KB Output is correct
4 Correct 1 ms 972 KB Output is correct
5 Correct 1 ms 1100 KB Output is correct
6 Correct 1 ms 1100 KB Output is correct
7 Correct 2 ms 1484 KB Output is correct
8 Correct 3 ms 1484 KB Output is correct
9 Correct 11 ms 1996 KB Output is correct
10 Correct 6 ms 1548 KB Output is correct
11 Correct 4 ms 1484 KB Output is correct
12 Correct 3 ms 1404 KB Output is correct
13 Correct 6 ms 1916 KB Output is correct
14 Correct 6 ms 1640 KB Output is correct
15 Correct 10 ms 2084 KB Output is correct
16 Correct 10 ms 2124 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 1100 KB Output is correct
2 Correct 1 ms 1100 KB Output is correct
3 Correct 1 ms 844 KB Output is correct
4 Correct 1 ms 972 KB Output is correct
5 Correct 1 ms 1100 KB Output is correct
6 Correct 1 ms 1100 KB Output is correct
7 Correct 2 ms 1484 KB Output is correct
8 Correct 3 ms 1484 KB Output is correct
9 Correct 11 ms 1996 KB Output is correct
10 Correct 6 ms 1548 KB Output is correct
11 Correct 4 ms 1484 KB Output is correct
12 Correct 3 ms 1404 KB Output is correct
13 Correct 6 ms 1916 KB Output is correct
14 Correct 6 ms 1640 KB Output is correct
15 Correct 10 ms 2084 KB Output is correct
16 Correct 10 ms 2124 KB Output is correct
17 Correct 7 ms 3532 KB Output is correct
18 Correct 53 ms 5908 KB Output is correct
19 Correct 45 ms 5844 KB Output is correct
20 Correct 30 ms 4080 KB Output is correct
21 Correct 29 ms 4016 KB Output is correct
22 Correct 26 ms 5152 KB Output is correct
23 Correct 38 ms 5508 KB Output is correct
24 Correct 35 ms 4824 KB Output is correct
25 Correct 42 ms 5856 KB Output is correct
26 Correct 67 ms 5816 KB Output is correct
27 Correct 50 ms 5908 KB Output is correct
28 Correct 46 ms 5872 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 1100 KB Output is correct
2 Correct 1 ms 1100 KB Output is correct
3 Correct 1 ms 844 KB Output is correct
4 Correct 1 ms 972 KB Output is correct
5 Correct 1 ms 1100 KB Output is correct
6 Correct 1 ms 1100 KB Output is correct
7 Correct 2 ms 1484 KB Output is correct
8 Correct 3 ms 1484 KB Output is correct
9 Correct 11 ms 1996 KB Output is correct
10 Correct 6 ms 1548 KB Output is correct
11 Correct 4 ms 1484 KB Output is correct
12 Correct 3 ms 1404 KB Output is correct
13 Correct 6 ms 1916 KB Output is correct
14 Correct 6 ms 1640 KB Output is correct
15 Correct 10 ms 2084 KB Output is correct
16 Correct 10 ms 2124 KB Output is correct
17 Correct 7 ms 3532 KB Output is correct
18 Correct 53 ms 5908 KB Output is correct
19 Correct 45 ms 5844 KB Output is correct
20 Correct 30 ms 4080 KB Output is correct
21 Correct 29 ms 4016 KB Output is correct
22 Correct 26 ms 5152 KB Output is correct
23 Correct 38 ms 5508 KB Output is correct
24 Correct 35 ms 4824 KB Output is correct
25 Correct 42 ms 5856 KB Output is correct
26 Correct 67 ms 5816 KB Output is correct
27 Correct 50 ms 5908 KB Output is correct
28 Correct 46 ms 5872 KB Output is correct
29 Correct 55 ms 22448 KB Output is correct
30 Correct 340 ms 35816 KB Output is correct
31 Correct 450 ms 35920 KB Output is correct
32 Correct 69 ms 22004 KB Output is correct
33 Correct 355 ms 22576 KB Output is correct
34 Correct 325 ms 23764 KB Output is correct
35 Correct 238 ms 21700 KB Output is correct
36 Correct 359 ms 32172 KB Output is correct
37 Correct 382 ms 35276 KB Output is correct
38 Correct 426 ms 35544 KB Output is correct
39 Correct 427 ms 35292 KB Output is correct
40 Correct 375 ms 35488 KB Output is correct
41 Correct 469 ms 35400 KB Output is correct
42 Correct 416 ms 35396 KB Output is correct
43 Correct 431 ms 35396 KB Output is correct
44 Correct 447 ms 35312 KB Output is correct