#include<bits/stdc++.h>
#pragma GCC optimize ("Ofast")
using namespace std;
#define rep(i,a,b) for(ll i = a; i<b;i++)
#define rrep(i,a,b) for(ll i = b-1; i>=a;i--)
#define trav(x,a) for(auto &x: a)
#define all(v) v.begin(),v.end()
#define sz(v) ll(v.size())
typedef long long ll;
typedef vector<ll> vl;
typedef pair<ll,ll> pll;
int main(){
cin.tie(0);
ios_base::sync_with_stdio(0);
ll n,m; cin>>n>>m;
vl s(n);
vl e(n);
rep(i,0,n) cin>>s[i]>>e[i];
vector<tuple<ll,ll,ll>> ev;
rep(i,0,n){
if(e[i]<s[i]){
e[i]+=m;
}
ev.emplace_back(s[i],0,i);
ev.emplace_back(e[i],1,i);
ev.emplace_back(s[i]+m,0,i);
ev.emplace_back(e[i]+m,1,i);
ev.emplace_back(s[i]+m*2,0,i);
ev.emplace_back(e[i]+m*2,1,i);
}
map<ll,ll> nextInterval;
sort(all(ev));
reverse(all(ev));
set<pll> best;
for(auto x: ev){
auto [t,b,idx] = x;
if(b==1){
if(sz(best)){
nextInterval[t] = max(nextInterval[t], prev(best.end())->first);
}
best.emplace(t,idx);
} else {
best.erase(make_pair(t+e[idx]-s[idx],idx));
}
}
if(sz(nextInterval)==0){
cout<<-1<<"\n";
return 0;
}
// for(auto x: nextInterval){
// cout<<x.first<<": "<<x.second<<endl;
// }
deque<ll> q;
q.push_front(nextInterval.begin()->first);
ll topScore = 1e18;
rep(i,0,10*n){
// cout<<"q:"<<endl;
// for(auto x:q){
// cout<<x<<" ";
// }
// cout<<endl;
if(q.front()-q.back()>=m){
topScore = min(topScore, sz(q)-1);
q.pop_back();
} else {
if(nextInterval.find(q.front()) == nextInterval.end()){
if(nextInterval.lower_bound(q.front())==nextInterval.end()) break;
else {
ll t = nextInterval.lower_bound(q.front())->first;
q.clear();
q.push_front(t);
}
}
q.push_front(nextInterval[q.front()]);
}
}
if(topScore==1e18){
cout<<-1<<"\n";
} else
cout<<topScore<<"\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
344 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
1 ms |
348 KB |
Output is correct |
35 |
Correct |
1 ms |
348 KB |
Output is correct |
36 |
Correct |
1 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
344 KB |
Output is correct |
38 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
344 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
1 ms |
348 KB |
Output is correct |
35 |
Correct |
1 ms |
348 KB |
Output is correct |
36 |
Correct |
1 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
344 KB |
Output is correct |
38 |
Correct |
1 ms |
348 KB |
Output is correct |
39 |
Correct |
8 ms |
2260 KB |
Output is correct |
40 |
Correct |
7 ms |
2260 KB |
Output is correct |
41 |
Correct |
13 ms |
2504 KB |
Output is correct |
42 |
Correct |
8 ms |
2260 KB |
Output is correct |
43 |
Correct |
14 ms |
2516 KB |
Output is correct |
44 |
Correct |
12 ms |
2260 KB |
Output is correct |
45 |
Correct |
8 ms |
2260 KB |
Output is correct |
46 |
Correct |
8 ms |
2260 KB |
Output is correct |
47 |
Correct |
6 ms |
2260 KB |
Output is correct |
48 |
Correct |
6 ms |
2260 KB |
Output is correct |
49 |
Correct |
6 ms |
2196 KB |
Output is correct |
50 |
Correct |
7 ms |
2260 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
456 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
456 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
8 ms |
2516 KB |
Output is correct |
16 |
Correct |
10 ms |
2208 KB |
Output is correct |
17 |
Correct |
9 ms |
2516 KB |
Output is correct |
18 |
Correct |
8 ms |
2128 KB |
Output is correct |
19 |
Correct |
6 ms |
2260 KB |
Output is correct |
20 |
Correct |
310 ms |
53672 KB |
Output is correct |
21 |
Correct |
394 ms |
89428 KB |
Output is correct |
22 |
Correct |
367 ms |
89528 KB |
Output is correct |
23 |
Correct |
455 ms |
73704 KB |
Output is correct |
24 |
Correct |
313 ms |
73772 KB |
Output is correct |
25 |
Correct |
591 ms |
83284 KB |
Output is correct |
26 |
Correct |
406 ms |
87380 KB |
Output is correct |
27 |
Correct |
454 ms |
74784 KB |
Output is correct |
28 |
Correct |
394 ms |
89172 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
360 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
7 ms |
2684 KB |
Output is correct |
13 |
Correct |
8 ms |
2260 KB |
Output is correct |
14 |
Correct |
7 ms |
2004 KB |
Output is correct |
15 |
Correct |
313 ms |
53632 KB |
Output is correct |
16 |
Correct |
444 ms |
73812 KB |
Output is correct |
17 |
Correct |
287 ms |
73352 KB |
Output is correct |
18 |
Correct |
296 ms |
74324 KB |
Output is correct |
19 |
Correct |
355 ms |
74236 KB |
Output is correct |
20 |
Correct |
340 ms |
72276 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
344 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
1 ms |
348 KB |
Output is correct |
35 |
Correct |
1 ms |
348 KB |
Output is correct |
36 |
Correct |
1 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
344 KB |
Output is correct |
38 |
Correct |
1 ms |
348 KB |
Output is correct |
39 |
Correct |
8 ms |
2260 KB |
Output is correct |
40 |
Correct |
7 ms |
2260 KB |
Output is correct |
41 |
Correct |
13 ms |
2504 KB |
Output is correct |
42 |
Correct |
8 ms |
2260 KB |
Output is correct |
43 |
Correct |
14 ms |
2516 KB |
Output is correct |
44 |
Correct |
12 ms |
2260 KB |
Output is correct |
45 |
Correct |
8 ms |
2260 KB |
Output is correct |
46 |
Correct |
8 ms |
2260 KB |
Output is correct |
47 |
Correct |
6 ms |
2260 KB |
Output is correct |
48 |
Correct |
6 ms |
2260 KB |
Output is correct |
49 |
Correct |
6 ms |
2196 KB |
Output is correct |
50 |
Correct |
7 ms |
2260 KB |
Output is correct |
51 |
Correct |
0 ms |
348 KB |
Output is correct |
52 |
Correct |
0 ms |
348 KB |
Output is correct |
53 |
Correct |
0 ms |
456 KB |
Output is correct |
54 |
Correct |
1 ms |
348 KB |
Output is correct |
55 |
Correct |
0 ms |
348 KB |
Output is correct |
56 |
Correct |
0 ms |
348 KB |
Output is correct |
57 |
Correct |
0 ms |
348 KB |
Output is correct |
58 |
Correct |
0 ms |
348 KB |
Output is correct |
59 |
Correct |
1 ms |
456 KB |
Output is correct |
60 |
Correct |
1 ms |
348 KB |
Output is correct |
61 |
Correct |
1 ms |
348 KB |
Output is correct |
62 |
Correct |
1 ms |
344 KB |
Output is correct |
63 |
Correct |
1 ms |
348 KB |
Output is correct |
64 |
Correct |
1 ms |
348 KB |
Output is correct |
65 |
Correct |
8 ms |
2516 KB |
Output is correct |
66 |
Correct |
10 ms |
2208 KB |
Output is correct |
67 |
Correct |
9 ms |
2516 KB |
Output is correct |
68 |
Correct |
8 ms |
2128 KB |
Output is correct |
69 |
Correct |
6 ms |
2260 KB |
Output is correct |
70 |
Correct |
310 ms |
53672 KB |
Output is correct |
71 |
Correct |
394 ms |
89428 KB |
Output is correct |
72 |
Correct |
367 ms |
89528 KB |
Output is correct |
73 |
Correct |
455 ms |
73704 KB |
Output is correct |
74 |
Correct |
313 ms |
73772 KB |
Output is correct |
75 |
Correct |
591 ms |
83284 KB |
Output is correct |
76 |
Correct |
406 ms |
87380 KB |
Output is correct |
77 |
Correct |
454 ms |
74784 KB |
Output is correct |
78 |
Correct |
394 ms |
89172 KB |
Output is correct |
79 |
Correct |
1 ms |
348 KB |
Output is correct |
80 |
Correct |
1 ms |
348 KB |
Output is correct |
81 |
Correct |
0 ms |
360 KB |
Output is correct |
82 |
Correct |
0 ms |
348 KB |
Output is correct |
83 |
Correct |
0 ms |
348 KB |
Output is correct |
84 |
Correct |
0 ms |
348 KB |
Output is correct |
85 |
Correct |
0 ms |
348 KB |
Output is correct |
86 |
Correct |
1 ms |
348 KB |
Output is correct |
87 |
Correct |
1 ms |
348 KB |
Output is correct |
88 |
Correct |
1 ms |
604 KB |
Output is correct |
89 |
Correct |
0 ms |
344 KB |
Output is correct |
90 |
Correct |
7 ms |
2684 KB |
Output is correct |
91 |
Correct |
8 ms |
2260 KB |
Output is correct |
92 |
Correct |
7 ms |
2004 KB |
Output is correct |
93 |
Correct |
313 ms |
53632 KB |
Output is correct |
94 |
Correct |
444 ms |
73812 KB |
Output is correct |
95 |
Correct |
287 ms |
73352 KB |
Output is correct |
96 |
Correct |
296 ms |
74324 KB |
Output is correct |
97 |
Correct |
355 ms |
74236 KB |
Output is correct |
98 |
Correct |
340 ms |
72276 KB |
Output is correct |
99 |
Correct |
305 ms |
73216 KB |
Output is correct |
100 |
Correct |
325 ms |
73044 KB |
Output is correct |
101 |
Correct |
390 ms |
74628 KB |
Output is correct |
102 |
Correct |
328 ms |
74240 KB |
Output is correct |
103 |
Correct |
435 ms |
74836 KB |
Output is correct |
104 |
Correct |
315 ms |
73152 KB |
Output is correct |
105 |
Correct |
323 ms |
72792 KB |
Output is correct |