답안 #854377

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
854377 2023-09-27T07:08:43 Z arbuzick Maze (JOI23_ho_t3) C++17
43 / 100
2000 ms 638248 KB
#pragma GCC optimize("O3")
#include <bits/stdc++.h>

using namespace std;

constexpr int R = 1 << 12;

set<pair<int, int>> tree[R * 2];

void add(int pos, pair<int, int> val) {
    for (pos += R; pos > 0; pos /= 2) {
        tree[pos].insert(val);
    }
}

void del(int pos, pair<int, int> val) {
    for (pos += R; pos > 0; pos /= 2) {
        tree[pos].erase(val);
    }
}

pair<int, int> get_val(int l, int r, int x, int y) {
    l = max(l, 0);
    r = min(r, R);
    l += R;
    r += R;
    pair<int, int> ans = {-1, -1};
    while (l < r) {
        if (l & 1) {
            auto it = tree[l].lower_bound({x, 0});
            if (it != tree[l].end() && x <= it->first && it->first < y) {
                return *it;
            }
            l++;
        }
        if (r & 1) {
            --r;
            auto it = tree[r].lower_bound({x, 0});
            if (it != tree[r].end() && x <= it->first && it->first < y) {
                return *it;
            }
        }
        l >>= 1;
        r >>= 1;
    }
    return ans;
}

void solve() {
    int r, c, n;
    cin >> r >> c >> n;
    int sx, sy;
    cin >> sx >> sy;
    sx--, sy--;
    int gx, gy;
    cin >> gx >> gy;
    gx--, gy--;
    vector<string> a(r);
    for (int i = 0; i < r; ++i) {
        cin >> a[i];
    }
    for (int i = 0; i < r; ++i) {
        for (int j = 0; j < c; ++j) {
            add(i, {j, i});
        }
    }
    vector<vector<int>> dp(r, vector<int>(c, r * c));
    deque<pair<int, int>> dq;
    dp[sx][sy] = 0;
    del(sx, {sy, sx});
    dq.push_back({sx, sy});
    while (!dq.empty()) {
        auto [x, y] = dq.front();
        dq.pop_front();
        if (x > 0 && a[x - 1][y] == '.' && dp[x - 1][y] > dp[x][y]) {
            if (dp[x - 1][y] == r * c) {
                del(x - 1, {y, x - 1});
            }
            dp[x - 1][y] = dp[x][y];
            dq.push_front({x - 1, y});
        }
        if (x + 1 < r && a[x + 1][y] == '.' && dp[x + 1][y] > dp[x][y]) {
            if (dp[x + 1][y] == r * c) {
                del(x + 1, {y, x + 1});
            }
            dp[x + 1][y] = dp[x][y];
            dq.push_front({x + 1, y});
        }
        if (y > 0 && a[x][y - 1] == '.' && dp[x][y - 1] > dp[x][y]) {
            if (dp[x][y - 1] == r * c) {
                del(x, {y - 1, x});
            }
            dp[x][y - 1] = dp[x][y];
            dq.push_front({x, y - 1});
        }
        if (y + 1 < c && a[x][y + 1] == '.' && dp[x][y + 1] > dp[x][y]) {
            if (dp[x][y + 1] == r * c) {
                del(x, {y + 1, x});
            }
            dp[x][y + 1] = dp[x][y];
            dq.push_front({x, y + 1});
        }
        while (true) {
            auto p = get_val(x - n + 1, x + n, y - n, y + n + 1);
            if (p.first == -1) {
                p = get_val(x - n, x + n + 1, y - n + 1, y + n);
                if (p.first == -1) {
                    break;
                }
            }
            del(p.second, p);
            swap(p.first, p.second);
            dp[p.first][p.second] = dp[x][y] + 1;
            dq.push_back(p);
        }
    }
    cout << dp[gx][gy] << '\n';
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int t = 1;
    // cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 860 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 2 ms 1372 KB Output is correct
4 Correct 2 ms 1372 KB Output is correct
5 Correct 2 ms 1372 KB Output is correct
6 Correct 3 ms 1380 KB Output is correct
7 Correct 2 ms 1372 KB Output is correct
8 Correct 3 ms 1372 KB Output is correct
9 Correct 0 ms 844 KB Output is correct
10 Correct 1 ms 860 KB Output is correct
11 Correct 1 ms 860 KB Output is correct
12 Correct 0 ms 840 KB Output is correct
13 Correct 1 ms 860 KB Output is correct
14 Correct 1 ms 1104 KB Output is correct
15 Correct 1 ms 860 KB Output is correct
16 Correct 2 ms 1372 KB Output is correct
17 Correct 2 ms 1372 KB Output is correct
18 Correct 2 ms 1372 KB Output is correct
19 Correct 177 ms 37624 KB Output is correct
20 Correct 104 ms 29292 KB Output is correct
21 Correct 154 ms 37724 KB Output is correct
22 Correct 187 ms 37668 KB Output is correct
23 Correct 196 ms 37700 KB Output is correct
24 Correct 145 ms 37976 KB Output is correct
25 Correct 140 ms 37972 KB Output is correct
26 Correct 206 ms 37712 KB Output is correct
27 Correct 182 ms 37644 KB Output is correct
28 Correct 171 ms 37704 KB Output is correct
29 Correct 456 ms 93008 KB Output is correct
30 Correct 144 ms 38556 KB Output is correct
31 Correct 431 ms 93268 KB Output is correct
32 Correct 512 ms 93012 KB Output is correct
33 Correct 510 ms 93192 KB Output is correct
34 Correct 407 ms 93892 KB Output is correct
35 Correct 402 ms 93664 KB Output is correct
36 Correct 497 ms 93528 KB Output is correct
37 Correct 541 ms 93200 KB Output is correct
38 Correct 509 ms 93196 KB Output is correct
39 Execution timed out 2070 ms 638248 KB Time limit exceeded
40 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 604 KB Output is correct
2 Correct 0 ms 860 KB Output is correct
3 Correct 1 ms 860 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
5 Correct 2 ms 1372 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 1 ms 860 KB Output is correct
8 Correct 1 ms 860 KB Output is correct
9 Correct 2 ms 1372 KB Output is correct
10 Correct 3 ms 1372 KB Output is correct
11 Correct 2 ms 1368 KB Output is correct
12 Correct 2 ms 1372 KB Output is correct
13 Correct 2 ms 1372 KB Output is correct
14 Correct 2 ms 1344 KB Output is correct
15 Correct 2 ms 1372 KB Output is correct
16 Correct 2 ms 1372 KB Output is correct
17 Correct 3 ms 1372 KB Output is correct
18 Correct 1 ms 604 KB Output is correct
19 Correct 2 ms 1116 KB Output is correct
20 Correct 1 ms 856 KB Output is correct
21 Correct 1 ms 856 KB Output is correct
22 Correct 2 ms 1116 KB Output is correct
23 Correct 1 ms 1112 KB Output is correct
24 Correct 1 ms 860 KB Output is correct
25 Correct 1 ms 604 KB Output is correct
26 Correct 1 ms 860 KB Output is correct
27 Correct 1 ms 856 KB Output is correct
28 Correct 2 ms 1100 KB Output is correct
29 Correct 1 ms 860 KB Output is correct
30 Correct 2 ms 1372 KB Output is correct
31 Correct 2 ms 1372 KB Output is correct
32 Correct 2 ms 1372 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 600 KB Output is correct
2 Correct 1 ms 860 KB Output is correct
3 Correct 1 ms 860 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
5 Correct 1 ms 860 KB Output is correct
6 Correct 1 ms 844 KB Output is correct
7 Correct 2 ms 1372 KB Output is correct
8 Correct 4 ms 1376 KB Output is correct
9 Correct 2 ms 1376 KB Output is correct
10 Correct 2 ms 1372 KB Output is correct
11 Correct 2 ms 1372 KB Output is correct
12 Correct 2 ms 1372 KB Output is correct
13 Correct 1 ms 604 KB Output is correct
14 Correct 2 ms 1116 KB Output is correct
15 Correct 1 ms 860 KB Output is correct
16 Correct 2 ms 1116 KB Output is correct
17 Correct 1 ms 1064 KB Output is correct
18 Correct 1 ms 860 KB Output is correct
19 Correct 0 ms 604 KB Output is correct
20 Correct 1 ms 860 KB Output is correct
21 Correct 2 ms 1100 KB Output is correct
22 Correct 2 ms 1368 KB Output is correct
23 Correct 2 ms 1368 KB Output is correct
24 Correct 5 ms 2140 KB Output is correct
25 Correct 118 ms 24400 KB Output is correct
26 Correct 140 ms 33860 KB Output is correct
27 Correct 160 ms 37716 KB Output is correct
28 Correct 146 ms 37716 KB Output is correct
29 Correct 146 ms 37712 KB Output is correct
30 Correct 159 ms 37536 KB Output is correct
31 Correct 144 ms 37848 KB Output is correct
32 Correct 189 ms 37904 KB Output is correct
33 Correct 189 ms 37968 KB Output is correct
34 Correct 352 ms 75092 KB Output is correct
35 Correct 424 ms 93192 KB Output is correct
36 Correct 398 ms 93264 KB Output is correct
37 Correct 397 ms 93208 KB Output is correct
38 Correct 394 ms 93260 KB Output is correct
39 Correct 1443 ms 268740 KB Output is correct
40 Execution timed out 2055 ms 553116 KB Time limit exceeded
41 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 604 KB Output is correct
2 Correct 0 ms 860 KB Output is correct
3 Correct 1 ms 860 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
5 Correct 2 ms 1372 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 1 ms 860 KB Output is correct
8 Correct 1 ms 860 KB Output is correct
9 Correct 2 ms 1372 KB Output is correct
10 Correct 3 ms 1372 KB Output is correct
11 Correct 2 ms 1368 KB Output is correct
12 Correct 2 ms 1372 KB Output is correct
13 Correct 2 ms 1372 KB Output is correct
14 Correct 2 ms 1344 KB Output is correct
15 Correct 2 ms 1372 KB Output is correct
16 Correct 2 ms 1372 KB Output is correct
17 Correct 3 ms 1372 KB Output is correct
18 Correct 1 ms 604 KB Output is correct
19 Correct 2 ms 1116 KB Output is correct
20 Correct 1 ms 856 KB Output is correct
21 Correct 1 ms 856 KB Output is correct
22 Correct 2 ms 1116 KB Output is correct
23 Correct 1 ms 1112 KB Output is correct
24 Correct 1 ms 860 KB Output is correct
25 Correct 1 ms 604 KB Output is correct
26 Correct 1 ms 860 KB Output is correct
27 Correct 1 ms 856 KB Output is correct
28 Correct 2 ms 1100 KB Output is correct
29 Correct 1 ms 860 KB Output is correct
30 Correct 2 ms 1372 KB Output is correct
31 Correct 2 ms 1372 KB Output is correct
32 Correct 2 ms 1372 KB Output is correct
33 Correct 178 ms 37456 KB Output is correct
34 Correct 5 ms 2140 KB Output is correct
35 Correct 8 ms 3412 KB Output is correct
36 Correct 100 ms 24224 KB Output is correct
37 Correct 106 ms 29268 KB Output is correct
38 Correct 141 ms 33988 KB Output is correct
39 Correct 151 ms 37520 KB Output is correct
40 Correct 171 ms 37664 KB Output is correct
41 Correct 198 ms 37712 KB Output is correct
42 Correct 144 ms 37664 KB Output is correct
43 Correct 143 ms 37664 KB Output is correct
44 Correct 139 ms 37712 KB Output is correct
45 Correct 143 ms 37972 KB Output is correct
46 Correct 141 ms 38020 KB Output is correct
47 Correct 135 ms 37932 KB Output is correct
48 Correct 165 ms 38036 KB Output is correct
49 Correct 151 ms 37712 KB Output is correct
50 Correct 143 ms 37784 KB Output is correct
51 Correct 149 ms 37972 KB Output is correct
52 Correct 188 ms 37724 KB Output is correct
53 Correct 177 ms 37732 KB Output is correct
54 Correct 169 ms 37460 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 604 KB Output is correct
2 Correct 0 ms 860 KB Output is correct
3 Correct 1 ms 860 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
5 Correct 2 ms 1372 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 1 ms 860 KB Output is correct
8 Correct 1 ms 860 KB Output is correct
9 Correct 2 ms 1372 KB Output is correct
10 Correct 3 ms 1372 KB Output is correct
11 Correct 2 ms 1368 KB Output is correct
12 Correct 2 ms 1372 KB Output is correct
13 Correct 2 ms 1372 KB Output is correct
14 Correct 2 ms 1344 KB Output is correct
15 Correct 2 ms 1372 KB Output is correct
16 Correct 2 ms 1372 KB Output is correct
17 Correct 3 ms 1372 KB Output is correct
18 Correct 1 ms 604 KB Output is correct
19 Correct 2 ms 1116 KB Output is correct
20 Correct 1 ms 856 KB Output is correct
21 Correct 1 ms 856 KB Output is correct
22 Correct 2 ms 1116 KB Output is correct
23 Correct 1 ms 1112 KB Output is correct
24 Correct 1 ms 860 KB Output is correct
25 Correct 1 ms 604 KB Output is correct
26 Correct 1 ms 860 KB Output is correct
27 Correct 1 ms 856 KB Output is correct
28 Correct 2 ms 1100 KB Output is correct
29 Correct 1 ms 860 KB Output is correct
30 Correct 2 ms 1372 KB Output is correct
31 Correct 2 ms 1372 KB Output is correct
32 Correct 2 ms 1372 KB Output is correct
33 Correct 178 ms 37456 KB Output is correct
34 Correct 5 ms 2140 KB Output is correct
35 Correct 8 ms 3412 KB Output is correct
36 Correct 100 ms 24224 KB Output is correct
37 Correct 106 ms 29268 KB Output is correct
38 Correct 141 ms 33988 KB Output is correct
39 Correct 151 ms 37520 KB Output is correct
40 Correct 171 ms 37664 KB Output is correct
41 Correct 198 ms 37712 KB Output is correct
42 Correct 144 ms 37664 KB Output is correct
43 Correct 143 ms 37664 KB Output is correct
44 Correct 139 ms 37712 KB Output is correct
45 Correct 143 ms 37972 KB Output is correct
46 Correct 141 ms 38020 KB Output is correct
47 Correct 135 ms 37932 KB Output is correct
48 Correct 165 ms 38036 KB Output is correct
49 Correct 151 ms 37712 KB Output is correct
50 Correct 143 ms 37784 KB Output is correct
51 Correct 149 ms 37972 KB Output is correct
52 Correct 188 ms 37724 KB Output is correct
53 Correct 177 ms 37732 KB Output is correct
54 Correct 169 ms 37460 KB Output is correct
55 Correct 466 ms 93008 KB Output is correct
56 Correct 142 ms 38664 KB Output is correct
57 Correct 346 ms 74968 KB Output is correct
58 Correct 385 ms 85076 KB Output is correct
59 Correct 443 ms 93204 KB Output is correct
60 Correct 482 ms 93264 KB Output is correct
61 Correct 535 ms 93084 KB Output is correct
62 Correct 400 ms 93288 KB Output is correct
63 Correct 405 ms 93068 KB Output is correct
64 Correct 394 ms 93392 KB Output is correct
65 Correct 399 ms 93668 KB Output is correct
66 Correct 408 ms 93884 KB Output is correct
67 Correct 389 ms 93524 KB Output is correct
68 Correct 471 ms 93264 KB Output is correct
69 Correct 445 ms 93268 KB Output is correct
70 Correct 495 ms 93320 KB Output is correct
71 Correct 435 ms 93272 KB Output is correct
72 Correct 505 ms 93028 KB Output is correct
73 Correct 528 ms 93200 KB Output is correct
74 Correct 530 ms 93260 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 860 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 2 ms 1372 KB Output is correct
4 Correct 2 ms 1372 KB Output is correct
5 Correct 2 ms 1372 KB Output is correct
6 Correct 3 ms 1380 KB Output is correct
7 Correct 2 ms 1372 KB Output is correct
8 Correct 3 ms 1372 KB Output is correct
9 Correct 0 ms 844 KB Output is correct
10 Correct 1 ms 860 KB Output is correct
11 Correct 1 ms 860 KB Output is correct
12 Correct 0 ms 840 KB Output is correct
13 Correct 1 ms 860 KB Output is correct
14 Correct 1 ms 1104 KB Output is correct
15 Correct 1 ms 860 KB Output is correct
16 Correct 2 ms 1372 KB Output is correct
17 Correct 2 ms 1372 KB Output is correct
18 Correct 2 ms 1372 KB Output is correct
19 Correct 177 ms 37624 KB Output is correct
20 Correct 104 ms 29292 KB Output is correct
21 Correct 154 ms 37724 KB Output is correct
22 Correct 187 ms 37668 KB Output is correct
23 Correct 196 ms 37700 KB Output is correct
24 Correct 145 ms 37976 KB Output is correct
25 Correct 140 ms 37972 KB Output is correct
26 Correct 206 ms 37712 KB Output is correct
27 Correct 182 ms 37644 KB Output is correct
28 Correct 171 ms 37704 KB Output is correct
29 Correct 456 ms 93008 KB Output is correct
30 Correct 144 ms 38556 KB Output is correct
31 Correct 431 ms 93268 KB Output is correct
32 Correct 512 ms 93012 KB Output is correct
33 Correct 510 ms 93192 KB Output is correct
34 Correct 407 ms 93892 KB Output is correct
35 Correct 402 ms 93664 KB Output is correct
36 Correct 497 ms 93528 KB Output is correct
37 Correct 541 ms 93200 KB Output is correct
38 Correct 509 ms 93196 KB Output is correct
39 Execution timed out 2070 ms 638248 KB Time limit exceeded
40 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 860 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 2 ms 1372 KB Output is correct
4 Correct 2 ms 1372 KB Output is correct
5 Correct 2 ms 1372 KB Output is correct
6 Correct 3 ms 1380 KB Output is correct
7 Correct 2 ms 1372 KB Output is correct
8 Correct 3 ms 1372 KB Output is correct
9 Correct 0 ms 844 KB Output is correct
10 Correct 1 ms 860 KB Output is correct
11 Correct 1 ms 860 KB Output is correct
12 Correct 0 ms 840 KB Output is correct
13 Correct 1 ms 860 KB Output is correct
14 Correct 1 ms 1104 KB Output is correct
15 Correct 1 ms 860 KB Output is correct
16 Correct 2 ms 1372 KB Output is correct
17 Correct 2 ms 1372 KB Output is correct
18 Correct 2 ms 1372 KB Output is correct
19 Correct 177 ms 37624 KB Output is correct
20 Correct 104 ms 29292 KB Output is correct
21 Correct 154 ms 37724 KB Output is correct
22 Correct 187 ms 37668 KB Output is correct
23 Correct 196 ms 37700 KB Output is correct
24 Correct 145 ms 37976 KB Output is correct
25 Correct 140 ms 37972 KB Output is correct
26 Correct 206 ms 37712 KB Output is correct
27 Correct 182 ms 37644 KB Output is correct
28 Correct 171 ms 37704 KB Output is correct
29 Correct 456 ms 93008 KB Output is correct
30 Correct 144 ms 38556 KB Output is correct
31 Correct 431 ms 93268 KB Output is correct
32 Correct 512 ms 93012 KB Output is correct
33 Correct 510 ms 93192 KB Output is correct
34 Correct 407 ms 93892 KB Output is correct
35 Correct 402 ms 93664 KB Output is correct
36 Correct 497 ms 93528 KB Output is correct
37 Correct 541 ms 93200 KB Output is correct
38 Correct 509 ms 93196 KB Output is correct
39 Execution timed out 2070 ms 638248 KB Time limit exceeded
40 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 860 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 2 ms 1372 KB Output is correct
4 Correct 2 ms 1372 KB Output is correct
5 Correct 2 ms 1372 KB Output is correct
6 Correct 3 ms 1380 KB Output is correct
7 Correct 2 ms 1372 KB Output is correct
8 Correct 3 ms 1372 KB Output is correct
9 Correct 0 ms 844 KB Output is correct
10 Correct 1 ms 860 KB Output is correct
11 Correct 1 ms 860 KB Output is correct
12 Correct 0 ms 840 KB Output is correct
13 Correct 1 ms 860 KB Output is correct
14 Correct 1 ms 1104 KB Output is correct
15 Correct 1 ms 860 KB Output is correct
16 Correct 2 ms 1372 KB Output is correct
17 Correct 2 ms 1372 KB Output is correct
18 Correct 2 ms 1372 KB Output is correct
19 Correct 177 ms 37624 KB Output is correct
20 Correct 104 ms 29292 KB Output is correct
21 Correct 154 ms 37724 KB Output is correct
22 Correct 187 ms 37668 KB Output is correct
23 Correct 196 ms 37700 KB Output is correct
24 Correct 145 ms 37976 KB Output is correct
25 Correct 140 ms 37972 KB Output is correct
26 Correct 206 ms 37712 KB Output is correct
27 Correct 182 ms 37644 KB Output is correct
28 Correct 171 ms 37704 KB Output is correct
29 Correct 456 ms 93008 KB Output is correct
30 Correct 144 ms 38556 KB Output is correct
31 Correct 431 ms 93268 KB Output is correct
32 Correct 512 ms 93012 KB Output is correct
33 Correct 510 ms 93192 KB Output is correct
34 Correct 407 ms 93892 KB Output is correct
35 Correct 402 ms 93664 KB Output is correct
36 Correct 497 ms 93528 KB Output is correct
37 Correct 541 ms 93200 KB Output is correct
38 Correct 509 ms 93196 KB Output is correct
39 Execution timed out 2070 ms 638248 KB Time limit exceeded
40 Halted 0 ms 0 KB -