結果

問題 No.2543 Many Meetings
ユーザー 👑 kmjpkmjp
提出日時 2023-11-24 23:08:13
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 182 ms / 2,000 ms
コード長 2,204 bytes
コンパイル時間 2,565 ms
コンパイル使用メモリ 214,004 KB
実行使用メモリ 37,144 KB
最終ジャッジ日時 2023-11-24 23:08:22
合計ジャッジ時間 7,946 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 13 ms
34,016 KB
testcase_01 AC 12 ms
34,016 KB
testcase_02 AC 12 ms
34,020 KB
testcase_03 AC 169 ms
37,144 KB
testcase_04 AC 167 ms
37,144 KB
testcase_05 AC 171 ms
37,144 KB
testcase_06 AC 166 ms
37,144 KB
testcase_07 AC 165 ms
37,144 KB
testcase_08 AC 176 ms
37,144 KB
testcase_09 AC 175 ms
37,144 KB
testcase_10 AC 175 ms
37,144 KB
testcase_11 AC 182 ms
37,144 KB
testcase_12 AC 177 ms
37,144 KB
testcase_13 AC 133 ms
36,512 KB
testcase_14 AC 81 ms
35,452 KB
testcase_15 AC 79 ms
35,432 KB
testcase_16 AC 110 ms
36,068 KB
testcase_17 AC 124 ms
36,420 KB
testcase_18 AC 137 ms
36,768 KB
testcase_19 AC 127 ms
36,464 KB
testcase_20 AC 137 ms
36,476 KB
testcase_21 AC 149 ms
36,864 KB
testcase_22 AC 126 ms
36,424 KB
testcase_23 AC 11 ms
34,040 KB
testcase_24 AC 11 ms
34,024 KB
testcase_25 AC 11 ms
34,028 KB
testcase_26 AC 11 ms
34,028 KB
testcase_27 AC 12 ms
34,040 KB
testcase_28 AC 125 ms
36,384 KB
testcase_29 AC 47 ms
34,856 KB
testcase_30 AC 48 ms
34,868 KB
testcase_31 AC 43 ms
34,764 KB
testcase_32 AC 116 ms
36,264 KB
testcase_33 AC 11 ms
34,016 KB
testcase_34 AC 12 ms
34,016 KB
testcase_35 AC 12 ms
34,024 KB
testcase_36 AC 11 ms
34,020 KB
testcase_37 AC 12 ms
34,024 KB
testcase_38 AC 12 ms
34,020 KB
testcase_39 AC 12 ms
34,024 KB
testcase_40 AC 10 ms
34,020 KB
testcase_41 AC 12 ms
34,016 KB
testcase_42 AC 11 ms
34,020 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define FORR2(x,y,arr) for(auto& [x,y]:arr)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
template<class T> bool chmax(T &a, const T &b) { if(a<b){a=b;return 1;}return 0;}
template<class T> bool chmin(T &a, const T &b) { if(a>b){a=b;return 1;}return 0;}
//-------------------------------------------------------

int N,K;
vector<pair<int,int>> V;

template<class V,int NV> class SegTree_Pair {
public:
	vector<pair<V,int> > val;
	static V const def=1<<30;
	pair<V,int> comp(pair<V,int> l,pair<V,int> r){ return min(l,r);}
	SegTree_Pair(){
		val.resize(NV*2);
		int i;
		FOR(i,NV) val[i+NV]=make_pair(def,i);
		for(i=NV-1;i>=1;i--) val[i]=comp(val[2*i],val[2*i+1]);
	};
	pair<V,int> getval(int x,int y,int l=0,int r=NV,int k=1) {
		if(r<=x || y<=l) return make_pair(def,NV);
		if(x<=l && r<=y) return val[k];
		return comp(getval(x,y,l,(l+r)/2,k*2),getval(x,y,(l+r)/2,r,k*2+1));
	}
	void update(int entry, V v) {
		entry += NV;
		val[entry]=make_pair(v,entry-NV);
		while(entry>1) entry>>=1, val[entry]=comp(val[entry*2],val[entry*2+1]);
	}
};
SegTree_Pair<int,1<<20> st;

int nex[20][202020];

void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>N>>K;
	FOR(i,N) {
		cin>>x>>y;
		V.push_back({x,y});
	}
	sort(ALL(V));
	FOR(i,N) {
		st.update(i,V[i].second);
	}
	FOR(i,N) {
		auto x=lower_bound(ALL(V),make_pair(V[i].second,0))-V.begin();
		if(x==N) nex[0][i]=N;
		else nex[0][i]=min(N,st.getval(x,N).second);
	}
	FOR(i,20) nex[i][N]=N;
	FOR(i,19) {
		FOR(j,N) nex[i+1][j]=nex[i][nex[i][j]];
	}
	int mi=1<<30;
	FOR(i,N) {
		int cur=i;
		int step=K-1;
		FOR(j,19) {
			if(step&(1<<j)) cur=nex[j][cur];
		}
		
		if(cur!=N) {
			mi=min(mi,V[cur].second-V[i].first);
		}
	}
	
	if(mi==(1<<30)) mi=-1;
	cout<<mi<<endl;
	
}


int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}
0