結果

問題 No.2543 Many Meetings
ユーザー fumofumofuni
提出日時 2023-11-25 18:36:25
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 335 ms / 2,000 ms
コード長 2,467 bytes
コンパイル時間 2,390 ms
コンパイル使用メモリ 207,768 KB
最終ジャッジ日時 2025-02-18 01:07:41
ジャッジサーバーID
(参考情報)
judge5 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 40
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
//#pragma GCC optimize("Ofast")

#define rep(i,n) for(ll i=0;i<n;i++)
#define repl(i,l,r) for(ll i=(l);i<(r);i++)
#define per(i,n) for(ll i=(n)-1;i>=0;i--)
#define perl(i,r,l) for(ll i=r-1;i>=l;i--)
#define fi first
#define se second
#define pb push_back
#define ins insert
#define pqueue(x) priority_queue<x,vector<x>,greater<x>>
#define all(x) (x).begin(),(x).end()
#define CST(x) cout<<fixed<<setprecision(x)
#define vtpl(x,y,z) vector<tuple<x,y,z>>
#define rev(x) reverse(x);
using ll=long long;
using vl=vector<ll>;
using vvl=vector<vector<ll>>;
using pl=pair<ll,ll>;
using vpl=vector<pl>;
using vvpl=vector<vpl>;
const ll MOD=1000000007;
const ll MOD9=998244353;
const int inf=1e9+10;
const ll INF=4e18;
const ll dy[9]={1,0,-1,0,1,1,-1,-1,0};
const ll dx[9]={0,1,0,-1,1,-1,1,-1,0};
template<class T> inline bool chmin(T& a, T b) {
    if (a >= b) {
        a = b;
        return true;
    }
    return false;
}
template<class T> inline bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

int main(){
    ll n,k;cin >> n >> k;
    vector<pair<pl,ll>> v(n);
    rep(i,n)cin >> v[i].first.first >> v[i].first.second;
    rep(i,n)v[i].second=i;
    auto nl=v;sort(all(nl));
    auto nr=v;sort(all(nr),[](pair<pl,ll> a,pair<pl,ll> b){return a.first.second<b.first.second;});
    ll rx=INF,idx=-1,now=n-1;
    vvl dp(30,vl(n));
    per(i,n){
        while(now>=0&&nl[now].first.first>=nr[i].first.second){
            if(chmin(rx,nl[now].first.second))idx=nl[now].second;
            now--;
        }
        dp[0][nr[i].second]=idx;
    }
    //rep(i,n)cout << dp[0][i] <<" ";cout << endl;
    rep(j,29){
        rep(i,n){
            if(dp[j][i]==-1){
                dp[j+1][i]=-1;
            }
            else {
                dp[j+1][i]=dp[j][dp[j][i]];
            }
        }
    }
    //rep(i,n)cout << dp[0][i] <<" ";cout << endl;
    //rep(i,n)cout << dp[1][i] <<" ";cout << endl;
    k--;
    ll ans=INF;
    rep(i,n){
        ll now=i;
        rep(j,30){
            if(now==-1)break;
            if(k>>j&1)now=dp[j][now];
        }
        //cout << now << endl;
        if(now!=-1){
            //cout << v[now].first.second <<" " << v[i].first.first << endl;
            chmin(ans,v[now].first.second-v[i].first.first);
            //cout << now <<" " << i << endl;
        }
    }
    if(ans==INF)cout << -1 << endl;
    else cout << ans << endl;

}

0