結果
問題 | No.2543 Many Meetings |
ユーザー |
![]() |
提出日時 | 2023-11-24 21:49:02 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 262 ms / 2,000 ms |
コード長 | 2,056 bytes |
コンパイル時間 | 4,256 ms |
コンパイル使用メモリ | 259,264 KB |
最終ジャッジ日時 | 2025-02-17 23:58:56 |
ジャッジサーバーID (参考情報) |
judge4 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 40 |
ソースコード
#include<bits/stdc++.h> using namespace std; //* ATCODER #include<atcoder/all> using namespace atcoder; typedef modint998244353 mint; //*/ /* BOOST MULTIPRECISION #include<boost/multiprecision/cpp_int.hpp> using namespace boost::multiprecision; //*/ typedef long long ll; #define rep(i, s, n) for (int i = (int)(s); i < (int)(n); i++) #define rrep(i, s, n) for (int i = (int)(n)-1; i >= (int)(s); i--) template <typename T> bool chmin(T &a, const T &b) { if (a <= b) return false; a = b; return true; } template <typename T> bool chmax(T &a, const T &b) { if (a >= b) return false; a = b; return true; } template <typename T> T max(vector<T> &a){ assert(!a.empty()); T ret = a[0]; for (int i=0; i<(int)a.size(); i++) chmax(ret, a[i]); return ret; } template <typename T> T min(vector<T> &a){ assert(!a.empty()); T ret = a[0]; for (int i=0; i<(int)a.size(); i++) chmin(ret, a[i]); return ret; } template <typename T> T sum(vector<T> &a){ T ret = 0; for (int i=0; i<(int)a.size(); i++) ret += a[i]; return ret; } ll op(ll l, ll r){ return max(l, r); } ll e(){ return -1e14; } int main(){ // ダブリングを行う int n, k; cin >> n >> k; vector<pair<ll,ll>> x(n); rep(i,0,n){ ll l, r; cin >> l >> r; x[i] = pair(l, r); } sort(x.begin(), x.end(), [](const pair<ll,ll> a, const pair<ll,ll> b){ return pair(a.second, a.first) < pair(b.second, b.first); }); vector db(20, vector<ll>(n+1, n)); segtree<ll,op,e> seg(n); rep(i,0,n){ seg.set(i, x[i].first); } ll g; auto f = [&](ll x){ return x < g; }; rep(i,0,n){ g = x[i].second; int z = seg.max_right(i+1, f); db[0][i] = z; //cout << i << ' ' << z << endl; } rep(num,0,19){ rep(i,0,n){ db[num+1][i] = db[num][db[num][i]]; } } const ll inf = 1e18; ll ans = inf; k--; rep(i,0,n){ // kokokara start int tar = i; rep(j,0,20){ if (k >> j & 1){ tar = db[j][tar]; } } if (tar < n){ chmin(ans, x[tar].second - x[i].first); } } if (ans == inf){ cout << -1 << '\n'; }else{ cout << ans << '\n'; } }