#include <bits/stdc++.h>
using namespace std;
constexpr int SZ = 1 << 18, INF = 0x3f3f3f3f;
pair<int,int> T[SZ<<1];
pair<int,int> Query(int l, int r){
l |= SZ; r |= SZ; pair<int,int> ret(INF,0);
while(l <= r){
if(l & 1) ret = min(ret, T[l++]);
if(~r & 1) ret = min(ret, T[r--]);
l >>= 1; r >>= 1;
}
return ret;
}
int N, K, Q, S[101010], E[101010], P[22][101010];
vector<int> C;
inline bool Check(int a, int b){ return S[b] <= E[a] && E[a] <= E[b]; }
int Solve(int a, int b){
int ret = 0;
if(a == b) return 0;
for(int i=21; i>=0; i--) if(S[P[i][b]] > S[a]) b = P[i][b], ret |= 1 << i;
if(ret + 1 == (1<<22)) return -1;
if(E[a] > E[b]) return -1;
if(Check(a, b)) return ret + 1;
if(P[0][b] != 0 && Check(a, P[0][b])) return ret + 2;
return -1;
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(nullptr);
cin >> N >> Q; C.reserve(N+N);
for(int i=1; i<=N; i++) cin >> S[i] >> E[i], C.push_back(S[i]), C.push_back(E[i]);
sort(C.begin(), C.end()); C.erase(unique(C.begin(), C.end()), C.end()); K = C.size();
for(int i=1; i<=N; i++){
S[i] = lower_bound(C.begin(), C.end(), S[i]) - C.begin() + 1;
E[i] = lower_bound(C.begin(), C.end(), E[i]) - C.begin() + 1;
}
fill(T, T+SZ*2, make_pair(INF,0));
for(int i=1; i<=N; i++) T[E[i]|SZ] = min(T[E[i]|SZ], make_pair(S[i], i));
for(int i=SZ-1; i; i--) T[i] = min(T[i<<1], T[i<<1|1]);
for(int i=1; i<=N; i++) P[0][i] = Query(S[i], E[i]).second;
for(int i=1; i<22; i++) for(int j=1; j<=N; j++) P[i][j] = P[i-1][P[i-1][j]];
for(int q=1; q<=Q; q++){
int a, b; cin >> a >> b;
int res = Solve(a, b);
if(res != -1) cout << res << "\n";
else cout << "impossible\n";
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4540 KB |
Output is correct |
2 |
Correct |
120 ms |
18380 KB |
Output is correct |
3 |
Correct |
106 ms |
18332 KB |
Output is correct |
4 |
Correct |
143 ms |
18212 KB |
Output is correct |
5 |
Correct |
124 ms |
18372 KB |
Output is correct |
6 |
Correct |
130 ms |
18528 KB |
Output is correct |
7 |
Correct |
117 ms |
18508 KB |
Output is correct |
8 |
Correct |
142 ms |
18800 KB |
Output is correct |
9 |
Correct |
171 ms |
18724 KB |
Output is correct |
10 |
Correct |
138 ms |
18640 KB |
Output is correct |
11 |
Correct |
118 ms |
18624 KB |
Output is correct |
12 |
Correct |
64 ms |
18020 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4436 KB |
Output is correct |
2 |
Correct |
3 ms |
4436 KB |
Output is correct |
3 |
Correct |
4 ms |
4564 KB |
Output is correct |
4 |
Correct |
3 ms |
4564 KB |
Output is correct |
5 |
Correct |
4 ms |
4628 KB |
Output is correct |
6 |
Correct |
4 ms |
4588 KB |
Output is correct |
7 |
Correct |
3 ms |
4564 KB |
Output is correct |
8 |
Correct |
3 ms |
4564 KB |
Output is correct |
9 |
Correct |
4 ms |
4564 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4436 KB |
Output is correct |
2 |
Correct |
3 ms |
4436 KB |
Output is correct |
3 |
Correct |
4 ms |
4564 KB |
Output is correct |
4 |
Correct |
3 ms |
4564 KB |
Output is correct |
5 |
Correct |
4 ms |
4628 KB |
Output is correct |
6 |
Correct |
4 ms |
4588 KB |
Output is correct |
7 |
Correct |
3 ms |
4564 KB |
Output is correct |
8 |
Correct |
3 ms |
4564 KB |
Output is correct |
9 |
Correct |
4 ms |
4564 KB |
Output is correct |
10 |
Correct |
3 ms |
4436 KB |
Output is correct |
11 |
Correct |
3 ms |
4436 KB |
Output is correct |
12 |
Correct |
5 ms |
4596 KB |
Output is correct |
13 |
Correct |
4 ms |
4564 KB |
Output is correct |
14 |
Correct |
4 ms |
4564 KB |
Output is correct |
15 |
Correct |
6 ms |
4564 KB |
Output is correct |
16 |
Correct |
4 ms |
4564 KB |
Output is correct |
17 |
Correct |
3 ms |
4564 KB |
Output is correct |
18 |
Correct |
3 ms |
4564 KB |
Output is correct |
19 |
Correct |
38 ms |
6408 KB |
Output is correct |
20 |
Correct |
34 ms |
6568 KB |
Output is correct |
21 |
Correct |
34 ms |
6772 KB |
Output is correct |
22 |
Correct |
33 ms |
6812 KB |
Output is correct |
23 |
Correct |
36 ms |
6628 KB |
Output is correct |
24 |
Correct |
33 ms |
6600 KB |
Output is correct |
25 |
Correct |
30 ms |
6348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4436 KB |
Output is correct |
2 |
Correct |
3 ms |
4436 KB |
Output is correct |
3 |
Correct |
4 ms |
4564 KB |
Output is correct |
4 |
Correct |
3 ms |
4564 KB |
Output is correct |
5 |
Correct |
4 ms |
4628 KB |
Output is correct |
6 |
Correct |
4 ms |
4588 KB |
Output is correct |
7 |
Correct |
3 ms |
4564 KB |
Output is correct |
8 |
Correct |
3 ms |
4564 KB |
Output is correct |
9 |
Correct |
4 ms |
4564 KB |
Output is correct |
10 |
Correct |
3 ms |
4436 KB |
Output is correct |
11 |
Correct |
2 ms |
4436 KB |
Output is correct |
12 |
Correct |
3 ms |
4620 KB |
Output is correct |
13 |
Correct |
3 ms |
4600 KB |
Output is correct |
14 |
Correct |
4 ms |
4644 KB |
Output is correct |
15 |
Correct |
5 ms |
4588 KB |
Output is correct |
16 |
Correct |
4 ms |
4564 KB |
Output is correct |
17 |
Correct |
4 ms |
4600 KB |
Output is correct |
18 |
Correct |
3 ms |
4588 KB |
Output is correct |
19 |
Correct |
68 ms |
16500 KB |
Output is correct |
20 |
Correct |
60 ms |
15820 KB |
Output is correct |
21 |
Correct |
71 ms |
16284 KB |
Output is correct |
22 |
Correct |
65 ms |
16336 KB |
Output is correct |
23 |
Correct |
83 ms |
16528 KB |
Output is correct |
24 |
Correct |
89 ms |
16532 KB |
Output is correct |
25 |
Correct |
30 ms |
15796 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
91 ms |
18380 KB |
Output is correct |
2 |
Correct |
116 ms |
18344 KB |
Output is correct |
3 |
Correct |
198 ms |
18100 KB |
Output is correct |
4 |
Correct |
129 ms |
18608 KB |
Output is correct |
5 |
Correct |
119 ms |
18640 KB |
Output is correct |
6 |
Correct |
131 ms |
18448 KB |
Output is correct |
7 |
Correct |
129 ms |
18392 KB |
Output is correct |
8 |
Correct |
124 ms |
18660 KB |
Output is correct |
9 |
Correct |
90 ms |
16472 KB |
Output is correct |
10 |
Correct |
118 ms |
18108 KB |
Output is correct |
11 |
Correct |
113 ms |
17904 KB |
Output is correct |
12 |
Correct |
130 ms |
18048 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4540 KB |
Output is correct |
2 |
Correct |
120 ms |
18380 KB |
Output is correct |
3 |
Correct |
106 ms |
18332 KB |
Output is correct |
4 |
Correct |
143 ms |
18212 KB |
Output is correct |
5 |
Correct |
124 ms |
18372 KB |
Output is correct |
6 |
Correct |
130 ms |
18528 KB |
Output is correct |
7 |
Correct |
117 ms |
18508 KB |
Output is correct |
8 |
Correct |
142 ms |
18800 KB |
Output is correct |
9 |
Correct |
171 ms |
18724 KB |
Output is correct |
10 |
Correct |
138 ms |
18640 KB |
Output is correct |
11 |
Correct |
118 ms |
18624 KB |
Output is correct |
12 |
Correct |
64 ms |
18020 KB |
Output is correct |
13 |
Correct |
3 ms |
4436 KB |
Output is correct |
14 |
Correct |
3 ms |
4436 KB |
Output is correct |
15 |
Correct |
4 ms |
4564 KB |
Output is correct |
16 |
Correct |
3 ms |
4564 KB |
Output is correct |
17 |
Correct |
4 ms |
4628 KB |
Output is correct |
18 |
Correct |
4 ms |
4588 KB |
Output is correct |
19 |
Correct |
3 ms |
4564 KB |
Output is correct |
20 |
Correct |
3 ms |
4564 KB |
Output is correct |
21 |
Correct |
4 ms |
4564 KB |
Output is correct |
22 |
Correct |
3 ms |
4436 KB |
Output is correct |
23 |
Correct |
3 ms |
4436 KB |
Output is correct |
24 |
Correct |
5 ms |
4596 KB |
Output is correct |
25 |
Correct |
4 ms |
4564 KB |
Output is correct |
26 |
Correct |
4 ms |
4564 KB |
Output is correct |
27 |
Correct |
6 ms |
4564 KB |
Output is correct |
28 |
Correct |
4 ms |
4564 KB |
Output is correct |
29 |
Correct |
3 ms |
4564 KB |
Output is correct |
30 |
Correct |
3 ms |
4564 KB |
Output is correct |
31 |
Correct |
38 ms |
6408 KB |
Output is correct |
32 |
Correct |
34 ms |
6568 KB |
Output is correct |
33 |
Correct |
34 ms |
6772 KB |
Output is correct |
34 |
Correct |
33 ms |
6812 KB |
Output is correct |
35 |
Correct |
36 ms |
6628 KB |
Output is correct |
36 |
Correct |
33 ms |
6600 KB |
Output is correct |
37 |
Correct |
30 ms |
6348 KB |
Output is correct |
38 |
Correct |
3 ms |
4436 KB |
Output is correct |
39 |
Correct |
2 ms |
4436 KB |
Output is correct |
40 |
Correct |
3 ms |
4620 KB |
Output is correct |
41 |
Correct |
3 ms |
4600 KB |
Output is correct |
42 |
Correct |
4 ms |
4644 KB |
Output is correct |
43 |
Correct |
5 ms |
4588 KB |
Output is correct |
44 |
Correct |
4 ms |
4564 KB |
Output is correct |
45 |
Correct |
4 ms |
4600 KB |
Output is correct |
46 |
Correct |
3 ms |
4588 KB |
Output is correct |
47 |
Correct |
68 ms |
16500 KB |
Output is correct |
48 |
Correct |
60 ms |
15820 KB |
Output is correct |
49 |
Correct |
71 ms |
16284 KB |
Output is correct |
50 |
Correct |
65 ms |
16336 KB |
Output is correct |
51 |
Correct |
83 ms |
16528 KB |
Output is correct |
52 |
Correct |
89 ms |
16532 KB |
Output is correct |
53 |
Correct |
30 ms |
15796 KB |
Output is correct |
54 |
Correct |
91 ms |
18380 KB |
Output is correct |
55 |
Correct |
116 ms |
18344 KB |
Output is correct |
56 |
Correct |
198 ms |
18100 KB |
Output is correct |
57 |
Correct |
129 ms |
18608 KB |
Output is correct |
58 |
Correct |
119 ms |
18640 KB |
Output is correct |
59 |
Correct |
131 ms |
18448 KB |
Output is correct |
60 |
Correct |
129 ms |
18392 KB |
Output is correct |
61 |
Correct |
124 ms |
18660 KB |
Output is correct |
62 |
Correct |
90 ms |
16472 KB |
Output is correct |
63 |
Correct |
118 ms |
18108 KB |
Output is correct |
64 |
Correct |
113 ms |
17904 KB |
Output is correct |
65 |
Correct |
130 ms |
18048 KB |
Output is correct |
66 |
Correct |
4 ms |
4436 KB |
Output is correct |
67 |
Correct |
93 ms |
18192 KB |
Output is correct |
68 |
Correct |
126 ms |
18244 KB |
Output is correct |
69 |
Correct |
150 ms |
18376 KB |
Output is correct |
70 |
Correct |
136 ms |
18564 KB |
Output is correct |
71 |
Correct |
122 ms |
18328 KB |
Output is correct |
72 |
Correct |
118 ms |
18380 KB |
Output is correct |
73 |
Correct |
137 ms |
18696 KB |
Output is correct |
74 |
Correct |
143 ms |
18888 KB |
Output is correct |
75 |
Correct |
141 ms |
18616 KB |
Output is correct |
76 |
Correct |
128 ms |
18764 KB |
Output is correct |
77 |
Correct |
60 ms |
18072 KB |
Output is correct |
78 |
Correct |
3 ms |
4436 KB |
Output is correct |
79 |
Correct |
3 ms |
4564 KB |
Output is correct |
80 |
Correct |
4 ms |
4596 KB |
Output is correct |
81 |
Correct |
3 ms |
4564 KB |
Output is correct |
82 |
Correct |
3 ms |
4564 KB |
Output is correct |
83 |
Correct |
3 ms |
4632 KB |
Output is correct |
84 |
Correct |
3 ms |
4564 KB |
Output is correct |
85 |
Correct |
4 ms |
4564 KB |
Output is correct |
86 |
Correct |
41 ms |
6576 KB |
Output is correct |
87 |
Correct |
34 ms |
6640 KB |
Output is correct |
88 |
Correct |
38 ms |
6704 KB |
Output is correct |
89 |
Correct |
35 ms |
6856 KB |
Output is correct |
90 |
Correct |
32 ms |
6692 KB |
Output is correct |
91 |
Correct |
30 ms |
6604 KB |
Output is correct |
92 |
Correct |
31 ms |
6348 KB |
Output is correct |
93 |
Correct |
68 ms |
16540 KB |
Output is correct |
94 |
Correct |
60 ms |
15740 KB |
Output is correct |
95 |
Correct |
71 ms |
16404 KB |
Output is correct |
96 |
Correct |
65 ms |
16580 KB |
Output is correct |
97 |
Correct |
93 ms |
16500 KB |
Output is correct |
98 |
Correct |
80 ms |
16536 KB |
Output is correct |
99 |
Correct |
30 ms |
15840 KB |
Output is correct |
100 |
Correct |
143 ms |
18380 KB |
Output is correct |
101 |
Correct |
146 ms |
18380 KB |
Output is correct |
102 |
Correct |
143 ms |
18504 KB |
Output is correct |
103 |
Correct |
111 ms |
18100 KB |
Output is correct |
104 |
Correct |
145 ms |
17744 KB |
Output is correct |
105 |
Correct |
133 ms |
18052 KB |
Output is correct |
106 |
Correct |
139 ms |
17704 KB |
Output is correct |
107 |
Correct |
134 ms |
17780 KB |
Output is correct |
108 |
Correct |
165 ms |
18396 KB |
Output is correct |
109 |
Correct |
139 ms |
18336 KB |
Output is correct |
110 |
Correct |
127 ms |
18320 KB |
Output is correct |
111 |
Correct |
146 ms |
18388 KB |
Output is correct |