/** * code generated by JHelperX * More info: https://github.com/GoBigorGoHome/JHelperX * @author zjs */ #if not defined LOCAL and not defined NDEBUG #define NDEBUG #endif #define debug(...) #define show(...) extern const bool show_all_failed_tests = false; extern const bool compare_real_numbers = false; // #define INTERACTIVE_MODE //对于交互题,取消此行注释。 // // Created by zjsdu on 5/28/2020. // #ifndef JHELPER_EXAMPLE_PROJECT_LIBRARY_ALIAS_HPP_ #define JHELPER_EXAMPLE_PROJECT_LIBRARY_ALIAS_HPP_ #include #include #include #ifndef JHELPER_EXAMPLE_PROJECT_LIBRARY_IO_HPP_ #define JHELPER_EXAMPLE_PROJECT_LIBRARY_IO_HPP_ #include #include #include // // Created by zjsdu on 10/22/2020. // #ifndef JHELPER_EXAMPLE_PROJECT_TASKS_TYPE_TRAITS_HPP_ #define JHELPER_EXAMPLE_PROJECT_TASKS_TYPE_TRAITS_HPP_ #include #if __cplusplus >= 201703L template T type();// no definition template auto value_type_of_() { if constexpr (std::is_array_v) return type>(); else return type(); } template using value_type_of = decltype(value_type_of_>()); #else template struct value_type_of_impl // default, non-array { using type = typename Container::value_type; }; template struct value_type_of_impl // arrays { using type = T; }; template using value_type_of = typename value_type_of_impl::type; #endif // Source: https://foonathan.net/2020/10/iife-metaprogramming/ #if __cplusplus >= 201703L namespace is_iterable_impl { using std::begin; using std::end; template using check_specs = std::void_t< std::enable_if_t< std::is_same())),// has begin() decltype(end(std::declval())) // has end() >::value>,// ... begin() and end() are the same type ... decltype(*begin(std::declval()))>;// ... which can be dereferenced template struct is_iterable : std::false_type {}; // specialization template struct is_iterable> : std::true_type {}; }// namespace is_iterable_impl template using is_iterable = is_iterable_impl::is_iterable; template constexpr bool is_iterable_v = is_iterable::value; // Source: https://stackoverflow.com/a/53429396/6793559 template using is_string = std::disjunction>, std::is_same>, std::is_same>>; template constexpr bool is_string_v = is_string::value; // Source: https://stackoverflow.com/a/57812868/6793559 template typename Target, typename Aux, typename... Ts> struct is_specialized_for_impl : std::false_type {}; template typename Target, typename... Args> struct is_specialized_for_impl)), Args...> : std::true_type {}; template typename Target, typename... Args> using is_specialized_for = is_specialized_for_impl; template typename Target, typename... Args> constexpr bool is_specialized_for_v = is_specialized_for::value; template using is_tuple_like = is_specialized_for; template constexpr bool is_tuple_like_v = is_tuple_like::value; template struct remove_all_extents_ { typedef std::remove_reference_t type; }; template struct remove_all_extents_()[0])>> { typedef typename remove_all_extents_()[0])>::type type; }; template struct rank_ : public std::integral_constant {}; template struct rank_()[0])>> : public std::integral_constant< std::size_t, rank_()[0])>::value + 1> {}; #endif #endif// JHELPER_EXAMPLE_PROJECT_TASKS_TYPE_TRAITS_HPP_ struct fast_ios { fast_ios() { #ifndef INTERACTIVE_MODE std::cin.tie(nullptr); #endif std::ios::sync_with_stdio(false); std::cout.precision(15); std::cout << std::fixed; }; } const fast_ios_; namespace io { template std::ostream &operator<<(std::ostream &out, const std::pair &p); template std::istream &operator>>(std::istream &in, std::tuple &t); template std::ostream &operator<<(std::ostream &, const std::tuple &); template std::istream &operator>>(std::istream &in, std::pair &p) { in >> p.first >> p.second; return in; } template std::istream &operator>>(std::istream &stream, std::vector &vec) { for (auto &x : vec) stream >> x; return stream; } #if __cplusplus >= 201703L template std::istream &operator>>(std::istream &in, std::tuple &t) { std::apply([&in](auto &...args) { ((in >> args), ...); }, t); return in; } template void scan(Args &...args) { ((std::cin >> args), ...); } template, std::negation>>>> std::ostream &operator<<(std::ostream &out, const Container &container) { using std::begin; using value_type = std::remove_reference_t()))>; constexpr char delimiter = is_iterable_v or is_tuple_like_v ? '\n' : ' '; bool first = true; for (auto &element : container) { if (first) first = false; else out << delimiter; out << element; } return out; } // Source: https://en.cppreference.com/w/cpp/utility/apply template std::ostream &operator<<(std::ostream &out, const std::tuple &theTuple) { std::apply( [&out](Ts const &...tupleArgs) { std::size_t n{0}; ((out << tupleArgs << (++n != sizeof...(Ts) ? " " : "")), ...); }, theTuple); return out; } #endif template std::ostream &operator<<(std::ostream &out, const std::pair &p) { out << p.first << ' ' << p.second; return out; } template std::ostream &operator<<(std::ostream &out, const std::vector> &t) { bool is_first = true; for (const auto &row : t) { if (is_first) is_first = false; else out << '\n'; out << row; } return out; } std::ostream &operator<<(std::ostream &os, __int128 n) { if (n < 0) { n = -n; os << '-'; } std::string s; while (n > 0) { s += char('0' + n % 10); n /= 10; } for (int i = (int) s.size() - 1; i >= 0; i--) os << s[i]; return os; } #if __cplusplus >= 201703L template void pt(Args &&...args) { ((std::cout << args << ' '), ...); } template void pl(const First &first, const Args &...args) { std::cout << first; ((std::cout << ' ' << args), ...); std::cout << '\n'; } template void pn(const Args &...args) { ((std::cout << args << '\n'), ...); } #endif }// namespace io #endif// JHELPER_EXAMPLE_PROJECT_LIBRARY_IO_HPP_ // // Created by zjsdu on 10/26/2020. // #ifndef JHELPER_EXAMPLE_PROJECT_LIBRARY_NDARRAY_HPP_ #define JHELPER_EXAMPLE_PROJECT_LIBRARY_NDARRAY_HPP_ template struct ndvec { using type = std::vector::type>; }; template struct ndvec { using type = T; }; // arbitrary-dimensional array that allows non-constexpr extents. // Usage: ndarray arr(extents..., init_value); // An initial value for all array items can be specified if all extensions are // specified. // Examples: // ndarray<2, int> a(2, 3, -1); // ndarray<3, int> b(2, 3, 4); // ndarray<3, int> c(2, 3); template class ndarray : public ndvec::type { using base_type = typename ndvec::type; using value_type = typename base_type::value_type; using base_type::base_type; public: template ndarray(unsigned d, Args... args) : std::vector(d, ndarray(args...)) {} }; template class ndarray<1, T> : public std::vector { using std::vector::vector; }; #endif// JHELPER_EXAMPLE_PROJECT_LIBRARY_NDARRAY_HPP_ // // Created by zjsdu on 2/9/2021. // #ifndef JHELPER_EXAMPLE_PROJECT_LIBRARY_MACROS_H_ #define JHELPER_EXAMPLE_PROJECT_LIBRARY_MACROS_H_ #define CALL_WITH_EXPANDED_ARGS(function, ...) function(__VA_ARGS__) #define JOIN_IMPL(arg1, arg2) arg1##arg2 #define JOIN(arg1, arg2) JOIN_IMPL(arg1, arg2) #define EXPAND_1(...) __VA_ARGS__ #define EXPAND_4(...) EXPAND_1(EXPAND_1(EXPAND_1(__VA_ARGS__))) #define EXPAND_13(...) EXPAND_4(EXPAND_4(EXPAND_4(__VA_ARGS__))) #define PAUSE #define COMMA() , #define TERMINATE(...) #define SELECT_SECOND_ARG(arg1, arg2, ...) arg2 #define CONDITIONAL(peek, arg1, arg2) \ CALL_WITH_EXPANDED_ARGS(SELECT_SECOND_ARG, COMMA peek arg1, arg2) #define TERMINATE_OR(peek, arg) CONDITIONAL(peek, TERMINATE, arg) #define FOR_EACH_2_IMPL0(function, arg1, arg2, peek, ...) \ function(arg1, arg2) TERMINATE_OR(peek, FOR_EACH_2_IMPL1) \ PAUSE(function, peek, __VA_ARGS__) #define FOR_EACH_2_IMPL1(function, arg1, arg2, peek, ...) \ function(arg1, arg2) TERMINATE_OR(peek, FOR_EACH_2_IMPL0) \ PAUSE(function, peek, __VA_ARGS__) #define FOR_EACH_2(function, ...) \ EXPAND_13(FOR_EACH_2_IMPL0(function, __VA_ARGS__, ())) #endif// JHELPER_EXAMPLE_PROJECT_LIBRARY_MACROS_H_ using ll = long long; using ull = unsigned long long; using i128 = __int128; using vl = std::vector; using vb = std::vector; using vi = std::vector; using vs = std::vector; using pii = std::pair; using pli = std::pair; using pil = std::pair; using pll = std::pair; using vii = std::vector; template using pq = std::priority_queue; template using min_pq = std::priority_queue, std::greater>; template using vt = std::vector>; template using vv = ndarray<2, T>; template struct range_tuple { const T &ref = beg; T beg; const T end; range_tuple(T b, T e) : beg(b), end(e) {} }; #define rng4(i, a, b, c) \ for (auto &&[i, JOIN(iter_, __LINE__), JOIN(end_, __LINE__)] = \ range_tuple::type>(a, \ b); \ i < JOIN(end_, __LINE__); JOIN(iter_, __LINE__) += c) #define rng3(i, a, b) rng4(i, a, b, 1) #define rng2(i, n) rng3(i, 0, n) #define GET4(_1, _2, _3, _4, NAME, ...) NAME #define rng(...) GET4(__VA_ARGS__, rng4, rng3, rng2)(__VA_ARGS__) #define up4(i, a, b, c) rng (i, a, b + 1, c) #define up3(i, a, b) up4(i, a, b, 1) #define up(...) GET4(__VA_ARGS__, up4, up3, NO_IMPL)(__VA_ARGS__) #define down4(i, b, a, c) \ for (auto &&[i, JOIN(iter_, __LINE__), JOIN(end_, __LINE__)] = \ range_tuple::type>(b, \ a); \ i >= JOIN(end_, __LINE__); JOIN(iter_, __LINE__) -= c) #define down3(i, b, a) down4(i, b, a, 1) #define down(...) GET4(__VA_ARGS__, down4, down3, NO_IMPL)(__VA_ARGS__) #define rep(n) \ for (auto JOIN(_iter_, __LINE__) = n; JOIN(_iter_, __LINE__) > 0; \ --JOIN(_iter_, __LINE__)) #define FOR_LAST_OPERATION_IMPL(arg1, arg2) arg1] : arg2 #define FOR_NORMAL_OPERATION_IMPL(arg1, arg2) arg1, #define FOR_IMPL0(arg1, arg2, peek, ...) \ CONDITIONAL(peek, FOR_LAST_OPERATION_IMPL, FOR_NORMAL_OPERATION_IMPL) \ (arg1, arg2) TERMINATE_OR(peek, FOR_IMPL1) PAUSE(arg2, peek, __VA_ARGS__) #define FOR_IMPL1(arg1, arg2, peek, ...) \ CONDITIONAL(peek, FOR_LAST_OPERATION_IMPL, FOR_NORMAL_OPERATION_IMPL) \ (arg1, arg2) TERMINATE_OR(peek, FOR_IMPL0) PAUSE(arg2, peek, __VA_ARGS__) #define FOR_IMPL3(arg1, arg2, peek, ...) \ CONDITIONAL(peek, for (auto && arg1 : arg2), \ for (auto && [EXPAND_13(FOR_IMPL0(arg1, arg2, peek, __VA_ARGS__)))) #define FOR(...) FOR_IMPL3(__VA_ARGS__, ()) #define ALL(x) std::begin(x), std::end(x) // hat off to 300iq #define RALL(x) std::rbegin(x), std::rend(x) #define pb push_back #define eb emplace_back #define MP make_pair #define ep emplace #define SZ(x) (int) (x).size() #define rp(...) return pl(__VA_ARGS__) #define rpn(...) return pn(__VA_ARGS__) #define adv(i, n) \ for (auto JOIN(_n_, __LINE__) = n; i < JOIN(_n_, __LINE__); ++i) #define radv(i, n) \ for (auto JOIN(_n_, __LINE__) = n; i > JOIN(_n_, __LINE__); --i) #define INT(...) \ int __VA_ARGS__; \ io::scan(__VA_ARGS__) #define LL(...) \ long long __VA_ARGS__; \ io::scan(__VA_ARGS__) #define STR(...) \ std::string __VA_ARGS__; \ io::scan(__VA_ARGS__) #define CHAR(...) \ char __VA_ARGS__; \ io::scan(__VA_ARGS__) #define NL \ [] { \ std::cout << '\n'; \ }() #define RI \ ([] { \ int x; \ std::cin >> x; \ return x; \ })() #define READ_VI(NAME, LEN) \ std::vector NAME(LEN); \ io::scan(NAME); #define VI(...) FOR_EACH_2(READ_VI, __VA_ARGS__) #define READ_VII(NAME, LEN) \ std::vector> NAME(LEN); \ io::scan(NAME); #define VII(...) FOR_EACH_2(READ_VII, __VA_ARGS__) #define READ_VL(NAME, LEN) \ std::vector NAME(LEN); \ io::scan(NAME); #define VL(...) FOR_EACH_2(READ_VL, __VA_ARGS__) #define READ_VS(NAME, LEN) \ std::vector NAME(LEN); \ io::scan(NAME); #define VS(...) FOR_EACH_2(READ_VS, __VA_ARGS__) #endif// JHELPER_EXAMPLE_PROJECT_LIBRARY_ALIAS_HPP_ #ifndef CP_UTILS #define CP_UTILS #include #include #include #include #include #include #include #include #include #include #include inline void Yn(bool p) { std::cout << (p ? "Yes\n" : "No\n"); } inline void YN(bool p) { std::cout << (p ? "YES\n" : "NO\n"); } inline void yn(bool p) { std::cout << (p ? "yes\n" : "no\n"); } template Container inc(Container &&c) { for (auto &e : c) ++e; return std::forward(c); } template Container dec(Container &&c) { for (auto &e : c) --e; return std::forward(c); } template bool chkmin(A& a, const B& b) { if (b < a) { a = b; return true; } return false; } template bool chkmax(A& a, const B& b) { if (a < b) { a = b; return true; } return false; } #if __cplusplus >= 201703L template bool chkmin(A& a, const B& b, const C&... c) { if (B res = std::min({b, c...}); res < a) { a = res; return true; } return false; } template bool chkmax(A& a, const B& b, const C&... c) { if (B res = std::max({b, c...}); res > a) { a = res; return true; } return false; } #endif template void append(T &container1, const U &container2) { container1.insert(container1.end(), container2.begin(), container2.end()); } template int argmin(const std::vector &a) { return (int) (std::min_element(a.begin(), a.end()) - a.begin()); } template int argmax(const std::vector &a) { return (int) (std::max_element(a.begin(), a.end()) - a.begin()); } template Container reverse(Container &&c) { std::reverse(std::begin(c), std::end(c)); return std::forward(c); } template Sequence rev_copy(Sequence a) { std::reverse(std::begin(a), std::end(a)); return a; } template Sequence uniq(Sequence &&s) { std::sort(std::begin(s), std::end(s)); s.erase(std::unique(std::begin(s), std::end(s)), std::end(s)); return std::forward(s); } template auto max(const Container &c) { assert(c.size() > 0); return *std::max_element(std::begin(c), std::end(c)); } template auto min(const Container &c) { assert(c.size() > 0); return *std::min_element(std::begin(c), std::end(c)); } template int maxi(const Array &a) { assert(a.size() > 0); return int(std::max_element(std::begin(a), std::end(a)) - std::begin(a)); } template int mini(const Array &a) { assert(a.size() > 0); return int(std::min_element(std::begin(a), std::end(a)) - std::begin(a)); } template auto lb(const Array &a, Value v) { return std::lower_bound(std::begin(a), std::end(a), v); } template auto ub(const Array &a, Value v) { return std::upper_bound(std::begin(a), std::end(a), v); } template auto lb(const Array &a, Value v, Compare compare) { return std::lower_bound(std::begin(a), std::end(a), v, compare); } template auto ub(const Array &a, Value v, Compare compare) { return std::upper_bound(std::begin(a), std::end(a), v, compare); } template int lbi(const Array &a, Value v) { return int(lb(a, v) - std::begin(a)); } template int lbi(Iter beg, int count, Value v) { assert(count > 0); return int(std::lower_bound(beg, beg + count, v) - beg); } template int ubi(Iter beg, int count, Value v) { assert(count > 0); return int(std::upper_bound(beg, beg + count, v) - beg); } template int ubi(const Array &a, Value v) { return int(ub(a, v) - std::begin(a)); } template Container iota(Container &&c, value_type_of v) { std::iota(std::begin(c), std::end(c), v); return std::forward(c); } template std::vector argsort(const std::vector &array, Comp comp) { std::vector res(array.size()); std::iota(res.begin(), res.end(), 0); std::stable_sort(res.begin(), res.end(), [&array, comp](int i, int j) { return comp(array[i], array[j]); }); return res; } template std::vector argsort(const std::vector& array) { std::vector res(array.size()); std::iota(res.begin(), res.end(), 0); std::stable_sort(res.begin(), res.end(), [&array](int i, int j) { return array[i] < array[j]; }); return res; } template std::vector order(const std::vector& array) { int n = (int) array.size(); std::vector I(n); std::iota(I.begin(), I.end(), 0); std::sort(I.begin(), I.end(), [&array](int i, int j) { return array[i] < array[j]; }); std::vector order(n); for (int i = 1; i < n; i++) order[I[i]] = order[I[i - 1]] + (array[I[i - 1]] < array[I[i]]); return order; } #if __cplusplus >=201703L template Container sort(Container &&c, Compare comp = nullptr) { if constexpr (std::is_same_v) std::sort(std::begin(c), std::end(c)); else std::sort(std::begin(c), std::end(c), comp); return std::forward(c); } #endif template struct reversion_wrapper { T &iterable; }; template auto begin(reversion_wrapper w) { using std::rbegin; return rbegin(w.iterable); } template auto end(reversion_wrapper w) { using std::rend; return rend(w.iterable); } template reversion_wrapper rev_view(T &&iterable) { return {std::forward(iterable)}; } /// @return nearest integer not less than the quotient x/y. template T qceil(T x, U y) { assert(y > 0); T q = x / y; return q + (q * y < x); } /// @return nearest integer not greater than the quotient x/y. template T qfloor(T x, U y) { assert(y > 0); T q = x / y; return q - (q * y > x); } template std::pair divmod(T x, U y) { assert(y > 0); T q = qfloor(x, y); return {q, x - q * y}; }; /// @return nearest multiple of y not less than x. template T mceil(T x, U y) { assert(y > 0); return qceil(x, y) * y; } /// @return nearest multiple of y not greater than x. template T mfloor(T x, U y) { assert(y > 0); return qfloor(x, y) * y; } #if __cplusplus >= 201703L template struct typelist {}; template constexpr bool any_same = (std::is_same::value || ...); template struct y_combinator { template struct ref { y_combinator &self; template decltype(auto) operator()(Args &&...args) const { using G = std::conditional_t, TLs...>, ref, ref>>; return self.f(G{self}, std::forward(args)...); } }; F f; template decltype(auto) operator()(Args &&...args) { return ref<>{*this}(std::forward(args)...); } }; template y_combinator(F) -> y_combinator; #endif template constexpr T INF = std::numeric_limits::max() / 2; /// @brief Usage: acc\(array) template T acc(const U& array) { return std::accumulate(std::begin(array), std::end(array), T(0)); } template T acc(const std::vector& array) { return std::accumulate(array.begin(), array.end(), T(0)); } // maps values of a into 0, 1, 2, ..., preserving order. template std::vector normalize(const std::vector &a) { assert(not a.empty()); int n = (int) a.size(); std::vector I = argsort(a); std::vector b(a.size()); b[I[0]] = 0; for (int i = 1; i < n; i++) b[I[i]] = b[I[i - 1]] + (a[I[i - 1]] < a[I[i]]); return b; } template Int binary_search(F check, Int ok, Int ng, bool check_ok = true) { if (check_ok) assert(check(ok)); while (std::abs(ok - ng) > 1) { Int x = ng + (ok - ng) / 2; (check(x) ? ok : ng) = x; } return ok; } template int bit(T a, Int i) { return a >> i & 1; } #define popcnt(x) __builtin_popcountll((x)) // sign used in principle of inclusion-exclusion int pie_sign(int s) { assert(s >= 0); return popcnt(s) & 1 ? -1 : 1; } #endif// CP_UTILS // // Created by zjs on 12/19/23. // #ifndef JHELPER_EXAMPLE_PROJECT_LIBRARY_LEVEL_ANCESTOR_HPP_ #define JHELPER_EXAMPLE_PROJECT_LIBRARY_LEVEL_ANCESTOR_HPP_ class level_ancestor { int n; int log; std::vector> a; public: explicit level_ancestor(const std::vector &parent) : n((int) parent.size()) { log = 0; int t = 1; while (t < n) { log++; t *= 2; } a.assign(log, std::vector(n, -1)); if (log) { a[0] = parent; for (int i = 1; i < log; i++) for (int j = 0; j < n; j++) if (a[i - 1][j] != -1) a[i][j] = a[i - 1][a[i - 1][j]]; } } int ancestor(int u, int k) const { if (k >= n) return -1; int v = u; for (int i = 0; i < log; i++) if (k >> i & 1) { v = a[i][v]; if (v == -1) break; } return v; } }; #endif// JHELPER_EXAMPLE_PROJECT_LIBRARY_LEVEL_ANCESTOR_HPP_ using namespace io; using namespace std; struct lazy_fenwick { int n; pii e = {0, -1}; unordered_map a; void update(int i, pii x) { while (i <= n) { if (a.count(i)) a[i] = max(a[i], x); else a[i] = x; i += i & -i; } } pii sum(int i) { pii res = e; while (i) { auto it = a.find(i); if (it != a.end()) res = max(res, it->second); i -= i & -i; } return res; } // n is the upper bound of keys lazy_fenwick(int n) : n(n) {} }; void solve() { // 树形结构 INT(n, k); vii a(n); scan(a); sort(a); debug(a); vi pa(n); lazy_fenwick b(1e9); rng (i, n) { auto [l, r] = a[i]; auto res = b.sum(l); pa[i] = res.second; b.update(r, {l, i}); } debug(pa); level_ancestor la(pa); int ans = INT_MAX; rng (i, n) { int j = la.ancestor(i, k - 1); debug(j); if (j != -1) { chkmin(ans, a[i].second - a[j].first); } } if (ans == INT_MAX) ans = -1; pl(ans); } #include int main() { #if defined FILE_IO and not defined LOCAL freopen(FILE_IO ".in", "r", stdin); freopen(FILE_IO ".out", "w", stdout); #endif solve(); return 0; }