#include <bits/stdc++.h>
#define int long long
using namespace std;
const int INFINI = 1000 * 1000 * 1000;
map<int, int> idxs;
vector<pair<int, int>> arbre((1 << 20), {INFINI, -1});
void set_min(int pos, pair<int, int> val) {
pos += (1 << 19);
while(pos != 0) {
arbre[pos] = min(arbre[pos], val);
pos /= 2;
}
}
pair<int, int> get_min(int deb, int fin) {
deb += (1 << 19);
fin += (1 << 19);
pair<int, int> mini = {INFINI, -1};
while(deb < fin) {
if(deb % 2 == 1) {
mini = min(mini, arbre[deb]);
deb++;
}
if(fin % 2 == 1) {
fin--;
mini = min(mini, arbre[fin]);
}
deb /= 2;
fin /= 2;
}
return mini;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
int N, Q;
cin >> N >> Q;
vector<pair<int, int>> inters;
for(int i = 0;i < N;i++) {
int deb, fin;
cin >> deb >> fin;
idxs[deb] = idxs[fin] = -1;
inters.push_back({deb, fin});
}
int iIdx = 0;
for(auto it = idxs.begin();it != idxs.end();it++) {
it->second = iIdx++;
}
int iInter = 0;
for(pair<int, int>& inter : inters) {
inter.first = idxs[inter.first];
inter.second = idxs[inter.second];
set_min(inter.second, {inter.first, iInter});
iInter++;
}
vector<vector<int>> parents(
30,
vector<int>(inters.size(), -1)
);
for(int iInter = 0;iInter < (int)inters.size();iInter++) {
parents[0][iInter] = get_min(inters[iInter].first, inters[iInter].second + 1).second;
}
for(int lvl = 1;lvl < 30;lvl++) {
for(int iInter = 0;iInter < (int)inters.size();iInter++) {
parents[lvl][iInter] = parents[lvl - 1][parents[lvl - 1][iInter]];
}
}
for(int iReq = 0;iReq < Q;iReq++) {
int deb, fin;
cin >> deb >> fin;
deb--; fin--;
if(inters[deb].second > inters[fin].second) {
cout << "impossible\n";
continue;
}
if(deb == fin) {
cout << "0\n";
continue;
}
if(inters[deb].second <= inters[fin].second && inters[deb].second >= inters[fin].first) {
cout << "1\n";
continue;
}
int cur = fin;
int nbSteps = 0;
for(int iHop = 29;iHop >= 0;iHop--) {
if(inters[parents[iHop][cur]].first > inters[deb].second) {
nbSteps += (1 << iHop);
cur = parents[iHop][cur];
}
}
if(inters[parents[0][cur]].first <= inters[deb].second) {
cout << nbSteps + 2 << "\n";
}
else {
cout << "impossible\n";
}
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
16716 KB |
Output is correct |
2 |
Correct |
225 ms |
49320 KB |
Output is correct |
3 |
Correct |
237 ms |
49400 KB |
Output is correct |
4 |
Correct |
341 ms |
49348 KB |
Output is correct |
5 |
Correct |
242 ms |
50044 KB |
Output is correct |
6 |
Correct |
281 ms |
50716 KB |
Output is correct |
7 |
Correct |
316 ms |
50828 KB |
Output is correct |
8 |
Correct |
371 ms |
56180 KB |
Output is correct |
9 |
Correct |
384 ms |
56196 KB |
Output is correct |
10 |
Correct |
260 ms |
49768 KB |
Output is correct |
11 |
Correct |
329 ms |
52288 KB |
Output is correct |
12 |
Correct |
191 ms |
52252 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
16716 KB |
Output is correct |
2 |
Correct |
7 ms |
16724 KB |
Output is correct |
3 |
Correct |
8 ms |
16980 KB |
Output is correct |
4 |
Correct |
12 ms |
16996 KB |
Output is correct |
5 |
Correct |
10 ms |
17028 KB |
Output is correct |
6 |
Correct |
15 ms |
16984 KB |
Output is correct |
7 |
Correct |
14 ms |
17140 KB |
Output is correct |
8 |
Correct |
11 ms |
17108 KB |
Output is correct |
9 |
Correct |
9 ms |
16980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
16716 KB |
Output is correct |
2 |
Correct |
7 ms |
16724 KB |
Output is correct |
3 |
Correct |
8 ms |
16980 KB |
Output is correct |
4 |
Correct |
12 ms |
16996 KB |
Output is correct |
5 |
Correct |
10 ms |
17028 KB |
Output is correct |
6 |
Correct |
15 ms |
16984 KB |
Output is correct |
7 |
Correct |
14 ms |
17140 KB |
Output is correct |
8 |
Correct |
11 ms |
17108 KB |
Output is correct |
9 |
Correct |
9 ms |
16980 KB |
Output is correct |
10 |
Correct |
11 ms |
16688 KB |
Output is correct |
11 |
Correct |
8 ms |
16724 KB |
Output is correct |
12 |
Correct |
11 ms |
17020 KB |
Output is correct |
13 |
Correct |
47 ms |
17064 KB |
Output is correct |
14 |
Correct |
9 ms |
17012 KB |
Output is correct |
15 |
Correct |
11 ms |
16980 KB |
Output is correct |
16 |
Correct |
14 ms |
17152 KB |
Output is correct |
17 |
Correct |
12 ms |
17152 KB |
Output is correct |
18 |
Correct |
10 ms |
16948 KB |
Output is correct |
19 |
Correct |
54 ms |
19844 KB |
Output is correct |
20 |
Correct |
65 ms |
19788 KB |
Output is correct |
21 |
Correct |
55 ms |
20052 KB |
Output is correct |
22 |
Correct |
109 ms |
20192 KB |
Output is correct |
23 |
Correct |
38 ms |
20300 KB |
Output is correct |
24 |
Correct |
33 ms |
20328 KB |
Output is correct |
25 |
Correct |
43 ms |
19916 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
16716 KB |
Output is correct |
2 |
Correct |
7 ms |
16724 KB |
Output is correct |
3 |
Correct |
8 ms |
16980 KB |
Output is correct |
4 |
Correct |
12 ms |
16996 KB |
Output is correct |
5 |
Correct |
10 ms |
17028 KB |
Output is correct |
6 |
Correct |
15 ms |
16984 KB |
Output is correct |
7 |
Correct |
14 ms |
17140 KB |
Output is correct |
8 |
Correct |
11 ms |
17108 KB |
Output is correct |
9 |
Correct |
9 ms |
16980 KB |
Output is correct |
10 |
Correct |
12 ms |
16772 KB |
Output is correct |
11 |
Correct |
11 ms |
16752 KB |
Output is correct |
12 |
Correct |
10 ms |
17020 KB |
Output is correct |
13 |
Correct |
9 ms |
17024 KB |
Output is correct |
14 |
Correct |
9 ms |
16984 KB |
Output is correct |
15 |
Correct |
9 ms |
17072 KB |
Output is correct |
16 |
Correct |
8 ms |
17108 KB |
Output is correct |
17 |
Correct |
10 ms |
17048 KB |
Output is correct |
18 |
Correct |
10 ms |
17016 KB |
Output is correct |
19 |
Correct |
266 ms |
50784 KB |
Output is correct |
20 |
Correct |
191 ms |
49908 KB |
Output is correct |
21 |
Correct |
253 ms |
50620 KB |
Output is correct |
22 |
Correct |
221 ms |
53020 KB |
Output is correct |
23 |
Correct |
320 ms |
57036 KB |
Output is correct |
24 |
Correct |
275 ms |
56960 KB |
Output is correct |
25 |
Correct |
46 ms |
43736 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
335 ms |
49416 KB |
Output is correct |
2 |
Correct |
286 ms |
49524 KB |
Output is correct |
3 |
Correct |
279 ms |
49368 KB |
Output is correct |
4 |
Correct |
399 ms |
56176 KB |
Output is correct |
5 |
Correct |
258 ms |
49708 KB |
Output is correct |
6 |
Correct |
352 ms |
55856 KB |
Output is correct |
7 |
Correct |
401 ms |
55736 KB |
Output is correct |
8 |
Correct |
333 ms |
55880 KB |
Output is correct |
9 |
Correct |
270 ms |
56960 KB |
Output is correct |
10 |
Correct |
392 ms |
58464 KB |
Output is correct |
11 |
Correct |
386 ms |
58372 KB |
Output is correct |
12 |
Correct |
428 ms |
58556 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
16716 KB |
Output is correct |
2 |
Correct |
225 ms |
49320 KB |
Output is correct |
3 |
Correct |
237 ms |
49400 KB |
Output is correct |
4 |
Correct |
341 ms |
49348 KB |
Output is correct |
5 |
Correct |
242 ms |
50044 KB |
Output is correct |
6 |
Correct |
281 ms |
50716 KB |
Output is correct |
7 |
Correct |
316 ms |
50828 KB |
Output is correct |
8 |
Correct |
371 ms |
56180 KB |
Output is correct |
9 |
Correct |
384 ms |
56196 KB |
Output is correct |
10 |
Correct |
260 ms |
49768 KB |
Output is correct |
11 |
Correct |
329 ms |
52288 KB |
Output is correct |
12 |
Correct |
191 ms |
52252 KB |
Output is correct |
13 |
Correct |
9 ms |
16716 KB |
Output is correct |
14 |
Correct |
7 ms |
16724 KB |
Output is correct |
15 |
Correct |
8 ms |
16980 KB |
Output is correct |
16 |
Correct |
12 ms |
16996 KB |
Output is correct |
17 |
Correct |
10 ms |
17028 KB |
Output is correct |
18 |
Correct |
15 ms |
16984 KB |
Output is correct |
19 |
Correct |
14 ms |
17140 KB |
Output is correct |
20 |
Correct |
11 ms |
17108 KB |
Output is correct |
21 |
Correct |
9 ms |
16980 KB |
Output is correct |
22 |
Correct |
11 ms |
16688 KB |
Output is correct |
23 |
Correct |
8 ms |
16724 KB |
Output is correct |
24 |
Correct |
11 ms |
17020 KB |
Output is correct |
25 |
Correct |
47 ms |
17064 KB |
Output is correct |
26 |
Correct |
9 ms |
17012 KB |
Output is correct |
27 |
Correct |
11 ms |
16980 KB |
Output is correct |
28 |
Correct |
14 ms |
17152 KB |
Output is correct |
29 |
Correct |
12 ms |
17152 KB |
Output is correct |
30 |
Correct |
10 ms |
16948 KB |
Output is correct |
31 |
Correct |
54 ms |
19844 KB |
Output is correct |
32 |
Correct |
65 ms |
19788 KB |
Output is correct |
33 |
Correct |
55 ms |
20052 KB |
Output is correct |
34 |
Correct |
109 ms |
20192 KB |
Output is correct |
35 |
Correct |
38 ms |
20300 KB |
Output is correct |
36 |
Correct |
33 ms |
20328 KB |
Output is correct |
37 |
Correct |
43 ms |
19916 KB |
Output is correct |
38 |
Correct |
12 ms |
16772 KB |
Output is correct |
39 |
Correct |
11 ms |
16752 KB |
Output is correct |
40 |
Correct |
10 ms |
17020 KB |
Output is correct |
41 |
Correct |
9 ms |
17024 KB |
Output is correct |
42 |
Correct |
9 ms |
16984 KB |
Output is correct |
43 |
Correct |
9 ms |
17072 KB |
Output is correct |
44 |
Correct |
8 ms |
17108 KB |
Output is correct |
45 |
Correct |
10 ms |
17048 KB |
Output is correct |
46 |
Correct |
10 ms |
17016 KB |
Output is correct |
47 |
Correct |
266 ms |
50784 KB |
Output is correct |
48 |
Correct |
191 ms |
49908 KB |
Output is correct |
49 |
Correct |
253 ms |
50620 KB |
Output is correct |
50 |
Correct |
221 ms |
53020 KB |
Output is correct |
51 |
Correct |
320 ms |
57036 KB |
Output is correct |
52 |
Correct |
275 ms |
56960 KB |
Output is correct |
53 |
Correct |
46 ms |
43736 KB |
Output is correct |
54 |
Correct |
335 ms |
49416 KB |
Output is correct |
55 |
Correct |
286 ms |
49524 KB |
Output is correct |
56 |
Correct |
279 ms |
49368 KB |
Output is correct |
57 |
Correct |
399 ms |
56176 KB |
Output is correct |
58 |
Correct |
258 ms |
49708 KB |
Output is correct |
59 |
Correct |
352 ms |
55856 KB |
Output is correct |
60 |
Correct |
401 ms |
55736 KB |
Output is correct |
61 |
Correct |
333 ms |
55880 KB |
Output is correct |
62 |
Correct |
270 ms |
56960 KB |
Output is correct |
63 |
Correct |
392 ms |
58464 KB |
Output is correct |
64 |
Correct |
386 ms |
58372 KB |
Output is correct |
65 |
Correct |
428 ms |
58556 KB |
Output is correct |
66 |
Correct |
9 ms |
16724 KB |
Output is correct |
67 |
Correct |
330 ms |
52464 KB |
Output is correct |
68 |
Correct |
303 ms |
52504 KB |
Output is correct |
69 |
Correct |
308 ms |
52448 KB |
Output is correct |
70 |
Correct |
286 ms |
53172 KB |
Output is correct |
71 |
Correct |
288 ms |
53792 KB |
Output is correct |
72 |
Correct |
306 ms |
53948 KB |
Output is correct |
73 |
Correct |
362 ms |
59200 KB |
Output is correct |
74 |
Correct |
354 ms |
59268 KB |
Output is correct |
75 |
Correct |
276 ms |
52936 KB |
Output is correct |
76 |
Correct |
374 ms |
55420 KB |
Output is correct |
77 |
Correct |
130 ms |
52272 KB |
Output is correct |
78 |
Correct |
12 ms |
16648 KB |
Output is correct |
79 |
Correct |
23 ms |
17024 KB |
Output is correct |
80 |
Correct |
11 ms |
16980 KB |
Output is correct |
81 |
Correct |
8 ms |
17012 KB |
Output is correct |
82 |
Correct |
9 ms |
17020 KB |
Output is correct |
83 |
Correct |
11 ms |
17236 KB |
Output is correct |
84 |
Correct |
12 ms |
17156 KB |
Output is correct |
85 |
Correct |
10 ms |
17016 KB |
Output is correct |
86 |
Correct |
68 ms |
19932 KB |
Output is correct |
87 |
Correct |
50 ms |
19860 KB |
Output is correct |
88 |
Correct |
50 ms |
19996 KB |
Output is correct |
89 |
Correct |
58 ms |
20200 KB |
Output is correct |
90 |
Correct |
43 ms |
20244 KB |
Output is correct |
91 |
Correct |
36 ms |
20360 KB |
Output is correct |
92 |
Correct |
45 ms |
19916 KB |
Output is correct |
93 |
Correct |
172 ms |
50776 KB |
Output is correct |
94 |
Correct |
248 ms |
49968 KB |
Output is correct |
95 |
Correct |
198 ms |
50620 KB |
Output is correct |
96 |
Correct |
353 ms |
53016 KB |
Output is correct |
97 |
Correct |
299 ms |
56964 KB |
Output is correct |
98 |
Correct |
313 ms |
56960 KB |
Output is correct |
99 |
Correct |
67 ms |
43752 KB |
Output is correct |
100 |
Correct |
449 ms |
58848 KB |
Output is correct |
101 |
Correct |
380 ms |
58852 KB |
Output is correct |
102 |
Correct |
352 ms |
58984 KB |
Output is correct |
103 |
Correct |
377 ms |
58476 KB |
Output is correct |
104 |
Correct |
424 ms |
58368 KB |
Output is correct |
105 |
Correct |
376 ms |
58560 KB |
Output is correct |
106 |
Correct |
260 ms |
50308 KB |
Output is correct |
107 |
Correct |
271 ms |
51924 KB |
Output is correct |
108 |
Correct |
360 ms |
52552 KB |
Output is correct |
109 |
Correct |
259 ms |
52668 KB |
Output is correct |
110 |
Correct |
363 ms |
58796 KB |
Output is correct |
111 |
Correct |
345 ms |
58772 KB |
Output is correct |