#include<bits/stdc++.h>
using namespace std;
#define pii pair<int, int>
const int MAX_S = 20;
int N, M;
vector<int> times;
vector<vector<int>> calc_bl(vector<int> optimal_next){
int n = optimal_next.size();
vector<vector<int>> bl(MAX_S, vector<int>(n));
for(int i = 0; i<n; i++){
bl[0][i] = optimal_next[i];
}
for(int step =1; step<MAX_S; step++){
for(int i = 0; i<n; i++){
bl[step][i] = bl[step-1][bl[step-1][i]];
}
}
return bl;
}
int get_nth(vector<vector<int>>& bl, int pos, int dist){
for(int i = MAX_S-1; i>=0; i--){
if((dist&(1LL<<i))!=0LL){
pos = bl[i][pos];
}
}
return pos;
}
signed main(){
cin>>N>>M;
vector<pii> inters;
for(int i = 0; i<N; i++){
pii inter;
cin>>inter.first>>inter.second;
if(inter.first>inter.second){
inters.push_back({inter.first, inter.second+M});
}
else{
inters.push_back({inter.first, inter.second});
inters.push_back({inter.first+M, inter.second+M});
}
}
for(auto inter: inters){
times.push_back(inter.first);
times.push_back(inter.second);
}
sort(times.begin(), times.end());
for(pii& e: inters){
if(e.first<e.second){
e.first = lower_bound(times.begin(), times.end(), e.first) - times.begin();
e.second = lower_bound(times.begin(), times.end(), e.second)- times.begin();
}
}
sort(inters.begin(), inters.end());
vector<pii> ev;
set<pii> cur_open;
for(int i = 0; i<inters.size(); i++){
auto e = inters[i];
ev.push_back({e.first, i});
}
vector<int> opt(times.size());
sort(ev.begin(), ev.end());
reverse(ev.begin(), ev.end());
for(int i = 0; i<times.size(); i++){
while(ev.size()>0 && ev.back().first==i){
auto cur_ev = ev.back();
ev.pop_back();
cur_open.insert(pii({inters[cur_ev.second].second, cur_ev.second}));
}
if(cur_open.size()>0){
opt[i] = (--cur_open.end())->first;
}
while(cur_open.size()>0 && cur_open.begin()->first == i){
cur_open.erase(cur_open.begin());
}
}
/*for(int i = 0; i<times.size(); i++){
cout<<times[i]<<" "<<times[opt[i]]<<endl;
}*/
auto bl = calc_bl(opt);
int res =1e6;
for(int i = 0; i<times.size(); i++){
int cur_dist = 1e6;
for(int step = (1<<20); step>=1; step/=2){
if(cur_dist-step>=0){
if((times[get_nth(bl, i, cur_dist-step)]-times[i])>=M){
cur_dist-=step;
}
}
}
res = min(res, cur_dist);
}
if(res==1e6){
cout<<-1<<endl;
}
else{
cout<<res<<endl;
}
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:70:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
70 | for(int i = 0; i<inters.size(); i++){
| ~^~~~~~~~~~~~~~
Main.cpp:78:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
78 | for(int i = 0; i<times.size(); i++){
| ~^~~~~~~~~~~~~
Main.cpp:101:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
101 | for(int i = 0; i<times.size(); i++){
| ~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
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 |
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 |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
432 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 |
396 KB |
Output is correct |
17 |
Correct |
1 ms |
348 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 |
0 ms |
348 KB |
Output is correct |
23 |
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 |
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 |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
432 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 |
396 KB |
Output is correct |
17 |
Correct |
1 ms |
348 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 |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
1 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 |
344 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 |
600 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
436 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
2 ms |
448 KB |
Output is correct |
35 |
Correct |
1 ms |
348 KB |
Output is correct |
36 |
Correct |
1 ms |
348 KB |
Output is correct |
37 |
Correct |
1 ms |
348 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 |
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 |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
432 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 |
396 KB |
Output is correct |
17 |
Correct |
1 ms |
348 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 |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
1 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 |
344 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 |
600 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
436 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
2 ms |
448 KB |
Output is correct |
35 |
Correct |
1 ms |
348 KB |
Output is correct |
36 |
Correct |
1 ms |
348 KB |
Output is correct |
37 |
Correct |
1 ms |
348 KB |
Output is correct |
38 |
Correct |
1 ms |
348 KB |
Output is correct |
39 |
Correct |
13 ms |
2396 KB |
Output is correct |
40 |
Correct |
14 ms |
2396 KB |
Output is correct |
41 |
Correct |
13 ms |
2364 KB |
Output is correct |
42 |
Correct |
12 ms |
2396 KB |
Output is correct |
43 |
Correct |
11 ms |
2396 KB |
Output is correct |
44 |
Correct |
13 ms |
2396 KB |
Output is correct |
45 |
Correct |
13 ms |
2392 KB |
Output is correct |
46 |
Correct |
13 ms |
2396 KB |
Output is correct |
47 |
Correct |
12 ms |
2440 KB |
Output is correct |
48 |
Correct |
13 ms |
2396 KB |
Output is correct |
49 |
Correct |
11 ms |
2396 KB |
Output is correct |
50 |
Correct |
13 ms |
2396 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 |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
344 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 |
436 KB |
Output is correct |
10 |
Correct |
1 ms |
576 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
13 ms |
2396 KB |
Output is correct |
16 |
Correct |
13 ms |
2396 KB |
Output is correct |
17 |
Correct |
12 ms |
2260 KB |
Output is correct |
18 |
Correct |
13 ms |
2368 KB |
Output is correct |
19 |
Correct |
14 ms |
2396 KB |
Output is correct |
20 |
Correct |
347 ms |
64436 KB |
Output is correct |
21 |
Correct |
658 ms |
85676 KB |
Output is correct |
22 |
Correct |
652 ms |
85848 KB |
Output is correct |
23 |
Correct |
601 ms |
84916 KB |
Output is correct |
24 |
Correct |
642 ms |
86004 KB |
Output is correct |
25 |
Correct |
643 ms |
80560 KB |
Output is correct |
26 |
Correct |
639 ms |
85680 KB |
Output is correct |
27 |
Correct |
621 ms |
85680 KB |
Output is correct |
28 |
Correct |
615 ms |
85676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
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 |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
13 ms |
2396 KB |
Output is correct |
13 |
Correct |
12 ms |
2396 KB |
Output is correct |
14 |
Correct |
12 ms |
2548 KB |
Output is correct |
15 |
Correct |
334 ms |
64432 KB |
Output is correct |
16 |
Correct |
599 ms |
84736 KB |
Output is correct |
17 |
Correct |
637 ms |
85676 KB |
Output is correct |
18 |
Correct |
604 ms |
82348 KB |
Output is correct |
19 |
Correct |
628 ms |
85672 KB |
Output is correct |
20 |
Correct |
613 ms |
84748 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 |
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 |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
432 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 |
396 KB |
Output is correct |
17 |
Correct |
1 ms |
348 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 |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
1 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 |
344 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 |
600 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
436 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
2 ms |
448 KB |
Output is correct |
35 |
Correct |
1 ms |
348 KB |
Output is correct |
36 |
Correct |
1 ms |
348 KB |
Output is correct |
37 |
Correct |
1 ms |
348 KB |
Output is correct |
38 |
Correct |
1 ms |
348 KB |
Output is correct |
39 |
Correct |
13 ms |
2396 KB |
Output is correct |
40 |
Correct |
14 ms |
2396 KB |
Output is correct |
41 |
Correct |
13 ms |
2364 KB |
Output is correct |
42 |
Correct |
12 ms |
2396 KB |
Output is correct |
43 |
Correct |
11 ms |
2396 KB |
Output is correct |
44 |
Correct |
13 ms |
2396 KB |
Output is correct |
45 |
Correct |
13 ms |
2392 KB |
Output is correct |
46 |
Correct |
13 ms |
2396 KB |
Output is correct |
47 |
Correct |
12 ms |
2440 KB |
Output is correct |
48 |
Correct |
13 ms |
2396 KB |
Output is correct |
49 |
Correct |
11 ms |
2396 KB |
Output is correct |
50 |
Correct |
13 ms |
2396 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 |
344 KB |
Output is correct |
54 |
Correct |
0 ms |
344 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 |
436 KB |
Output is correct |
60 |
Correct |
1 ms |
576 KB |
Output is correct |
61 |
Correct |
1 ms |
348 KB |
Output is correct |
62 |
Correct |
1 ms |
348 KB |
Output is correct |
63 |
Correct |
1 ms |
348 KB |
Output is correct |
64 |
Correct |
1 ms |
348 KB |
Output is correct |
65 |
Correct |
13 ms |
2396 KB |
Output is correct |
66 |
Correct |
13 ms |
2396 KB |
Output is correct |
67 |
Correct |
12 ms |
2260 KB |
Output is correct |
68 |
Correct |
13 ms |
2368 KB |
Output is correct |
69 |
Correct |
14 ms |
2396 KB |
Output is correct |
70 |
Correct |
347 ms |
64436 KB |
Output is correct |
71 |
Correct |
658 ms |
85676 KB |
Output is correct |
72 |
Correct |
652 ms |
85848 KB |
Output is correct |
73 |
Correct |
601 ms |
84916 KB |
Output is correct |
74 |
Correct |
642 ms |
86004 KB |
Output is correct |
75 |
Correct |
643 ms |
80560 KB |
Output is correct |
76 |
Correct |
639 ms |
85680 KB |
Output is correct |
77 |
Correct |
621 ms |
85680 KB |
Output is correct |
78 |
Correct |
615 ms |
85676 KB |
Output is correct |
79 |
Correct |
1 ms |
344 KB |
Output is correct |
80 |
Correct |
1 ms |
348 KB |
Output is correct |
81 |
Correct |
0 ms |
348 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 |
344 KB |
Output is correct |
88 |
Correct |
1 ms |
348 KB |
Output is correct |
89 |
Correct |
1 ms |
344 KB |
Output is correct |
90 |
Correct |
13 ms |
2396 KB |
Output is correct |
91 |
Correct |
12 ms |
2396 KB |
Output is correct |
92 |
Correct |
12 ms |
2548 KB |
Output is correct |
93 |
Correct |
334 ms |
64432 KB |
Output is correct |
94 |
Correct |
599 ms |
84736 KB |
Output is correct |
95 |
Correct |
637 ms |
85676 KB |
Output is correct |
96 |
Correct |
604 ms |
82348 KB |
Output is correct |
97 |
Correct |
628 ms |
85672 KB |
Output is correct |
98 |
Correct |
613 ms |
84748 KB |
Output is correct |
99 |
Correct |
668 ms |
85724 KB |
Output is correct |
100 |
Correct |
671 ms |
85932 KB |
Output is correct |
101 |
Correct |
627 ms |
85680 KB |
Output is correct |
102 |
Correct |
648 ms |
85896 KB |
Output is correct |
103 |
Correct |
618 ms |
85892 KB |
Output is correct |
104 |
Correct |
630 ms |
85680 KB |
Output is correct |
105 |
Correct |
515 ms |
84652 KB |
Output is correct |