답안 #781351

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
781351 2023-07-13T04:02:48 Z green_gold_dog Maze (JOI23_ho_t3) C++17
8 / 100
2000 ms 35712 KB
#include<bits/stdc++.h>

using namespace std;

typedef int ll;

const ll INF = 1'000'000'000;

template<typename T>
bool assign_min(T& a, T b) {
        if (a > b) {
                a = b;
                return true;
        }
        return false;
}

template<typename T>
bool assign_max(T& a, T b) {
        if (a < b) {
                a = b;
                return true;
        }
        return false;
}

struct segment_tree {
        vector<bool> tree;
        ll size;
        segment_tree(ll n) {
                size = 1;
                while (size < n) {
                        size *= 2;
                }
                tree.resize(size * 2, false);
        }
        vector<ll> set(ll l, ll r) {
                vector<ll> ans;
                set(1, 0, size, l, r, ans);
                return ans;
        }
        void set(ll v, ll l, ll r, ll ql, ll qr, vector<ll>& ans) {
                if (tree[v]) {
                        return;
                }
                if (r <= ql || qr <= l) {
                        return;
                }
                if (r - l == 1) {
                        ans.push_back(l);
                        tree[v] = true;
                        return;
                }
                ll mid = (l + r) / 2;
                set(v * 2, l, mid, ql, qr, ans);
                set(v * 2 + 1, mid, r, ql, qr, ans);
                tree[v] = tree[v * 2] && tree[v * 2 + 1];
        }
};

struct segment_tree_am {
        vector<ll> m, tree;
        ll size;
        segment_tree_am(ll n) {
                size = 1;
                while (size < n) {
                        size *= 2;
                }
                m.resize(size * 2, 0);
                tree.resize(size * 2, 0);
        }
        bool get(ll l, ll r) {
                return get(1, 0, size, l, r);
        }
        bool get(ll v, ll l, ll r, ll ql, ll qr) {
                if (tree[v] != 0) {
                        return false;
                }
                if (ql <= l && r <= qr) {
                        return true;
                }
                if (qr <= l || r <= ql) {
                        return false;
                }
                ll mid = (l + r) / 2;
                return get(v * 2, l, mid, ql, qr) || get(v * 2 + 1, mid, r, ql, qr);
        }
        void add(ll l, ll r, ll x) {
                add(1, 0, size, l, r, x);
        }
        void add(ll v, ll l, ll r, ll ql, ll qr, ll x) {
                if (ql <= l && r <= qr) {
                        tree[v] += x;
                        m[v] += x;
                        return;
                }
                if (qr <= l || r <= ql) {
                        return;
                }
                ll mid = (l + r) / 2;
                add(v * 2, l, mid, ql, qr, x);
                add(v * 2 + 1, mid, r, ql, qr, x);
                tree[v] = min(tree[v * 2], tree[v * 2 + 1]) + m[v];
        }
        ll find_first(ll l, ll r) {
                if (!get(l, r)) {
                        return r;
                }
                return find_first(1, 0, size, l, r);
        }
        ll find_first(ll v, ll l, ll r, ll ql, ll qr) {
                if (r - l == 1) {
                        return l;
                }
                ll mid = (l + r) / 2;
                if (get(v * 2, l, mid, ql, qr)) {
                        return find_first(v * 2, l, mid, ql, qr);
                } else {
                        return find_first(v * 2 + 1, mid, r, ql, qr);
                }
        }
};

void solve() {
        ll n, r, c;
        cin >> r >> c >> n;
        ll sx, sy, gx, gy;
        cin >> sx >> sy >> gx >> gy;
        sx--;
        sy--;
        gx--;
        gy--;
        vector<vector<bool>> arr(r, vector<bool>(c, false));
        vector<vector<bool>> used(r, vector<bool>(c, false));
        vector<segment_tree> dist(r, segment_tree(c));
        for (ll i = 0; i < r; i++) {
                string s;
                cin >> s;
                for (ll j = 0; j < c; j++) {
                        if (s[j] == '.') {
                                arr[i][j] = true;
                        }
                }
        }
        vector<pair<ll, ll>> now;
        now.emplace_back(sx, sy);
        used[sx][sy] = true;
        ll na = 0;
        while (true) {
                vector<pair<ll, ll>> nu;
                vector<vector<ll>> all(r * 2);
                while (!now.empty()) {
                        auto[x, y] = now.back();
                        nu.emplace_back(x, y);
                        if (x == gx && y == gy) {
                                cout << na << '\n';
                                return;
                        }
                        now.pop_back();
                        if (x > 0 && arr[x - 1][y]) {
                                if (!used[x - 1][y]) {
                                        used[x - 1][y] = true;
                                        now.emplace_back(x - 1, y);
                                }
                        }
                        if (x + 1 < r && arr[x + 1][y]) {
                                if (!used[x + 1][y]) {
                                        used[x + 1][y] = true;
                                        now.emplace_back(x + 1, y);
                                }
                        }
                        if (y > 0 && arr[x][y - 1]) {
                                if (!used[x][y - 1]) {
                                        used[x][y - 1] = true;
                                        now.emplace_back(x, y - 1);
                                }
                        }
                        if (y + 1 < c && arr[x][y + 1]) {
                                if (!used[x][y + 1]) {
                                        used[x][y + 1] = true;
                                        now.emplace_back(x, y + 1);
                                }
                        }
                        all[max(x - n + 1, (ll)0) * 2].emplace_back(y);
                        all[(min(x + n, r) - 1) * 2 + 1].emplace_back(y);
                }
                na++;
                for (auto[x, y] : nu) {
                        if (x - n >= 0) {
                                ll i = x - n;
                                for (auto j : dist[i].set(max(y - n + 1, (ll)0), min(y + n, c))) {
                                        if (!used[i][j]) {
                                                used[i][j] = true;
                                                now.emplace_back(i, j);
                                        }
                                }
                        }
                        if (x + n < r) {
                                ll i = x + n;
                                for (auto j : dist[i].set(max(y - n + 1, (ll)0), min(y + n, c))) {
                                        if (!used[i][j]) {
                                                used[i][j] = true;
                                                now.emplace_back(i, j);
                                        }
                                }
                        }
                }
                multiset<ll> nps;
                set<ll> no;
                no.insert(-INF);
                for (ll i = 0; i < r; i++) {
                        for (auto y : all[i * 2]) {
                                ll l = y - n, r = y + n + 1;
                                auto it = nps.lower_bound(y);
                                ll prev = -INF, next = INF;
                                if (it != nps.end()) {
                                        next = *it;
                                }
                                if (it != nps.begin()) {
                                        it--;
                                        prev = *it;
                                }
                                nps.insert(y);
                                if (next - prev - 1 <= n * 2) {
                                        continue;
                                }
                                auto io = no.lower_bound(l);
                                auto nio = io;
                                if (y - prev - 1 <= n * 2) {
                                        nio++;
                                        no.erase(io);
                                } else {
                                        no.insert(l);
                                }
                                if (next - y - 1 <= n * 2) {
                                        no.erase(nio);
                                } else {
                                        no.insert(r);
                                }
                        }
                        ll lst = -INF;
                        for (auto j : no) {
                                if (lst == -INF) {
                                        lst = j;
                                        continue;
                                }
                                for (auto j : dist[i].set(max(lst, (ll)0), min(j, c))) {
                                        if (!used[i][j]) {
                                                used[i][j] = true;
                                                now.emplace_back(i, j);
                                        }
                                }
                                lst = -INF;
                        }
                        for (auto y : all[i * 2 + 1]) {
                                ll l = y - n, r = y + n + 1;
                                auto io = no.upper_bound(y);
                                auto nio = io;
                                nio--;
                                nps.erase(nps.find(y));
                                if (nps.empty()) {
                                        no.erase(io);
                                        no.erase(nio);
                                        continue;
                                }
                                ll lo = *nio, ro = *io;
                                auto it = nps.lower_bound(y);
                                if (it != nps.end() && *it == y) {
                                        continue;
                                }
                                if (it == nps.end()) {
                                        no.erase(ro);
                                        it--;
                                        if (lo < l) {
                                                no.insert(*it + n + 1);
                                        } else {
                                                no.erase(lo);
                                        }
                                        continue;
                                }
                                if (it == nps.begin()) {
                                        no.erase(lo);
                                        if (ro > r) {
                                                no.insert(*it - n);
                                        } else {
                                                no.erase(ro);
                                        }
                                        continue;
                                }
                                auto lit = it, rit = it;
                                lit--;
                                if (*rit - *lit - 1 <= n * 2) {
                                        continue;
                                }
                                if (lo < l) {
                                        no.insert(*lit + n + 1);
                                } else {
                                        no.erase(lo);
                                }
                                if (ro > r) {
                                        no.insert(*rit - n);
                                } else {
                                        no.erase(ro);
                                }
                        }
                }
        }
}

int main() {
        ios_base::sync_with_stdio(false);
        cin.tie(0);
        cout.tie(0);
        solve();
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 320 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 328 KB Output is correct
15 Correct 1 ms 320 KB Output is correct
16 Correct 1 ms 320 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 320 KB Output is correct
19 Correct 26 ms 468 KB Output is correct
20 Correct 17 ms 508 KB Output is correct
21 Correct 2 ms 1580 KB Output is correct
22 Correct 32 ms 532 KB Output is correct
23 Correct 28 ms 472 KB Output is correct
24 Correct 12 ms 460 KB Output is correct
25 Correct 19 ms 460 KB Output is correct
26 Correct 2 ms 468 KB Output is correct
27 Correct 2 ms 460 KB Output is correct
28 Correct 12 ms 536 KB Output is correct
29 Correct 84 ms 596 KB Output is correct
30 Correct 22 ms 468 KB Output is correct
31 Correct 3 ms 2104 KB Output is correct
32 Correct 89 ms 740 KB Output is correct
33 Correct 69 ms 652 KB Output is correct
34 Correct 36 ms 1004 KB Output is correct
35 Correct 59 ms 852 KB Output is correct
36 Correct 45 ms 1016 KB Output is correct
37 Correct 69 ms 876 KB Output is correct
38 Correct 5 ms 720 KB Output is correct
39 Correct 943 ms 1876 KB Output is correct
40 Correct 27 ms 468 KB Output is correct
41 Correct 21 ms 920 KB Output is correct
42 Correct 134 ms 668 KB Output is correct
43 Correct 91 ms 764 KB Output is correct
44 Correct 163 ms 1188 KB Output is correct
45 Correct 199 ms 1376 KB Output is correct
46 Correct 44 ms 35712 KB Output is correct
47 Correct 801 ms 2036 KB Output is correct
48 Correct 696 ms 1836 KB Output is correct
49 Correct 213 ms 4376 KB Output is correct
50 Correct 444 ms 4456 KB Output is correct
51 Correct 648 ms 3320 KB Output is correct
52 Correct 486 ms 2516 KB Output is correct
53 Correct 767 ms 2156 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 324 KB Output is correct
3 Correct 1 ms 320 KB Output is correct
4 Correct 1 ms 324 KB Output is correct
5 Correct 1 ms 316 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Execution timed out 2068 ms 212 KB Time limit exceeded
9 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 320 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Runtime error 1 ms 444 KB Execution killed with signal 6
15 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 324 KB Output is correct
3 Correct 1 ms 320 KB Output is correct
4 Correct 1 ms 324 KB Output is correct
5 Correct 1 ms 316 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Execution timed out 2068 ms 212 KB Time limit exceeded
9 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 324 KB Output is correct
3 Correct 1 ms 320 KB Output is correct
4 Correct 1 ms 324 KB Output is correct
5 Correct 1 ms 316 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Execution timed out 2068 ms 212 KB Time limit exceeded
9 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 320 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 328 KB Output is correct
15 Correct 1 ms 320 KB Output is correct
16 Correct 1 ms 320 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 320 KB Output is correct
19 Correct 26 ms 468 KB Output is correct
20 Correct 17 ms 508 KB Output is correct
21 Correct 2 ms 1580 KB Output is correct
22 Correct 32 ms 532 KB Output is correct
23 Correct 28 ms 472 KB Output is correct
24 Correct 12 ms 460 KB Output is correct
25 Correct 19 ms 460 KB Output is correct
26 Correct 2 ms 468 KB Output is correct
27 Correct 2 ms 460 KB Output is correct
28 Correct 12 ms 536 KB Output is correct
29 Correct 84 ms 596 KB Output is correct
30 Correct 22 ms 468 KB Output is correct
31 Correct 3 ms 2104 KB Output is correct
32 Correct 89 ms 740 KB Output is correct
33 Correct 69 ms 652 KB Output is correct
34 Correct 36 ms 1004 KB Output is correct
35 Correct 59 ms 852 KB Output is correct
36 Correct 45 ms 1016 KB Output is correct
37 Correct 69 ms 876 KB Output is correct
38 Correct 5 ms 720 KB Output is correct
39 Correct 943 ms 1876 KB Output is correct
40 Correct 27 ms 468 KB Output is correct
41 Correct 21 ms 920 KB Output is correct
42 Correct 134 ms 668 KB Output is correct
43 Correct 91 ms 764 KB Output is correct
44 Correct 163 ms 1188 KB Output is correct
45 Correct 199 ms 1376 KB Output is correct
46 Correct 44 ms 35712 KB Output is correct
47 Correct 801 ms 2036 KB Output is correct
48 Correct 696 ms 1836 KB Output is correct
49 Correct 213 ms 4376 KB Output is correct
50 Correct 444 ms 4456 KB Output is correct
51 Correct 648 ms 3320 KB Output is correct
52 Correct 486 ms 2516 KB Output is correct
53 Correct 767 ms 2156 KB Output is correct
54 Correct 1 ms 212 KB Output is correct
55 Correct 1 ms 324 KB Output is correct
56 Correct 1 ms 320 KB Output is correct
57 Correct 1 ms 324 KB Output is correct
58 Correct 1 ms 316 KB Output is correct
59 Correct 0 ms 212 KB Output is correct
60 Correct 1 ms 212 KB Output is correct
61 Execution timed out 2068 ms 212 KB Time limit exceeded
62 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 320 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 328 KB Output is correct
15 Correct 1 ms 320 KB Output is correct
16 Correct 1 ms 320 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 320 KB Output is correct
19 Correct 26 ms 468 KB Output is correct
20 Correct 17 ms 508 KB Output is correct
21 Correct 2 ms 1580 KB Output is correct
22 Correct 32 ms 532 KB Output is correct
23 Correct 28 ms 472 KB Output is correct
24 Correct 12 ms 460 KB Output is correct
25 Correct 19 ms 460 KB Output is correct
26 Correct 2 ms 468 KB Output is correct
27 Correct 2 ms 460 KB Output is correct
28 Correct 12 ms 536 KB Output is correct
29 Correct 84 ms 596 KB Output is correct
30 Correct 22 ms 468 KB Output is correct
31 Correct 3 ms 2104 KB Output is correct
32 Correct 89 ms 740 KB Output is correct
33 Correct 69 ms 652 KB Output is correct
34 Correct 36 ms 1004 KB Output is correct
35 Correct 59 ms 852 KB Output is correct
36 Correct 45 ms 1016 KB Output is correct
37 Correct 69 ms 876 KB Output is correct
38 Correct 5 ms 720 KB Output is correct
39 Correct 943 ms 1876 KB Output is correct
40 Correct 27 ms 468 KB Output is correct
41 Correct 21 ms 920 KB Output is correct
42 Correct 134 ms 668 KB Output is correct
43 Correct 91 ms 764 KB Output is correct
44 Correct 163 ms 1188 KB Output is correct
45 Correct 199 ms 1376 KB Output is correct
46 Correct 44 ms 35712 KB Output is correct
47 Correct 801 ms 2036 KB Output is correct
48 Correct 696 ms 1836 KB Output is correct
49 Correct 213 ms 4376 KB Output is correct
50 Correct 444 ms 4456 KB Output is correct
51 Correct 648 ms 3320 KB Output is correct
52 Correct 486 ms 2516 KB Output is correct
53 Correct 767 ms 2156 KB Output is correct
54 Correct 1 ms 212 KB Output is correct
55 Correct 1 ms 324 KB Output is correct
56 Correct 1 ms 320 KB Output is correct
57 Correct 1 ms 324 KB Output is correct
58 Correct 1 ms 316 KB Output is correct
59 Correct 0 ms 212 KB Output is correct
60 Correct 1 ms 212 KB Output is correct
61 Execution timed out 2068 ms 212 KB Time limit exceeded
62 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 320 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 328 KB Output is correct
15 Correct 1 ms 320 KB Output is correct
16 Correct 1 ms 320 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 320 KB Output is correct
19 Correct 26 ms 468 KB Output is correct
20 Correct 17 ms 508 KB Output is correct
21 Correct 2 ms 1580 KB Output is correct
22 Correct 32 ms 532 KB Output is correct
23 Correct 28 ms 472 KB Output is correct
24 Correct 12 ms 460 KB Output is correct
25 Correct 19 ms 460 KB Output is correct
26 Correct 2 ms 468 KB Output is correct
27 Correct 2 ms 460 KB Output is correct
28 Correct 12 ms 536 KB Output is correct
29 Correct 84 ms 596 KB Output is correct
30 Correct 22 ms 468 KB Output is correct
31 Correct 3 ms 2104 KB Output is correct
32 Correct 89 ms 740 KB Output is correct
33 Correct 69 ms 652 KB Output is correct
34 Correct 36 ms 1004 KB Output is correct
35 Correct 59 ms 852 KB Output is correct
36 Correct 45 ms 1016 KB Output is correct
37 Correct 69 ms 876 KB Output is correct
38 Correct 5 ms 720 KB Output is correct
39 Correct 943 ms 1876 KB Output is correct
40 Correct 27 ms 468 KB Output is correct
41 Correct 21 ms 920 KB Output is correct
42 Correct 134 ms 668 KB Output is correct
43 Correct 91 ms 764 KB Output is correct
44 Correct 163 ms 1188 KB Output is correct
45 Correct 199 ms 1376 KB Output is correct
46 Correct 44 ms 35712 KB Output is correct
47 Correct 801 ms 2036 KB Output is correct
48 Correct 696 ms 1836 KB Output is correct
49 Correct 213 ms 4376 KB Output is correct
50 Correct 444 ms 4456 KB Output is correct
51 Correct 648 ms 3320 KB Output is correct
52 Correct 486 ms 2516 KB Output is correct
53 Correct 767 ms 2156 KB Output is correct
54 Correct 1 ms 212 KB Output is correct
55 Correct 1 ms 324 KB Output is correct
56 Correct 1 ms 320 KB Output is correct
57 Correct 1 ms 324 KB Output is correct
58 Correct 1 ms 316 KB Output is correct
59 Correct 0 ms 212 KB Output is correct
60 Correct 1 ms 212 KB Output is correct
61 Execution timed out 2068 ms 212 KB Time limit exceeded
62 Halted 0 ms 0 KB -