結果

問題 No.2157 崖
コンテスト
ユーザー eQe
提出日時 2026-11-11 00:12:18
言語 C++23
(gcc 15.3.0 + boost 1.92.0 + ACL)
コンパイル:
g++-15 -O2 -fplugin=/usr/lib/yukicoder/noavx512-gcc15.so -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 3,983 ms / 6,000 ms
+ 244µs
コード長 6,884 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 3,383 ms
コンパイル使用メモリ 363,104 KB
実行使用メモリ 27,568 KB
最終ジャッジ日時 2026-11-11 00:13:09
合計ジャッジ時間 47,589 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge7_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 22
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

/*
#include<kyopro/master>
void self::main(){
  LL(N,M);
  VVL(N,M,a);
  fo(i,N)sort(a[i]);
  
  ll D=binary_search_first_true(-1,(int)oo,[&](ll D){
    vec dp(N+1,M,int{});
    fo(j,M)dp[1][j]=1;
    
    fo(i,1,N){
      SegMax<int>s(dp[i]);
      fo(j,M){
        ll l=a[i-1].first_ge(a[i][j]-D).dist();
        ll r=a[i-1].first_gt(a[i][j]).dist();
        dp[i+1][j]=s.prod(l,r);
      }
    }
    return dp[N].max()>0;
  });
  pp(D==(int)oo?-1:D);
}
*/

#include<bits/stdc++.h>
using namespace std;
namespace self{void main();void sub();void pp(auto&&...a);void ppe(auto&&...a);}
int main(){cin.tie(nullptr)->sync_with_stdio(0);cout<<fixed<<setprecision(15);self::main();}
#define RD(T,...) T __VA_ARGS__;lin(__VA_ARGS__)
#define LL(...) RD(ll,__VA_ARGS__)
#define RDVVL(T,n,m,...) vec<vec<T>>__VA_ARGS__;fe(refs(__VA_ARGS__),e)e.get().resizes(n,m);lin(__VA_ARGS__)
#define VVL(n,m,...) RDVVL(ll,n,m,__VA_ARGS__)
#define fo(i,...) for(auto[i,i##stop,i##step]=for_range<ll>(0,__VA_ARGS__);i<i##stop;i+=i##step)
#define of(i,...) for(auto[i,i##stop,i##step]=for_range<ll>(1,__VA_ARGS__);i>=i##stop;i+=i##step)
#define fe(a,e,...) for(auto&&__VA_OPT__([)e __VA_OPT__(,__VA_ARGS__]):a)
namespace self{
using ll=long long;
constexpr auto refs(auto&...a){return array{ref(a)...};}
template<class T>constexpr auto for_range(T s,T b){T a=0;if(s)swap(a,b);return array{a-s,b,1-s*2};}
template<class T>constexpr auto for_range(T s,T a,T b,T c=1){return array{a-s,b,(1-s*2)*c};}
void lin(auto&...a){(cin>>...>>a);}
constexpr ll leading_zero64(ll x){return __builtin_clzll(x);}
constexpr ll trailing_zero64(ll x){return __builtin_ctzll(x);}
constexpr ll most_significant_bit(ll x){return x?63-leading_zero64(x):-1;}
constexpr ll size2(auto x){return most_significant_bit(x|1)+1;}
constexpr ll size10(auto x){x|=1;ll r=0;while(x>0)x/=10,++r;return r;}
template<class T>constexpr ll maxsize10(){return size10(numeric_limits<T>::max());}
template<class T=ll>T pow2(ll n){return T(1)<<n;}
template<class T=ll>T gemin_pow2(T x){if(x<=1)return 1;return pow2<T>(size2(x-1));}
constexpr ll log2_floor(auto x){return size2(x)-1;}
auto max(auto...a){return max(initializer_list<common_type_t<decltype(a)...>>{a...});}
auto min(auto...a){return min(initializer_list<common_type_t<decltype(a)...>>{a...});}
ll binary_search_first_true(ll l,ll r,auto&&f){while(l+1<r)(f((l+r)/2)?r:l)=(l+r)/2;return r;}
template<class A,class B=A>struct pair{
  A a;B b;
  pair()=default;
  pair(A a,B b):a(a),b(b){}
  auto operator<=>(const pair&)const=default;
};
template<bool is_negative=false>struct infinity{
  template<integral T>static constexpr T ones(size_t n){return n?ones<T>(n-1)*10+1:0;}
  template<integral T>constexpr operator T()const{return ones<T>(maxsize10<T>())*(1-is_negative*2);}
  constexpr auto operator-()const{return infinity<!is_negative>();}
};
constexpr infinity oo;
template<class F=less<>>decltype(auto)sort(auto&&a,F f={}){ranges::sort(a,f);return a;}
template<class...A>using pack_back_t=tuple_element_t<sizeof...(A)-1,tuple<A...>>;
template<class I>class Iterator{
private:
  I it_,begin_,end_;
public:
  Iterator(I it,I begin,I end):it_(it),begin_(begin),end_(end){}
  ll dist()const{return it_-begin_;}
};
}
namespace self{
template<class V>concept vectorial=is_base_of_v<vector<typename remove_cvref_t<V>::value_type>,remove_cvref_t<V>>;
template<class V>istream&operator>>(istream&i,vector<V>&v){fe(v,e)i>>e;return i;}
template<class V>constexpr int depth=0;
template<vectorial V>constexpr int depth<V> =depth<typename V::value_type>+1;
template<class T>struct core_t_helper{using type=T;};
template<vectorial V>struct core_t_helper<V>{using type=typename core_t_helper<typename V::value_type>::type;};
template<class T>using core_t=core_t_helper<T>::type;
template<class V>struct vec;
template<int D,class T>struct hvec_helper{using type=vec<typename hvec_helper<D-1,T>::type>;};
template<class T>struct hvec_helper<0,T>{using type=T;};
template<int D,class T>using hvec=hvec_helper<D,T>::type;
template<class V>struct vec:vector<V>{
  static constexpr int D=depth<V>+1;
  using C=core_t<V>;
  using vector<V>::vector;
  vec(const auto&...a)requires(sizeof...(a)>=3){resizes(a...);}
  void resizes(const auto&...a){if constexpr(sizeof...(a)==D)*this=make(a...,C{});else*this=make(a...);}
  static auto make(ll n,const auto&...a){
    if constexpr(sizeof...(a)==1)return vec<C>(n,array{a...}[0]);
    else return vec<decltype(make(a...))>(n,make(a...));
  }
  auto first_true(const V&x,auto&&f)const{
    auto b=this->begin(),e=this->end();
    auto p=partition_point(b,e,[&x,&f](const V&v){return !f(x,v);});
    return Iterator{p,b,e};
  }
  auto first_ge(const V&x)const{return first_true(x,[](auto&x,auto&e){return x<=e;});}
  auto first_gt(const V&x)const{return first_true(x,[](auto&x,auto&e){return x<e;});}
  template<class F>void for_each_core(F&&f)const{fe(*this,e){if constexpr(vectorial<V>){ }else f(e);}}
  template<class T=C>auto fold(auto&&f)const{
    pair<T,bool>res{};
    for_each_core([&](const C&e){
      if(res.b)f(res.a,e);
      else res={e,1};
    });
    return res;
  }
  auto max()const{return fold([](auto&a,auto b){if(a<b)a=b;}).a;}
};
template<class...A>requires(sizeof...(A)>=2)vec(const A&...a)->vec<hvec<sizeof...(A)-2,pack_back_t<A...>>>;
}
namespace self{
template<class S,S op(S,S),S e()>class SegTree{
public:
  ll n_,leaf_size_;
  vector<S>d_;
  explicit SegTree(const vector<S>&v):n_(v.size()){
    leaf_size_=gemin_pow2(n_);
    d_=vector<S>(leaf_size_*2,e());
    fo(i,n_)d_[i+leaf_size_]=v[i];
    of(i,leaf_size_,1)pull(i);
  }
  void pull(ll k){d_[k]=op(d_[k*2],d_[k*2+1]);}
  void for_each_canonical_cover(ll j,ll k,auto&&g)const{
    while(j<k){
      ll t=min(trailing_zero64(j),log2_floor(k-j));
      g(j>>t);
      j+=1<<t;
    }
  }
  S prod(ll l,ll r)const{
    assert(0<=l&&l<=r&&r<=n_);
    if(l==r)return e();
    l+=leaf_size_,r+=leaf_size_;

    S cum=e();
    for_each_canonical_cover(l,r,[&](ll k){cum=op(cum,d_[k]);});
    return cum;
  }
};
}
namespace self{
template<class T>struct max_node{
  using S=T;
  static S op(S a,S b){return max(a,b);}
  static S e(){return-oo;}
};
}
namespace self{
template<class M>using SegTreeMonoidAlias=SegTree<typename M::S,M::op,M::e>;
template<class T>class SegMax:public SegTreeMonoidAlias<max_node<T>>{
public:
  using SegTreeMonoidAlias<max_node<T>>::SegTreeMonoidAlias;
};
}
void self::pp(auto&&...a){const char*c="";((cout<<c<<a,c=" "),...);cout<<'\n';}
void self::main(){
  LL(N,M);
  VVL(N,M,a);
  fo(i,N)sort(a[i]);

  ll D=binary_search_first_true(-1,(int)oo,[&](ll D){
    vec dp(N+1,M,int{});
    fo(j,M)dp[1][j]=1;

    fo(i,1,N){
      SegMax<int>s(dp[i]);
      fo(j,M){
        ll l=a[i-1].first_ge(a[i][j]-D).dist();
        ll r=a[i-1].first_gt(a[i][j]).dist();
        dp[i+1][j]=s.prod(l,r);
      }
    }
    return dp[N].max()>0;
  });
  pp(D==(int)oo?-1:D);
}
0