結果

問題 No.3679 なんかでっかい虫リターンズ
コンテスト
ユーザー 541
提出日時 2026-09-05 15:36:38
言語 C++23(gcc16)
(gcc 16.1.0 + boost 1.92.0)
コンパイル:
g++-16 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 2 ms / 2,000 ms
+ 269µs
コード長 5,473 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,545 ms
コンパイル使用メモリ 369,300 KB
実行使用メモリ 9,788 KB
最終ジャッジ日時 2026-09-05 15:36:50
合計ジャッジ時間 4,313 ms
ジャッジサーバーID
(参考情報)
judge5_0 / judge4_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 17
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#ifndef ONLINE_JUDGE
#define _GLIBCXX_DEBUG
#endif

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

using ll=int64_t;
using ull=uint64_t;
using lll=__int128_t;
using ulll=__uint128_t;

using db=double;
using ld=long double;

using pint=pair<int,int>;
using pill=pair<int,ll>;
using pll=pair<ll,ll>;
using plli=pair<ll,int>;

template<typename T1,typename T2>
using unmap=unordered_map<T1,T2>;
template<typename T>
using unset=unordered_set<T>;

template<typename T>
using heap=priority_queue<T>;
template<typename T>
using min_heap=priority_queue<T,vector<T>,greater<T>>;

namespace rgs=ranges;

#define rep(i,l,r) for(ll i=(ll)(l);i<(ll)(r);i++)
#define per(i,r,l) for(ll i=(ll)(r);i>=(ll)(l);i--)

#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()

#define ep emplace
#define ef emplace_front
#define pof pop_front
#define eb emplace_back
#define pob pop_back

#define fi first
#define se second

#define YES cout<<"Yes\n"
#define NO cout<<"No\n"
#define YN(flag) (flag) ? YES:NO
#define ENDL cout<<'\n'

const int INF32=(1<<30)-1;
const ll INF64=(1ll<<62)-1;
const ll MOD99=998244353;
const ll MOD10=1000000007;
const ld PI=acosl(-1);

const array<int,8> di={1,0,-1,0,-1,1,-1,1};
const array<int,8> dj={0,1,0,-1,-1,1,1,-1};

template<typename T1,typename T2>
istream& operator>>(istream &is,pair<T1,T2> &x){ return is>>x.fi>>x.se; }
template<typename T1,typename T2>
ostream& operator<<(ostream &os,const pair<T1,T2> &x){ return os<<x.fi<<' '<<x.se; }

template<typename T>
istream& operator>>(istream &is,vector<T> &vec){ rep(i,0,size(vec)) is>>vec[i]; return is; }
template<typename T>
ostream& operator<<(ostream &os,const vector<T> &vec){ rep(i,0,size(vec)) (i==0 ? os<<vec[i]:os<<' '<<vec[i]); return os; }
template<typename T>
ostream& operator<<(ostream &os,const vector<vector<T>> &vec){ rep(i,0,size(vec)) (i==0 ? os<<vec[i]:os<<'\n'<<vec[i]); return os; }

template<typename T,size_t N>
istream& operator>>(istream &is,array<T,N> &arr){ rep(i,0,N) is>>arr[i]; return is; }
template<typename T,size_t N>
ostream& operator<<(ostream &os,const array<T,N> &arr){ rep(i,0,N) (i==0 ? os<<arr[i]:os<<' '<<arr[i]); return os; }

template<typename T>
ostream& operator<<(ostream &os,const set<T> &st){ size_t i=0; for(T x : st) (i++==0 ? os<<x:os<<' '<<x); return os; }
template<typename T>
ostream& operator<<(ostream &os,const multiset<T> &mst){ size_t i=0; for(T x : mst) (i++==0 ? os<<x:os<<' '<<x); return os; }

void input(){ 'v'; }
template<typename Head,typename... Tail>
void input(Head &head,Tail&... tail){ cin>>head; input(tail...); }

void output(){ 'v'; }
template<typename Head,typename... Tail>
void output(const Head &head,const Tail&... tail){ cout<<head; if(sizeof...(tail)>0) cout<<' '; output(tail...); }

void outputln(){ ENDL; }
template<typename Head,typename... Tail>
void outputln(const Head &head,const Tail&... tail){ cout<<head; if(sizeof...(tail)>0) cout<<' '; outputln(tail...); }

void outputlines(){ ENDL; }
template<typename Head,typename... Tail>
void outputlines(const Head &head,const Tail&... tail){ cout<<head; if(sizeof...(tail)>0) cout<<'\n'; outputlines(tail...); }

void lines(int n=10){ while(n--) cout<<'-'; ENDL; }

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

template<typename T>
bool outside(const T &i,const T &j,const T &h,const T &w) { return (i<0 or i>=h or j<0 or j>=w); }

template<typename T>
T divfloor(T x,T y ){ if(y<0) x*=(-1),y*=(-1); return(x>=0 ? x/y:(x-y+1)/y); }
template<typename T>
T divceil(T x,T y) { if(y<0) x*=(-1),y*=(-1); return(x>=0 ? (x+y-1)/y:x/y); }

void flip(bool& b) { b^=1; }

////////////////////////////////////////////////////////////////////////////////


bool solve(){
    int h,w,a,b,r1,c1,r2,c2,p,q;
    input(h,w,a,b,r1,c1,r2,c2,p,q);
    a--,b--,r1--,c1--,p--,q--;

    auto bfs=[&](int si,int sj,auto goal)->pair<int,pint> {
        vector<vector<int>> dist(h,vector<int>(w,INF32));
        dist[si][sj]=0;
        queue<pint> q;
        q.ep(si,sj);
        int gi=-1,gj=-1;
        while(!q.empty()){
            auto [i,j]=q.front();
            q.pop();
            //outputln(i,j,"|",goal(i,j));
            if(goal(i,j)){
                gi=i,gj=j;
                //outputln("(",i,",",j,")","(",gi,",",gj,")");
                break;
            }
            rep(k,0,4){
                int ni=i+di[k],nj=j+dj[k];
                if(outside(ni,nj,h,w)) continue;
                if(chmin(dist[ni][nj],dist[i][j]+1)) q.ep(ni,nj);
            }
        }
        //assert(gi!=-1); assert(gj!=-1);
        return {dist[gi][gj],{gi,gj}};
    };

    int ans=0,si=a,sj=b;
    {
        auto f=[&](int i,int j)->bool {
            return (r1<=i and i<r2 and c1<=j and j<c2);
        };
        auto [d,g]=bfs(si,sj,f);
        ans+=d;
        tie(si,sj)=g;
        //outputln(d);
    }
    {
        auto f=[&](int i,int j){
            return tie(i,j)==tie(p,q);
        };
        auto [d,g]=bfs(si,sj,f);
        ans+=d;
        tie(si,sj)=g;
        //outputln(d);
    }
    {
        auto f=[&](int i,int j){
            return tie(i,j)==tie(a,b);
        };
        auto [d,g]=bfs(si,sj,f);
        ans+=d;
        tie(si,sj)=g;
        //outputln(d);
    }
    outputln(ans);

    return 0;
}

int main(){
    cout<<fixed<<setprecision(20);

    int t=1;
    //cin>>t;

    while(t--) while(solve());

    return 0;
}
0