#include <bits/stdc++.h>
using namespace std;
int N;
int T;
int reach[400100][30];
deque<int> dq;
int s,e;
vector<pair<int,int>> process;
vector<int> de;
int ans[400100];
int temp[400100];
int big(int j, int o, int n){
return (max( (o - j + T) % T, (n - j + T) % T) + j) % T;
}
int main(){
scanf(" %d",&N);
scanf(" %d",&T);
de.clear();
for(int i = 0; i < N; i++){
scanf(" %d",&s);
scanf(" %d",&e);
process.push_back({s,e});
de.push_back(s);
de.push_back(e);
}
sort(de.begin(),de.end());
de.resize(unique(de.begin(),de.end() ) - de.begin() );
for(int i = 0; i < de.size(); i++){
reach[i][0] = i;
}
T = de.size();
for(int i = 0; i < N; i++){
process[i].first = lower_bound(de.begin(), de.end(), process[i].first) - de.begin();
process[i].second = lower_bound(de.begin(), de.end(), process[i].second) - de.begin();
int j = process[i].first;
int p = process[i].second;
reach[j][0] = (max( (reach[j][0] - j + T) % T, (p - j + T) % T) + j) % T;
if(reach[(j - 1 + T) % T][0] != (j - 1 + T) % T)reach[j][0] = big(j,reach[j][0],reach[(j - 1 + T) % T ][0]);
}
for(int j = 0; j < T; j++){
if(reach[(j - 1 + T) % T][0] != (j - 1 + T) % T) reach[j][0] = big(j,reach[j][0],reach[(j - 1 + T) % T ][0]);
}
int cont = 1;
while(true){
if(cont == 29) break;
bool die = false;
for(int i = 0; i < T; i++){
reach[i][cont] = reach[reach[i][cont - 1]][cont - 1];
if(big(i,reach[i][cont], reach[i][cont - 1]) == reach[i][cont - 1] && reach[i][cont] != reach[i][cont - 1] ){
die = true;
break;
}
}
if(die) break;
cont++;
}
if(cont == 29){
printf("-1");
return 0;
}
int num = (1<<(cont - 1));
for(int i = 0; i < T; i++){
ans[i] = reach[i][cont - 1];
}
for(int k = cont - 1; k >= 0; k--){
bool die = false;
for(int i = 0; i < T; i++){
temp[i] = reach[ans[i]][k];
if(big(i, temp[i], ans[i]) == ans[i] && temp[i] != ans[i]){
die = true;
break;
}
}
if(die) continue;
num += (1<<k);
for(int i = 0; i < T; i++){
ans[i] = temp[i];
}
}
num++;
printf("%d",num);
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:38:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
38 | for(int i = 0; i < de.size(); i++){
| ~~^~~~~~~~~~~
Main.cpp:21:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
21 | scanf(" %d",&N);
| ~~~~~^~~~~~~~~~
Main.cpp:22:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
22 | scanf(" %d",&T);
| ~~~~~^~~~~~~~~~
Main.cpp:27:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
27 | scanf(" %d",&s);
| ~~~~~^~~~~~~~~~
Main.cpp:28:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
28 | scanf(" %d",&e);
| ~~~~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4432 KB |
Output is correct |
2 |
Correct |
1 ms |
2384 KB |
Output is correct |
3 |
Correct |
1 ms |
4432 KB |
Output is correct |
4 |
Correct |
1 ms |
4432 KB |
Output is correct |
5 |
Correct |
1 ms |
4548 KB |
Output is correct |
6 |
Correct |
1 ms |
4432 KB |
Output is correct |
7 |
Correct |
1 ms |
4432 KB |
Output is correct |
8 |
Correct |
1 ms |
2444 KB |
Output is correct |
9 |
Correct |
1 ms |
4432 KB |
Output is correct |
10 |
Correct |
1 ms |
2384 KB |
Output is correct |
11 |
Correct |
1 ms |
4432 KB |
Output is correct |
12 |
Correct |
1 ms |
4432 KB |
Output is correct |
13 |
Correct |
1 ms |
4432 KB |
Output is correct |
14 |
Correct |
2 ms |
4688 KB |
Output is correct |
15 |
Correct |
1 ms |
4432 KB |
Output is correct |
16 |
Correct |
1 ms |
4432 KB |
Output is correct |
17 |
Correct |
1 ms |
2384 KB |
Output is correct |
18 |
Correct |
1 ms |
2384 KB |
Output is correct |
19 |
Correct |
1 ms |
4432 KB |
Output is correct |
20 |
Correct |
1 ms |
4432 KB |
Output is correct |
21 |
Correct |
2 ms |
4432 KB |
Output is correct |
22 |
Correct |
2 ms |
4432 KB |
Output is correct |
23 |
Correct |
1 ms |
4432 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4432 KB |
Output is correct |
2 |
Correct |
1 ms |
2384 KB |
Output is correct |
3 |
Correct |
1 ms |
4432 KB |
Output is correct |
4 |
Correct |
1 ms |
4432 KB |
Output is correct |
5 |
Correct |
1 ms |
4548 KB |
Output is correct |
6 |
Correct |
1 ms |
4432 KB |
Output is correct |
7 |
Correct |
1 ms |
4432 KB |
Output is correct |
8 |
Correct |
1 ms |
2444 KB |
Output is correct |
9 |
Correct |
1 ms |
4432 KB |
Output is correct |
10 |
Correct |
1 ms |
2384 KB |
Output is correct |
11 |
Correct |
1 ms |
4432 KB |
Output is correct |
12 |
Correct |
1 ms |
4432 KB |
Output is correct |
13 |
Correct |
1 ms |
4432 KB |
Output is correct |
14 |
Correct |
2 ms |
4688 KB |
Output is correct |
15 |
Correct |
1 ms |
4432 KB |
Output is correct |
16 |
Correct |
1 ms |
4432 KB |
Output is correct |
17 |
Correct |
1 ms |
2384 KB |
Output is correct |
18 |
Correct |
1 ms |
2384 KB |
Output is correct |
19 |
Correct |
1 ms |
4432 KB |
Output is correct |
20 |
Correct |
1 ms |
4432 KB |
Output is correct |
21 |
Correct |
2 ms |
4432 KB |
Output is correct |
22 |
Correct |
2 ms |
4432 KB |
Output is correct |
23 |
Correct |
1 ms |
4432 KB |
Output is correct |
24 |
Correct |
2 ms |
4432 KB |
Output is correct |
25 |
Correct |
2 ms |
4432 KB |
Output is correct |
26 |
Correct |
2 ms |
4432 KB |
Output is correct |
27 |
Correct |
2 ms |
4432 KB |
Output is correct |
28 |
Correct |
1 ms |
4556 KB |
Output is correct |
29 |
Correct |
2 ms |
4432 KB |
Output is correct |
30 |
Correct |
2 ms |
4432 KB |
Output is correct |
31 |
Correct |
2 ms |
4432 KB |
Output is correct |
32 |
Correct |
1 ms |
4432 KB |
Output is correct |
33 |
Correct |
1 ms |
4432 KB |
Output is correct |
34 |
Correct |
1 ms |
4432 KB |
Output is correct |
35 |
Correct |
2 ms |
4432 KB |
Output is correct |
36 |
Correct |
2 ms |
4432 KB |
Output is correct |
37 |
Correct |
2 ms |
4432 KB |
Output is correct |
38 |
Correct |
2 ms |
4432 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4432 KB |
Output is correct |
2 |
Correct |
1 ms |
2384 KB |
Output is correct |
3 |
Correct |
1 ms |
4432 KB |
Output is correct |
4 |
Correct |
1 ms |
4432 KB |
Output is correct |
5 |
Correct |
1 ms |
4548 KB |
Output is correct |
6 |
Correct |
1 ms |
4432 KB |
Output is correct |
7 |
Correct |
1 ms |
4432 KB |
Output is correct |
8 |
Correct |
1 ms |
2444 KB |
Output is correct |
9 |
Correct |
1 ms |
4432 KB |
Output is correct |
10 |
Correct |
1 ms |
2384 KB |
Output is correct |
11 |
Correct |
1 ms |
4432 KB |
Output is correct |
12 |
Correct |
1 ms |
4432 KB |
Output is correct |
13 |
Correct |
1 ms |
4432 KB |
Output is correct |
14 |
Correct |
2 ms |
4688 KB |
Output is correct |
15 |
Correct |
1 ms |
4432 KB |
Output is correct |
16 |
Correct |
1 ms |
4432 KB |
Output is correct |
17 |
Correct |
1 ms |
2384 KB |
Output is correct |
18 |
Correct |
1 ms |
2384 KB |
Output is correct |
19 |
Correct |
1 ms |
4432 KB |
Output is correct |
20 |
Correct |
1 ms |
4432 KB |
Output is correct |
21 |
Correct |
2 ms |
4432 KB |
Output is correct |
22 |
Correct |
2 ms |
4432 KB |
Output is correct |
23 |
Correct |
1 ms |
4432 KB |
Output is correct |
24 |
Correct |
2 ms |
4432 KB |
Output is correct |
25 |
Correct |
2 ms |
4432 KB |
Output is correct |
26 |
Correct |
2 ms |
4432 KB |
Output is correct |
27 |
Correct |
2 ms |
4432 KB |
Output is correct |
28 |
Correct |
1 ms |
4556 KB |
Output is correct |
29 |
Correct |
2 ms |
4432 KB |
Output is correct |
30 |
Correct |
2 ms |
4432 KB |
Output is correct |
31 |
Correct |
2 ms |
4432 KB |
Output is correct |
32 |
Correct |
1 ms |
4432 KB |
Output is correct |
33 |
Correct |
1 ms |
4432 KB |
Output is correct |
34 |
Correct |
1 ms |
4432 KB |
Output is correct |
35 |
Correct |
2 ms |
4432 KB |
Output is correct |
36 |
Correct |
2 ms |
4432 KB |
Output is correct |
37 |
Correct |
2 ms |
4432 KB |
Output is correct |
38 |
Correct |
2 ms |
4432 KB |
Output is correct |
39 |
Correct |
5 ms |
6736 KB |
Output is correct |
40 |
Correct |
5 ms |
6736 KB |
Output is correct |
41 |
Correct |
5 ms |
6736 KB |
Output is correct |
42 |
Correct |
4 ms |
4688 KB |
Output is correct |
43 |
Correct |
4 ms |
4688 KB |
Output is correct |
44 |
Correct |
5 ms |
4688 KB |
Output is correct |
45 |
Correct |
4 ms |
4816 KB |
Output is correct |
46 |
Correct |
5 ms |
4688 KB |
Output is correct |
47 |
Correct |
4 ms |
6860 KB |
Output is correct |
48 |
Correct |
5 ms |
6736 KB |
Output is correct |
49 |
Correct |
5 ms |
6736 KB |
Output is correct |
50 |
Correct |
4 ms |
4688 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2384 KB |
Output is correct |
2 |
Correct |
1 ms |
4432 KB |
Output is correct |
3 |
Correct |
1 ms |
4432 KB |
Output is correct |
4 |
Correct |
1 ms |
4432 KB |
Output is correct |
5 |
Correct |
1 ms |
4432 KB |
Output is correct |
6 |
Correct |
1 ms |
2384 KB |
Output is correct |
7 |
Correct |
1 ms |
2384 KB |
Output is correct |
8 |
Correct |
1 ms |
4432 KB |
Output is correct |
9 |
Correct |
2 ms |
4600 KB |
Output is correct |
10 |
Correct |
1 ms |
4432 KB |
Output is correct |
11 |
Correct |
2 ms |
4600 KB |
Output is correct |
12 |
Correct |
2 ms |
4432 KB |
Output is correct |
13 |
Correct |
2 ms |
4432 KB |
Output is correct |
14 |
Correct |
2 ms |
4432 KB |
Output is correct |
15 |
Correct |
5 ms |
6736 KB |
Output is correct |
16 |
Correct |
4 ms |
4688 KB |
Output is correct |
17 |
Correct |
4 ms |
4688 KB |
Output is correct |
18 |
Correct |
4 ms |
4688 KB |
Output is correct |
19 |
Correct |
4 ms |
6872 KB |
Output is correct |
20 |
Correct |
35 ms |
9820 KB |
Output is correct |
21 |
Correct |
175 ms |
58540 KB |
Output is correct |
22 |
Correct |
157 ms |
58540 KB |
Output is correct |
23 |
Correct |
142 ms |
34472 KB |
Output is correct |
24 |
Correct |
226 ms |
58540 KB |
Output is correct |
25 |
Correct |
99 ms |
30356 KB |
Output is correct |
26 |
Correct |
137 ms |
35248 KB |
Output is correct |
27 |
Correct |
136 ms |
35244 KB |
Output is correct |
28 |
Correct |
169 ms |
58028 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2384 KB |
Output is correct |
2 |
Correct |
1 ms |
4432 KB |
Output is correct |
3 |
Correct |
1 ms |
4432 KB |
Output is correct |
4 |
Correct |
1 ms |
4432 KB |
Output is correct |
5 |
Correct |
1 ms |
2384 KB |
Output is correct |
6 |
Correct |
1 ms |
2384 KB |
Output is correct |
7 |
Correct |
2 ms |
2384 KB |
Output is correct |
8 |
Correct |
2 ms |
4432 KB |
Output is correct |
9 |
Correct |
2 ms |
4432 KB |
Output is correct |
10 |
Correct |
1 ms |
4432 KB |
Output is correct |
11 |
Correct |
2 ms |
4600 KB |
Output is correct |
12 |
Correct |
5 ms |
6992 KB |
Output is correct |
13 |
Correct |
5 ms |
4688 KB |
Output is correct |
14 |
Correct |
3 ms |
4688 KB |
Output is correct |
15 |
Correct |
36 ms |
9644 KB |
Output is correct |
16 |
Correct |
151 ms |
34524 KB |
Output is correct |
17 |
Correct |
173 ms |
58540 KB |
Output is correct |
18 |
Correct |
152 ms |
58536 KB |
Output is correct |
19 |
Correct |
186 ms |
58540 KB |
Output is correct |
20 |
Correct |
145 ms |
34472 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4432 KB |
Output is correct |
2 |
Correct |
1 ms |
2384 KB |
Output is correct |
3 |
Correct |
1 ms |
4432 KB |
Output is correct |
4 |
Correct |
1 ms |
4432 KB |
Output is correct |
5 |
Correct |
1 ms |
4548 KB |
Output is correct |
6 |
Correct |
1 ms |
4432 KB |
Output is correct |
7 |
Correct |
1 ms |
4432 KB |
Output is correct |
8 |
Correct |
1 ms |
2444 KB |
Output is correct |
9 |
Correct |
1 ms |
4432 KB |
Output is correct |
10 |
Correct |
1 ms |
2384 KB |
Output is correct |
11 |
Correct |
1 ms |
4432 KB |
Output is correct |
12 |
Correct |
1 ms |
4432 KB |
Output is correct |
13 |
Correct |
1 ms |
4432 KB |
Output is correct |
14 |
Correct |
2 ms |
4688 KB |
Output is correct |
15 |
Correct |
1 ms |
4432 KB |
Output is correct |
16 |
Correct |
1 ms |
4432 KB |
Output is correct |
17 |
Correct |
1 ms |
2384 KB |
Output is correct |
18 |
Correct |
1 ms |
2384 KB |
Output is correct |
19 |
Correct |
1 ms |
4432 KB |
Output is correct |
20 |
Correct |
1 ms |
4432 KB |
Output is correct |
21 |
Correct |
2 ms |
4432 KB |
Output is correct |
22 |
Correct |
2 ms |
4432 KB |
Output is correct |
23 |
Correct |
1 ms |
4432 KB |
Output is correct |
24 |
Correct |
2 ms |
4432 KB |
Output is correct |
25 |
Correct |
2 ms |
4432 KB |
Output is correct |
26 |
Correct |
2 ms |
4432 KB |
Output is correct |
27 |
Correct |
2 ms |
4432 KB |
Output is correct |
28 |
Correct |
1 ms |
4556 KB |
Output is correct |
29 |
Correct |
2 ms |
4432 KB |
Output is correct |
30 |
Correct |
2 ms |
4432 KB |
Output is correct |
31 |
Correct |
2 ms |
4432 KB |
Output is correct |
32 |
Correct |
1 ms |
4432 KB |
Output is correct |
33 |
Correct |
1 ms |
4432 KB |
Output is correct |
34 |
Correct |
1 ms |
4432 KB |
Output is correct |
35 |
Correct |
2 ms |
4432 KB |
Output is correct |
36 |
Correct |
2 ms |
4432 KB |
Output is correct |
37 |
Correct |
2 ms |
4432 KB |
Output is correct |
38 |
Correct |
2 ms |
4432 KB |
Output is correct |
39 |
Correct |
5 ms |
6736 KB |
Output is correct |
40 |
Correct |
5 ms |
6736 KB |
Output is correct |
41 |
Correct |
5 ms |
6736 KB |
Output is correct |
42 |
Correct |
4 ms |
4688 KB |
Output is correct |
43 |
Correct |
4 ms |
4688 KB |
Output is correct |
44 |
Correct |
5 ms |
4688 KB |
Output is correct |
45 |
Correct |
4 ms |
4816 KB |
Output is correct |
46 |
Correct |
5 ms |
4688 KB |
Output is correct |
47 |
Correct |
4 ms |
6860 KB |
Output is correct |
48 |
Correct |
5 ms |
6736 KB |
Output is correct |
49 |
Correct |
5 ms |
6736 KB |
Output is correct |
50 |
Correct |
4 ms |
4688 KB |
Output is correct |
51 |
Correct |
2 ms |
2384 KB |
Output is correct |
52 |
Correct |
1 ms |
4432 KB |
Output is correct |
53 |
Correct |
1 ms |
4432 KB |
Output is correct |
54 |
Correct |
1 ms |
4432 KB |
Output is correct |
55 |
Correct |
1 ms |
4432 KB |
Output is correct |
56 |
Correct |
1 ms |
2384 KB |
Output is correct |
57 |
Correct |
1 ms |
2384 KB |
Output is correct |
58 |
Correct |
1 ms |
4432 KB |
Output is correct |
59 |
Correct |
2 ms |
4600 KB |
Output is correct |
60 |
Correct |
1 ms |
4432 KB |
Output is correct |
61 |
Correct |
2 ms |
4600 KB |
Output is correct |
62 |
Correct |
2 ms |
4432 KB |
Output is correct |
63 |
Correct |
2 ms |
4432 KB |
Output is correct |
64 |
Correct |
2 ms |
4432 KB |
Output is correct |
65 |
Correct |
5 ms |
6736 KB |
Output is correct |
66 |
Correct |
4 ms |
4688 KB |
Output is correct |
67 |
Correct |
4 ms |
4688 KB |
Output is correct |
68 |
Correct |
4 ms |
4688 KB |
Output is correct |
69 |
Correct |
4 ms |
6872 KB |
Output is correct |
70 |
Correct |
35 ms |
9820 KB |
Output is correct |
71 |
Correct |
175 ms |
58540 KB |
Output is correct |
72 |
Correct |
157 ms |
58540 KB |
Output is correct |
73 |
Correct |
142 ms |
34472 KB |
Output is correct |
74 |
Correct |
226 ms |
58540 KB |
Output is correct |
75 |
Correct |
99 ms |
30356 KB |
Output is correct |
76 |
Correct |
137 ms |
35248 KB |
Output is correct |
77 |
Correct |
136 ms |
35244 KB |
Output is correct |
78 |
Correct |
169 ms |
58028 KB |
Output is correct |
79 |
Correct |
1 ms |
2384 KB |
Output is correct |
80 |
Correct |
1 ms |
4432 KB |
Output is correct |
81 |
Correct |
1 ms |
4432 KB |
Output is correct |
82 |
Correct |
1 ms |
4432 KB |
Output is correct |
83 |
Correct |
1 ms |
2384 KB |
Output is correct |
84 |
Correct |
1 ms |
2384 KB |
Output is correct |
85 |
Correct |
2 ms |
2384 KB |
Output is correct |
86 |
Correct |
2 ms |
4432 KB |
Output is correct |
87 |
Correct |
2 ms |
4432 KB |
Output is correct |
88 |
Correct |
1 ms |
4432 KB |
Output is correct |
89 |
Correct |
2 ms |
4600 KB |
Output is correct |
90 |
Correct |
5 ms |
6992 KB |
Output is correct |
91 |
Correct |
5 ms |
4688 KB |
Output is correct |
92 |
Correct |
3 ms |
4688 KB |
Output is correct |
93 |
Correct |
36 ms |
9644 KB |
Output is correct |
94 |
Correct |
151 ms |
34524 KB |
Output is correct |
95 |
Correct |
173 ms |
58540 KB |
Output is correct |
96 |
Correct |
152 ms |
58536 KB |
Output is correct |
97 |
Correct |
186 ms |
58540 KB |
Output is correct |
98 |
Correct |
145 ms |
34472 KB |
Output is correct |
99 |
Correct |
199 ms |
56336 KB |
Output is correct |
100 |
Correct |
172 ms |
58696 KB |
Output is correct |
101 |
Correct |
205 ms |
58532 KB |
Output is correct |
102 |
Correct |
207 ms |
58536 KB |
Output is correct |
103 |
Correct |
149 ms |
35260 KB |
Output is correct |
104 |
Correct |
192 ms |
57840 KB |
Output is correct |
105 |
Correct |
163 ms |
57440 KB |
Output is correct |