#include<bits/stdc++.h>
using namespace std;
#define int long long
const int MAX_INTER=200*1000+5,MAX_LOG=18,INFINI=1000*1000*1000;
int nbInter,tailleTour;
vector<pair<int,int>> listeInter;
int inter[MAX_INTER][2];
int proch[MAX_INTER];
int pere[MAX_INTER][MAX_LOG][2];
bool tourn[MAX_INTER];
multiset<pair<int,int>> enCours;
int tailleInter(int deb,int fin) {
if (deb<=fin) {
return fin-deb;
}
else {
return tailleTour-(deb-fin);
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin>>nbInter>>tailleTour;
for (int i=1;i<=nbInter;i++) {
cin>>inter[i][0]>>inter[i][1];
if (inter[i][0]<inter[i][1]) {
listeInter.push_back({inter[i][0],-i});
listeInter.push_back({inter[i][1],i});
listeInter.push_back({inter[i][0]+tailleTour,-i});
listeInter.push_back({inter[i][1]+tailleTour,i});
}
else {
tourn[i]=true;
listeInter.push_back({inter[i][0],-i});
listeInter.push_back({inter[i][1]+tailleTour,i});
}
}
multiset<pair<int,int>>::iterator it;
sort(listeInter.begin(),listeInter.end());
for (auto i:listeInter) {
if (i.second<0) {
i.second*=-1;
if (tourn[i.second]) {
enCours.insert({inter[i.second][1]+tailleTour,i.second});
}
else {
enCours.insert({i.first-inter[i.second][0]+inter[i.second][1],i.second});
}
}
else {
if (tourn[i.second] or i.first<tailleTour) {
it=enCours.end();
it--;
proch[i.second]=(*it).second;
//cout<<i.second<<" "<<proch[i.second]<<endl;
}
it=enCours.lower_bound(i);
enCours.erase(it);
}
}
for (int i=1;i<=nbInter;i++) {
pere[i][0][0]=proch[i];
pere[i][0][1]=tailleInter(inter[i][1],inter[proch[i]][1]);
}
for (int j=1;j<MAX_LOG;j++) {
for (int i=1;i<=nbInter;i++) {
pere[i][j][0]=pere[pere[i][j-1][0]][j-1][0];
pere[i][j][1]=pere[i][j-1][1]+pere[pere[i][j-1][0]][j-1][1];
}
}
int pos,dejFait,nbUti,rep=INFINI;
for (int i=1;i<=nbInter;i++) {
pos=i;
dejFait=tailleInter(inter[i][0],inter[i][1]);
//cout<<dejFait<<" ";
nbUti=1;
for (int j=MAX_LOG-1;j>=0;j--) {
if (dejFait+pere[pos][j][1]<tailleTour) {
nbUti+=(1<<j);
dejFait+=pere[pos][j][1];
pos=pere[pos][j][0];
}
}
nbUti++;
dejFait+=pere[pos][0][1];
//cout<<i<<" "<<nbUti<<endl;
if (dejFait>=tailleTour) {
rep=min(rep,nbUti);
}
}
if (rep==INFINI) {
cout<<-1<<endl;
}
else {
cout<<rep<<endl;
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 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 |
344 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 |
2392 KB |
Output is correct |
14 |
Correct |
1 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 |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
344 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 |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 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 |
344 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 |
2392 KB |
Output is correct |
14 |
Correct |
1 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 |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
344 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
2396 KB |
Output is correct |
25 |
Correct |
1 ms |
604 KB |
Output is correct |
26 |
Correct |
1 ms |
600 KB |
Output is correct |
27 |
Correct |
1 ms |
6492 KB |
Output is correct |
28 |
Correct |
1 ms |
6496 KB |
Output is correct |
29 |
Correct |
1 ms |
6492 KB |
Output is correct |
30 |
Correct |
1 ms |
636 KB |
Output is correct |
31 |
Correct |
1 ms |
604 KB |
Output is correct |
32 |
Correct |
1 ms |
604 KB |
Output is correct |
33 |
Correct |
1 ms |
608 KB |
Output is correct |
34 |
Correct |
1 ms |
604 KB |
Output is correct |
35 |
Correct |
1 ms |
604 KB |
Output is correct |
36 |
Correct |
1 ms |
604 KB |
Output is correct |
37 |
Correct |
1 ms |
604 KB |
Output is correct |
38 |
Correct |
1 ms |
600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 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 |
344 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 |
2392 KB |
Output is correct |
14 |
Correct |
1 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 |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
344 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
2396 KB |
Output is correct |
25 |
Correct |
1 ms |
604 KB |
Output is correct |
26 |
Correct |
1 ms |
600 KB |
Output is correct |
27 |
Correct |
1 ms |
6492 KB |
Output is correct |
28 |
Correct |
1 ms |
6496 KB |
Output is correct |
29 |
Correct |
1 ms |
6492 KB |
Output is correct |
30 |
Correct |
1 ms |
636 KB |
Output is correct |
31 |
Correct |
1 ms |
604 KB |
Output is correct |
32 |
Correct |
1 ms |
604 KB |
Output is correct |
33 |
Correct |
1 ms |
608 KB |
Output is correct |
34 |
Correct |
1 ms |
604 KB |
Output is correct |
35 |
Correct |
1 ms |
604 KB |
Output is correct |
36 |
Correct |
1 ms |
604 KB |
Output is correct |
37 |
Correct |
1 ms |
604 KB |
Output is correct |
38 |
Correct |
1 ms |
600 KB |
Output is correct |
39 |
Correct |
7 ms |
4316 KB |
Output is correct |
40 |
Correct |
6 ms |
4316 KB |
Output is correct |
41 |
Correct |
4 ms |
2524 KB |
Output is correct |
42 |
Correct |
4 ms |
4316 KB |
Output is correct |
43 |
Correct |
7 ms |
2520 KB |
Output is correct |
44 |
Correct |
4 ms |
2524 KB |
Output is correct |
45 |
Correct |
4 ms |
2524 KB |
Output is correct |
46 |
Correct |
6 ms |
4320 KB |
Output is correct |
47 |
Correct |
4 ms |
4316 KB |
Output is correct |
48 |
Correct |
6 ms |
4316 KB |
Output is correct |
49 |
Correct |
6 ms |
2524 KB |
Output is correct |
50 |
Correct |
4 ms |
7132 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
464 KB |
Output is correct |
5 |
Correct |
1 ms |
600 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
472 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
0 ms |
604 KB |
Output is correct |
15 |
Correct |
6 ms |
2440 KB |
Output is correct |
16 |
Correct |
6 ms |
2524 KB |
Output is correct |
17 |
Correct |
6 ms |
2524 KB |
Output is correct |
18 |
Correct |
4 ms |
2524 KB |
Output is correct |
19 |
Correct |
4 ms |
2524 KB |
Output is correct |
20 |
Correct |
206 ms |
84388 KB |
Output is correct |
21 |
Correct |
242 ms |
78000 KB |
Output is correct |
22 |
Correct |
235 ms |
78076 KB |
Output is correct |
23 |
Correct |
323 ms |
77068 KB |
Output is correct |
24 |
Correct |
230 ms |
77936 KB |
Output is correct |
25 |
Correct |
278 ms |
81056 KB |
Output is correct |
26 |
Correct |
259 ms |
77932 KB |
Output is correct |
27 |
Correct |
313 ms |
77996 KB |
Output is correct |
28 |
Correct |
239 ms |
77864 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
2524 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
472 KB |
Output is correct |
12 |
Correct |
6 ms |
2524 KB |
Output is correct |
13 |
Correct |
6 ms |
2524 KB |
Output is correct |
14 |
Correct |
4 ms |
2524 KB |
Output is correct |
15 |
Correct |
212 ms |
84244 KB |
Output is correct |
16 |
Correct |
301 ms |
77084 KB |
Output is correct |
17 |
Correct |
260 ms |
78224 KB |
Output is correct |
18 |
Correct |
229 ms |
78528 KB |
Output is correct |
19 |
Correct |
266 ms |
78312 KB |
Output is correct |
20 |
Correct |
324 ms |
77008 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 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 |
344 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 |
2392 KB |
Output is correct |
14 |
Correct |
1 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 |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
344 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
2396 KB |
Output is correct |
25 |
Correct |
1 ms |
604 KB |
Output is correct |
26 |
Correct |
1 ms |
600 KB |
Output is correct |
27 |
Correct |
1 ms |
6492 KB |
Output is correct |
28 |
Correct |
1 ms |
6496 KB |
Output is correct |
29 |
Correct |
1 ms |
6492 KB |
Output is correct |
30 |
Correct |
1 ms |
636 KB |
Output is correct |
31 |
Correct |
1 ms |
604 KB |
Output is correct |
32 |
Correct |
1 ms |
604 KB |
Output is correct |
33 |
Correct |
1 ms |
608 KB |
Output is correct |
34 |
Correct |
1 ms |
604 KB |
Output is correct |
35 |
Correct |
1 ms |
604 KB |
Output is correct |
36 |
Correct |
1 ms |
604 KB |
Output is correct |
37 |
Correct |
1 ms |
604 KB |
Output is correct |
38 |
Correct |
1 ms |
600 KB |
Output is correct |
39 |
Correct |
7 ms |
4316 KB |
Output is correct |
40 |
Correct |
6 ms |
4316 KB |
Output is correct |
41 |
Correct |
4 ms |
2524 KB |
Output is correct |
42 |
Correct |
4 ms |
4316 KB |
Output is correct |
43 |
Correct |
7 ms |
2520 KB |
Output is correct |
44 |
Correct |
4 ms |
2524 KB |
Output is correct |
45 |
Correct |
4 ms |
2524 KB |
Output is correct |
46 |
Correct |
6 ms |
4320 KB |
Output is correct |
47 |
Correct |
4 ms |
4316 KB |
Output is correct |
48 |
Correct |
6 ms |
4316 KB |
Output is correct |
49 |
Correct |
6 ms |
2524 KB |
Output is correct |
50 |
Correct |
4 ms |
7132 KB |
Output is correct |
51 |
Correct |
1 ms |
4444 KB |
Output is correct |
52 |
Correct |
1 ms |
6492 KB |
Output is correct |
53 |
Correct |
1 ms |
348 KB |
Output is correct |
54 |
Correct |
1 ms |
464 KB |
Output is correct |
55 |
Correct |
1 ms |
600 KB |
Output is correct |
56 |
Correct |
1 ms |
348 KB |
Output is correct |
57 |
Correct |
1 ms |
348 KB |
Output is correct |
58 |
Correct |
0 ms |
344 KB |
Output is correct |
59 |
Correct |
1 ms |
604 KB |
Output is correct |
60 |
Correct |
1 ms |
472 KB |
Output is correct |
61 |
Correct |
1 ms |
604 KB |
Output is correct |
62 |
Correct |
1 ms |
604 KB |
Output is correct |
63 |
Correct |
1 ms |
604 KB |
Output is correct |
64 |
Correct |
0 ms |
604 KB |
Output is correct |
65 |
Correct |
6 ms |
2440 KB |
Output is correct |
66 |
Correct |
6 ms |
2524 KB |
Output is correct |
67 |
Correct |
6 ms |
2524 KB |
Output is correct |
68 |
Correct |
4 ms |
2524 KB |
Output is correct |
69 |
Correct |
4 ms |
2524 KB |
Output is correct |
70 |
Correct |
206 ms |
84388 KB |
Output is correct |
71 |
Correct |
242 ms |
78000 KB |
Output is correct |
72 |
Correct |
235 ms |
78076 KB |
Output is correct |
73 |
Correct |
323 ms |
77068 KB |
Output is correct |
74 |
Correct |
230 ms |
77936 KB |
Output is correct |
75 |
Correct |
278 ms |
81056 KB |
Output is correct |
76 |
Correct |
259 ms |
77932 KB |
Output is correct |
77 |
Correct |
313 ms |
77996 KB |
Output is correct |
78 |
Correct |
239 ms |
77864 KB |
Output is correct |
79 |
Correct |
1 ms |
6492 KB |
Output is correct |
80 |
Correct |
1 ms |
4444 KB |
Output is correct |
81 |
Correct |
1 ms |
348 KB |
Output is correct |
82 |
Correct |
1 ms |
4444 KB |
Output is correct |
83 |
Correct |
0 ms |
2396 KB |
Output is correct |
84 |
Correct |
1 ms |
4444 KB |
Output is correct |
85 |
Correct |
1 ms |
2524 KB |
Output is correct |
86 |
Correct |
1 ms |
604 KB |
Output is correct |
87 |
Correct |
1 ms |
604 KB |
Output is correct |
88 |
Correct |
1 ms |
604 KB |
Output is correct |
89 |
Correct |
1 ms |
472 KB |
Output is correct |
90 |
Correct |
6 ms |
2524 KB |
Output is correct |
91 |
Correct |
6 ms |
2524 KB |
Output is correct |
92 |
Correct |
4 ms |
2524 KB |
Output is correct |
93 |
Correct |
212 ms |
84244 KB |
Output is correct |
94 |
Correct |
301 ms |
77084 KB |
Output is correct |
95 |
Correct |
260 ms |
78224 KB |
Output is correct |
96 |
Correct |
229 ms |
78528 KB |
Output is correct |
97 |
Correct |
266 ms |
78312 KB |
Output is correct |
98 |
Correct |
324 ms |
77008 KB |
Output is correct |
99 |
Correct |
211 ms |
78124 KB |
Output is correct |
100 |
Correct |
241 ms |
78108 KB |
Output is correct |
101 |
Correct |
302 ms |
77984 KB |
Output is correct |
102 |
Correct |
283 ms |
78040 KB |
Output is correct |
103 |
Correct |
301 ms |
78704 KB |
Output is correct |
104 |
Correct |
222 ms |
77804 KB |
Output is correct |
105 |
Correct |
174 ms |
76704 KB |
Output is correct |