結果

問題 No.2096 Rage With Our Friends
ユーザー k1suxuk1suxu
提出日時 2022-10-07 22:47:23
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 12,304 bytes
コンパイル時間 5,531 ms
コンパイル使用メモリ 310,236 KB
実行使用メモリ 813,568 KB
最終ジャッジ日時 2024-06-12 20:07:00
合計ジャッジ時間 7,731 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,812 KB
testcase_02 AC 2 ms
6,812 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 WA -
testcase_05 AC 1 ms
6,940 KB
testcase_06 AC 1 ms
6,940 KB
testcase_07 WA -
testcase_08 AC 2 ms
6,940 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 MLE -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>
using namespace std;

#define rep(i,n) for(int i = 0; i < (int)n; i++)
#define FOR(n) for(int i = 0; i < (int)n; i++)
#define repi(i,a,b) for(int i = (int)a; i < (int)b; i++)
#define pb push_back
#define all(x) x.begin(),x.end()
//#define mp make_pair
#define vi vector<int>
#define vvi vector<vi>
#define vll vector<ll>
#define vvll vector<vll>
#define vs vector<string>
#define vvs vector<vs>
#define vc vector<char>
#define vvc vector<vc>
#define pii pair<int,int>
#define pllll pair<ll,ll>
#define vpii vector<pair<int,int>>
#define vpllll vector<pair<ll,ll>>
#define vpis vector<pair<int,string>>
#define vplls vector<pair<ll, string>>
#define vpsi vector<pair<string, int>>
#define vpsll vector<pair<string, ll>>

template<typename T>
void chmax(T &a, const T &b) {a = (a > b? a : b);}
template<typename T>
void chmin(T &a, const T &b) {a = (a < b? a : b);}

using ll = long long;
using ld = long double;
using ull = unsigned long long;

const ll INF = numeric_limits<long long>::max() / 2;
const ld pi = 3.1415926535897932384626433832795028;
const ll mod = 998244353;
int dx[] = {-1, 0, 1, 0, -1, -1, 1, 1};
int dy[] = {0, -1, 0, 1, -1, 1, -1, 1};

#define int long long

struct SegmentTree {
//functions and values
    int n, n0;
    vector<int> max_v, smax_v, max_c;
    vector<int> min_v, smin_v, min_c;
    vector<int> sum;
    vector<int> len, ladd, lval;

    SegmentTree(int n, bool init = false, vector<int> a = {}) : n(n) {
        n0 = 1;
        while(n0 < n) n0 <<= 1;

        max_v.resize(4 * n0);
        smax_v.resize(4 * n0);
        max_c.resize(4 * n0);
        min_v.resize(4 * n0);
        smin_v.resize(4 * n0);
        min_c.resize(4 * n0);
        sum.resize(4 * n0);
        len.resize(4 * n0);
        ladd.resize(4 * n0);
        lval.resize(4 * n0);

        for(int i=0; i<2*n0; ++i) ladd[i] = 0, lval[i] = INF;
        len[0] = n0;
        for(int i=0; i<n0-1; ++i) len[2*i+1] = len[2*i+2] = (len[i] >> 1);

        for(int i=0; i<n; ++i) {
            max_v[n0-1+i] = min_v[n0-1+i] = sum[n0-1+i] = (init ? a[i] : 0);
            smax_v[n0-1+i] = -INF;
            smin_v[n0-1+i] = INF;
            max_c[n0-1+i] = min_c[n0-1+i] = 1;
        }

        for(int i=n; i<n0; ++i) {
            max_v[n0-1+i] = smax_v[n0-1+i] = -INF;
            min_v[n0-1+i] = smin_v[n0-1+i] = INF;
            max_c[n0-1+i] = min_c[n0-1+i] = 0;
        }
        for(int i=n0-2; i>=0; i--) {
            update(i);
        }
    }

    void update_node_max(int k, ll x) {
        sum[k] += (x - max_v[k]) * max_c[k];

        if(max_v[k] == min_v[k]) {
            max_v[k] = min_v[k] = x;
        } else if(max_v[k] == smin_v[k]) {
            max_v[k] = smin_v[k] = x;
        } else {
            max_v[k] = x;
        }

        if(lval[k] != INF && x < lval[k]) {
            lval[k] = x;
        }
    }
    void update_node_min(int k, ll x) {
        sum[k] += (x - min_v[k]) * min_c[k];

        if(max_v[k] == min_v[k]) {
            max_v[k] = min_v[k] = x;
        } else if(smax_v[k] == min_v[k]) {
            min_v[k] = smax_v[k] = x;
        } else {
            min_v[k] = x;
        }

        if(lval[k] != INF && lval[k] < x) {
            lval[k] = x;
        }
    }

    void push(int k) {

        if(n0-1 <= k) return;

        if(lval[k] != INF) {
            updateall(2*k+1, lval[k]);
            updateall(2*k+2, lval[k]);
            lval[k] = INF;
            return;
        }

        if(ladd[k] != 0) {
            addall(2*k+1, ladd[k]);
            addall(2*k+2, ladd[k]);
            ladd[k] = 0;
        }

        if(max_v[k] < max_v[2*k+1]) {
            update_node_max(2*k+1, max_v[k]);
        }
        if(min_v[2*k+1] < min_v[k]) {
            update_node_min(2*k+1, min_v[k]);
        }

        if(max_v[k] < max_v[2*k+2]) {
            update_node_max(2*k+2, max_v[k]);
        }
        if(min_v[2*k+2] < min_v[k]) {
            update_node_min(2*k+2, min_v[k]);
        }
    }

    void update(int k) {
        sum[k] = sum[2*k+1] + sum[2*k+2];

        if(max_v[2*k+1] < max_v[2*k+2]) {
            max_v[k] = max_v[2*k+2];
            max_c[k] = max_c[2*k+2];
            smax_v[k] = max(max_v[2*k+1], smax_v[2*k+2]);
        } else if(max_v[2*k+1] > max_v[2*k+2]) {
            max_v[k] = max_v[2*k+1];
            max_c[k] = max_c[2*k+1];
            smax_v[k] = max(smax_v[2*k+1], max_v[2*k+2]);
        } else {
            max_v[k] = max_v[2*k+1];
            max_c[k] = max_c[2*k+1] + max_c[2*k+2];
            smax_v[k] = max(smax_v[2*k+1], smax_v[2*k+2]);
        }

        if(min_v[2*k+1] < min_v[2*k+2]) {
            min_v[k] = min_v[2*k+1];
            min_c[k] = min_c[2*k+1];
            smin_v[k] = min(smin_v[2*k+1], min_v[2*k+2]);
        } else if(min_v[2*k+1] > min_v[2*k+2]) {
            min_v[k] = min_v[2*k+2];
            min_c[k] = min_c[2*k+2];
            smin_v[k] = min(min_v[2*k+1], smin_v[2*k+2]);
        } else {
            min_v[k] = min_v[2*k+1];
            min_c[k] = min_c[2*k+1] + min_c[2*k+2];
            smin_v[k] = min(smin_v[2*k+1], smin_v[2*k+2]);
        }
    }

    void _update_min(ll x, int a, int b, int k, int l, int r) {
        if(b <= l || r <= a || max_v[k] <= x) {
            return;
        }
        if(a <= l && r <= b && smax_v[k] < x) {
            update_node_max(k, x);
            return;
        }

        push(k);
        _update_min(x, a, b, 2*k+1, l, (l+r)/2);
        _update_min(x, a, b, 2*k+2, (l+r)/2, r);
        update(k);
    }

    void _update_max(ll x, int a, int b, int k, int l, int r) {
        if(b <= l || r <= a || x <= min_v[k]) {
            return;
        }
        if(a <= l && r <= b && x < smin_v[k]) {
            update_node_min(k, x);
            return;
        }

        push(k);
        _update_max(x, a, b, 2*k+1, l, (l+r)/2);
        _update_max(x, a, b, 2*k+2, (l+r)/2, r);
        update(k);
    }

    void addall(int k, ll x) {
        max_v[k] += x;
        if(smax_v[k] != -INF) smax_v[k] += x;
        min_v[k] += x;
        if(smin_v[k] != INF) smin_v[k] += x;

        sum[k] += len[k] * x;
        if(lval[k] != INF) {
            lval[k] += x;
        } else {
            ladd[k] += x;
        }
    }

    void updateall(int k, ll x) {
        max_v[k] = x; smax_v[k] = -INF;
        min_v[k] = x; smin_v[k] = INF;
        max_c[k] = min_c[k] = len[k];

        sum[k] = x * len[k];
        lval[k] = x; ladd[k] = 0;
    }

    void _add_val(ll x, int a, int b, int k, int l, int r) {
        if(b <= l || r <= a) {
            return;
        }
        if(a <= l && r <= b) {
        addall(k, x);
            return;
        }

        push(k);
        _add_val(x, a, b, 2*k+1, l, (l+r)/2);
        _add_val(x, a, b, 2*k+2, (l+r)/2, r);
        update(k);
    }

    void _update_val(ll x, int a, int b, int k, int l, int r) {
        if(b <= l || r <= a) {
            return;
        }
        if(a <= l && r <= b) {
            updateall(k, x);
            return;
        }

        push(k);
        _update_val(x, a, b, 2*k+1, l, (l+r)/2);
        _update_val(x, a, b, 2*k+2, (l+r)/2, r);
        update(k);
    }

    ll _query_max(int a, int b, int k, int l, int r) {
        if(b <= l || r <= a) {
            return -INF;
        }
        if(a <= l && r <= b) {
            return max_v[k];
        }
        push(k);
        ll lv = _query_max(a, b, 2*k+1, l, (l+r)/2);
        ll rv = _query_max(a, b, 2*k+2, (l+r)/2, r);
        return max(lv, rv);
    }

    ll _query_min(int a, int b, int k, int l, int r) {
        if(b <= l || r <= a) {
            return INF;
        }
        if(a <= l && r <= b) {
            return min_v[k];
        }
        push(k);
        ll lv = _query_min(a, b, 2*k+1, l, (l+r)/2);
        ll rv = _query_min(a, b, 2*k+2, (l+r)/2, r);
        return min(lv, rv);
    }

    ll _query_sum(int a, int b, int k, int l, int r) {
        if(b <= l || r <= a) {
            return 0;
        }
        if(a <= l && r <= b) {
            return sum[k];
        }
        push(k);
        ll lv = _query_sum(a, b, 2*k+1, l, (l+r)/2);
        ll rv = _query_sum(a, b, 2*k+2, (l+r)/2, r);
        return lv + rv;
    }

    // range minimize query
    void update_min(int a, int b, ll x) {
        _update_min(x, a, b, 0, 0, n0);
    }

    // range maximize query
    void update_max(int a, int b, ll x) {
        _update_max(x, a, b, 0, 0, n0);
    }

    // range add query
    void add_val(int a, int b, ll x) {
        _add_val(x, a, b, 0, 0, n0);
    }

    // range update query
    void update_val(int a, int b, ll x) {
        _update_val(x, a, b, 0, 0, n0);
    }

    // range minimum query
    ll query_max(int a, int b) {
        return _query_max(a, b, 0, 0, n0);
    }

    // range maximum query
    ll query_min(int a, int b) {
        return _query_min(a, b, 0, 0, n0);
    }

    // range sum query
    ll query_sum(int a, int b) {
        return _query_sum(a, b, 0, 0, n0);
    }

    vector<ll> get_all() {
        vector<ll> ret(n);
        for(int i = 0; i < n; i++) {
            ret[i] = query_max(i, i + 1);
        }
        return ret;
    }
    inline int operator[](int i) {
        return query_max(i, i + 1);
    }
    void print() {
        for(int i = 0; i < n; i++) {
            cout << (*this)[i];
            if(i != n - 1) cout << ",";
        }
        cout << endl;
    }
};

void solve() {
    int h, w;
    cin >> h >> w;
    int sx, sy, gx, gy;
    cin >> sx >> sy >> gx >> gy;
    --sx, --sy;
    --gx, --gy;
    vector<string> s(h);
    FOR(h) cin >> s[i];

    vvi blocks(h);
    rep(i, h) {
        blocks[i].push_back(-1);
        rep(j, w) {
            if(s[i][j] == '#') {
                blocks[i].push_back(j);
            }
        }
        blocks[i].push_back(w);
    }

    vector<SegmentTree> dp(h, SegmentTree(w));
    rep(i, h) dp[i].update_val(0, w, INF);

    vpii seg(w);
    auto update_segs = [&](int i) -> void {
        rep(j, w) if(s[i][j] == '.') {
            auto rit = lower_bound(all(blocks[i]), j);
            auto lit = --rit; ++rit;
            chmin(seg[j].first, *lit);
            chmax(seg[j].second, *rit);
        }
        return;
    };
    update_segs(0);

    dp[0].update_val(seg[sy].first+1, seg[sy].second, 0);

    rep(i, h-1) {
        rep(j, w) if(s[i][j] == '.') {
            if(j > 0 && s[i+1][j-1] == '.') {
                dp[i+1].update_min(j-1, j, dp[i][j]);
            }
            if(j < w-1 && s[i+1][j+1] == '.') {
                dp[i+1].update_min(j+1, j+2, dp[i][j]);
            }

            if(j > 0) dp[i+1].update_min(max(0LL, seg[j-1].first-1), seg[j-1].second+2, dp[i][j]+1);
            if(j < w-1) dp[i+1].update_min(max(0LL, seg[j+1].first-1), seg[j+1].second+2, dp[i][j]+1);
        }

        update_segs(i+1);

        rep(j, w) if(s[i+1][j] == '#') {
            dp[i+1].update_val(j, j+1, INF);
        }

        rep(j, w) if(s[i+1][j] == '.') {
            auto rit = lower_bound(all(blocks[i+1]), j);
            auto lit = --rit; ++rit;
            dp[i+1].update_min(*lit+1, *rit, dp[i+1].query_min(*lit+1, *rit));
        }
    }

    if(dp[gx][gy] != INF) cout << dp[gx][gy] << endl;
    else cout << -1 << endl;


    // vector<bool> ex(w, false);
    // vvi dp(h, vi(w, INF));
    // dp[sx][sy] = 0;

    // rep(i, h-1) {
    //     rep(j, w) {
    //         if(j > 0 && s[i+1][j-1] == '.') {
    //             chmin(dp[i+1][j-1], dp[i][j]);
    //         }
    //         if(j < w-1 && s[i+1][j+1] == '.') {
    //             chmin(dp[i+1][j+1], dp[i][j]);
    //         }
    //         if((j > 0 && ex[j-1]) || (j < w-1 && ex[j+1])) {
    //             chmin(dp[i+1][j], dp[i][j]+1);
    //         }
    //         if(j > 1 && ex[j-1]) {
    //             chmin(dp[i+1][j-2], dp[i][j]+1);
    //         }
    //         if(j < w-2 && ex[j+1]) {
    //             chmin(dp[i+1][j+2], dp[i][j]+1);
    //         }
    //     }
    //     rep(j, w) {
    //         if(s[i][j] == '.') ex[j] = true;
    //     }
    // }

    // if(dp[gx][gy] == INF) cout << -1 << endl;
    // else cout << dp[gx][gy] << endl;
}

signed main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    solve();
    return 0;
}
0