답안 #896638

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
896638 2024-01-01T19:58:46 Z arbuzick Maze (JOI23_ho_t3) C++17
0 / 100
1 ms 1372 KB
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx,avx2")
#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);
    }
}

vector<pair<int, int>> vals;

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

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});
    set<pair<int, int>> used;
    while (!dq.empty()) {
        auto [x, y] = dq.front();
        dq.pop_front();
        if (used.count({x, y})) {
            continue;
        }
        used.insert({x, y});
        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});
        }
        get_vals(x - n + 1, x + n, y - n, y + n + 1);
        get_vals(x - n, x + n + 1, y - n + 1, y + n);
        for (auto p : vals) {
            if (dp[p.first][p.second] != r * c) {
                continue;
            }
            del(p.second, p);
            swap(p.first, p.second);
            dp[p.first][p.second] = dp[x][y] + 1;
            dq.push_back(p);
        }
        vals.clear();
    }
    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 Incorrect 1 ms 860 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 1372 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 1368 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 1372 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 1372 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 860 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 860 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 860 KB Output isn't correct
2 Halted 0 ms 0 KB -