【悬3关】【我是唐氏】WATLE 55 pts 求条
查看原帖
【悬3关】【我是唐氏】WATLE 55 pts 求条
1309682
majoego楼主2024/7/28 20:07
// Author: Expert_Dream
// Copyright (c) 2024 Expert_Dream All rights reserved.
// Problem: P1712 [NOI2016] 区间
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P1712
// Memory Limit: 250 MB
// Time Limit: 1000 ms
// Date: 2024-07-23 14:19:36

// #pragma GCC optimize(2,3,"Ofast","inline")
#include<bits/stdc++.h>
// #include <atcoder/all>
#define ll long long
#define i128 __int128
#define ull unsigned long long
#define pii pair<int,int>
#define pll pair<long long, long long>
#define fs first
#define sc second
#define x0 _x0_
#define y1 _y1_
#define endl '\n'
#define re register
#define pb push_back
#define vi vector<int>
#define pq priority_queue
#define mem(a,x) memset((a),(x),sizeof(a))
#define debug(x) cout<<"debug:"<<x<<endl;
#define sz(s) (int)(s.size())
#define all(a) a.begin(),a.end()
#define rd(l,r) (myrand()%(r-l+1)+l)
#define print(x) {cout<<x<<endl;return;}
#define For(x,y,z) for(int x = y;x <= z;x++)
#define For_(x,y,z) for(int x = y;x >= z;x--)
#define between(x, flo, top) (flo <= x && x <= top)
#define YES cout<<"YES\n"
#define Yes cout<<"Yes\n"
#define yes cout<<"yes\n"
#define NO cout<<"NO\n"
#define No cout<<"No\n"
#define no cout<<"no\n"
#define em(x) x.empty()
// #define double long double
using namespace std;
// using namespace atcoder;
mt19937 myrand(time(0));
template <typename Type> void get_min(Type &w1,const Type w2) { if(w2<w1) w1=w2; } 
template <typename Type> void get_max(Type &w1,const Type w2) { if(w2>w1) w1=w2; }
template <typename Type> Type up_div(Type w1,Type w2) { return (w1/w2+(w1%w2?1:0)); }//向上取整
template <typename Type> Type gcd(Type a,Type b) {long long i=__builtin_ctz(a),j=__builtin_ctz(b),x=i>j?j:i,y;b>>=j;while(a){a>>=i;y=b-a;i=__builtin_ctz(y);b=a<b?a:b;a=y<0?-y:y;}return b<<x;}
template <typename Type> Type lcm(Type a,Type b) {return a/gcd(a,b)*b;}
template <typename Type> Type ksm(Type a,Type b) {int res=1;while(b){if(b&1) res=res*a;a=a*a;b>>=1;}return res;}
template <typename Type> Type ksm(Type a,Type b,Type mod) {int res=1;while(b){if(b&1) res=(res*a)%mod;a=(a*a)%mod;b>>=1;}return res%mod;}
inline int Sqrt(int x){int l = 1, r = sqrtl(x) + 10; while (l < r) {int mid = l + r + 1 >> 1; if (mid * mid <= x) l = mid; else r = mid - 1;}return l;}
namespace FastIO {template <typename T = int>inline T read() {T s=0,w=1;char c=getchar();while(!isdigit(c)){if(c=='-')w=-1;c=getchar();}while(isdigit(c))s=(s<<1)+(s<<3)+(c^48),c=getchar();return s*w;}template <typename T>inline void read(T &s){s=0;int w=1;char c=getchar();while(!isdigit(c)){if(c=='-')w=-1;c=getchar();}while(isdigit(c))s=(s<<1)+(s<<3)+(c^48),c=getchar();s*=w;}template <typename T, typename... Args> inline void read(T &x, Args &...args) {read(x), read(args...);}template <typename T>inline void write(T x,char ch = '\n') {if(x<0)x=-x,putchar('-');static char stk[200];int top=0;do{stk[top++]=x%10+'0',x/=10;}while(x);while(top)putchar(stk[--top]);putchar(ch);return;}template <typename T, typename... Args> inline void write(T x, Args ...args) {write(x,' '), write(args...);}template <typename Type> void Write(vector<Type> v) {for(auto it:v)write(it,' ');puts("");}}
using namespace FastIO;
#define int long long
const int mod = 998244353,inf = 0x3f3f3f3f,N =2e6+5,M = 1e6+5,K = 3000+5;// 1000000007
const long long linf = 0x3f3f3f3f3f3f3f3f;
int n,m;
struct data{
	int len,l,r;
};
vector<struct data> v; 
// SegmentTree_Sum
namespace seg{
	struct node{int l,r,sum,lazy,mx;}t[N<<2];
	void build (int p,int l,int r){//建树 
		t[p].l = l,t[p].r = r;
		if(l == r) {return ;}	
		int mid = (l+r) >> 1;
		build(p<<1,l,mid),build(p<<1|1,mid + 1,r);
	}void Add(int p,int x){
		t[p].lazy += x,t[p].sum += x ;
	}void pushdown(int p){
		if(t[p].lazy != 0 && t[p].l != t[p].r)
			Add(p<<1,t[p].lazy),Add(p<<1|1,t[p].lazy),t[p].lazy = 0;
	}void update(int p,int l,int r,int x){//区间修改
		pushdown(p); if(l <= t[p].l && t[p].r <= r){Add(p,x); return ;}
		int mid = (t[p].r + t[p].l) >> 1;
		if(l <= mid) update(p<<1,l,r,x); 
		if(r > mid) update(p<<1|1,l,r,x);
		t[p].sum  = max(t[p<<1].sum , t[p<<1|1].sum) ;
	}int query(int p,int l,int r){//区间查询
		return t[p].sum;
	}
}using namespace seg;
map<int,int> mp;
int b[N];
pii a[N];
int ans=1e9;
void solve(){
	vector<int> u;
	cin >> n >> m;
	v.pb({0,0,0});
	build(1,1,n*2+1);
	For(i,1,n){
		int l,r;cin>>l>>r;
		v.pb({r-l,l,r});
		u.pb(l),u.pb(r);
		a[i]={l,r};
	}sort(all(v),[](struct data a,struct data b){return a.len < b.len;});
	sort(all(u));
	u.erase(unique(all(u)),u.end());
	For(i,1,n) {
		mp[a[i].fs]=lower_bound(all(u),a[i].fs)-u.begin()+1;
		mp[a[i].sc]=lower_bound(all(u),a[i].sc)-u.begin()+1;
	}
	int tot=0;
	int lst=1;
	For(i,1,n){
		update(1,mp[v[i].l],mp[v[i].r],1);
		while(lst<=i && query(1,1,n)>m){
			update(1,mp[v[lst].l],mp[v[lst].r],-1);
			lst++;
		}
		if(query(1,1,n) >= m){
			ans = min(ans,v[i].len-v[lst].len);
		}
	}
	if(ans==1e9) cout<<-1<<endl;
	else cout<<ans<<endl;
}signed main(){
	solve();
}
2024/7/28 20:07
加载中...