#include <bits/stdc++.h>
#define iter(a) a.begin(), a.end()
#define lsort(a) sort(iter(a))
#define gsort(a) sort(iter(a), greater<>())
#define eb emplace_back
#define pob pop_back()
#define ef emplace_front
#define pof pop_front()
#define mp make_pair
#define F first
#define S second
#define uni(a) a.resize(unique(iter(a)) - a.begin())
#define printv(a, b) { \
for(auto pv : a) b << pv << " "; \
b << "\n"; \
}
using namespace std;
typedef long long ll;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
struct SparseTable{
vector<vector<pii>> st;
int n;
SparseTable(vector<pii>& a){
n = a.size();
st.resize(20, vector<pii>(n));
st[0] = a;
for(int i = 1; i < 20; i++){
for(int j = 0; j + (1 << i) - 1 < n; j++){
st[i][j] = min(st[i - 1][j], st[i - 1][j + (1 << (i - 1))]);
}
}
}
pii query(int l, int r){
int lg = __lg(r - l + 1);
return min(st[lg][l], st[lg][r - (1 << lg) + 1]);
}
};
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, q;
cin >> n >> q;
vector<int> l(n + 1), r(n + 1);
vector<vector<int>> anc(20, vector<int>(n + 1));
for(int i = 1; i <= n; i++){
cin >> l[i] >> r[i];
}
vector<int> dr = r;
lsort(dr);
uni(dr);
int sz = dr.size();
vector<pii> mn(sz, mp(INT_MAX, -1));
for(int i = 1; i <= n; i++){
int pos = lower_bound(iter(dr), r[i]) - dr.begin();
mn[pos] = min(mn[pos], mp(l[i], i));
}
SparseTable st(mn);
for(int i = 1; i <= n; i++){
int lp = lower_bound(iter(dr), l[i]) - dr.begin();
int rp = lower_bound(iter(dr), r[i]) - dr.begin();
anc[0][i] = st.query(lp, rp).S;
if(anc[0][i] == i) anc[0][i] = -1;
}
for(int i = 1; i < 20; i++){
for(int j = 1; j <= n; j++){
if(anc[i - 1][j] == -1) anc[i][j] = -1;
else anc[i][j] = anc[i - 1][anc[i - 1][j]];
}
}
while(q--){
int s, e;
cin >> s >> e;
if(s == e){
cout << "0\n";
continue;
}
if(r[s] > r[e]){
cout << "impossible\n";
continue;
}
int ans = 0;
int now = e;
for(int i = 19; i >= 0; i--){
if(anc[i][now] == -1) continue;
if(l[anc[i][now]] <= r[s]) continue;
now = anc[i][now];
ans += 1 << i;
}
if(r[s] < l[now] && anc[0][now] != -1) ans++, now = anc[0][now];
if(r[s] < l[now]) cout << "impossible\n";
else cout << ans + 1 << "\n";
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
101 ms |
26512 KB |
Output is correct |
3 |
Correct |
127 ms |
26512 KB |
Output is correct |
4 |
Correct |
198 ms |
26512 KB |
Output is correct |
5 |
Correct |
165 ms |
26632 KB |
Output is correct |
6 |
Correct |
150 ms |
26508 KB |
Output is correct |
7 |
Correct |
127 ms |
26516 KB |
Output is correct |
8 |
Correct |
121 ms |
26936 KB |
Output is correct |
9 |
Correct |
110 ms |
26868 KB |
Output is correct |
10 |
Correct |
144 ms |
26756 KB |
Output is correct |
11 |
Correct |
136 ms |
26740 KB |
Output is correct |
12 |
Correct |
58 ms |
19020 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
2 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
2 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
2 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
31 ms |
2112 KB |
Output is correct |
20 |
Correct |
46 ms |
2088 KB |
Output is correct |
21 |
Correct |
37 ms |
2360 KB |
Output is correct |
22 |
Correct |
26 ms |
1892 KB |
Output is correct |
23 |
Correct |
28 ms |
2256 KB |
Output is correct |
24 |
Correct |
23 ms |
2224 KB |
Output is correct |
25 |
Correct |
36 ms |
1840 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
2 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
2 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
82 ms |
26520 KB |
Output is correct |
20 |
Correct |
69 ms |
26508 KB |
Output is correct |
21 |
Correct |
82 ms |
17964 KB |
Output is correct |
22 |
Correct |
91 ms |
26624 KB |
Output is correct |
23 |
Correct |
79 ms |
26612 KB |
Output is correct |
24 |
Correct |
89 ms |
26624 KB |
Output is correct |
25 |
Correct |
34 ms |
9300 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
110 ms |
26516 KB |
Output is correct |
2 |
Correct |
122 ms |
26564 KB |
Output is correct |
3 |
Correct |
182 ms |
26636 KB |
Output is correct |
4 |
Correct |
111 ms |
26868 KB |
Output is correct |
5 |
Correct |
175 ms |
26740 KB |
Output is correct |
6 |
Correct |
136 ms |
26620 KB |
Output is correct |
7 |
Correct |
138 ms |
26552 KB |
Output is correct |
8 |
Correct |
129 ms |
26608 KB |
Output is correct |
9 |
Correct |
80 ms |
26512 KB |
Output is correct |
10 |
Correct |
139 ms |
26664 KB |
Output is correct |
11 |
Correct |
124 ms |
26628 KB |
Output is correct |
12 |
Correct |
142 ms |
26612 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
101 ms |
26512 KB |
Output is correct |
3 |
Correct |
127 ms |
26512 KB |
Output is correct |
4 |
Correct |
198 ms |
26512 KB |
Output is correct |
5 |
Correct |
165 ms |
26632 KB |
Output is correct |
6 |
Correct |
150 ms |
26508 KB |
Output is correct |
7 |
Correct |
127 ms |
26516 KB |
Output is correct |
8 |
Correct |
121 ms |
26936 KB |
Output is correct |
9 |
Correct |
110 ms |
26868 KB |
Output is correct |
10 |
Correct |
144 ms |
26756 KB |
Output is correct |
11 |
Correct |
136 ms |
26740 KB |
Output is correct |
12 |
Correct |
58 ms |
19020 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
2 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
0 ms |
212 KB |
Output is correct |
24 |
Correct |
2 ms |
468 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
1 ms |
468 KB |
Output is correct |
27 |
Correct |
1 ms |
468 KB |
Output is correct |
28 |
Correct |
1 ms |
468 KB |
Output is correct |
29 |
Correct |
1 ms |
468 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
31 ms |
2112 KB |
Output is correct |
32 |
Correct |
46 ms |
2088 KB |
Output is correct |
33 |
Correct |
37 ms |
2360 KB |
Output is correct |
34 |
Correct |
26 ms |
1892 KB |
Output is correct |
35 |
Correct |
28 ms |
2256 KB |
Output is correct |
36 |
Correct |
23 ms |
2224 KB |
Output is correct |
37 |
Correct |
36 ms |
1840 KB |
Output is correct |
38 |
Correct |
0 ms |
212 KB |
Output is correct |
39 |
Correct |
0 ms |
212 KB |
Output is correct |
40 |
Correct |
1 ms |
468 KB |
Output is correct |
41 |
Correct |
1 ms |
468 KB |
Output is correct |
42 |
Correct |
1 ms |
468 KB |
Output is correct |
43 |
Correct |
1 ms |
468 KB |
Output is correct |
44 |
Correct |
2 ms |
468 KB |
Output is correct |
45 |
Correct |
1 ms |
468 KB |
Output is correct |
46 |
Correct |
1 ms |
340 KB |
Output is correct |
47 |
Correct |
82 ms |
26520 KB |
Output is correct |
48 |
Correct |
69 ms |
26508 KB |
Output is correct |
49 |
Correct |
82 ms |
17964 KB |
Output is correct |
50 |
Correct |
91 ms |
26624 KB |
Output is correct |
51 |
Correct |
79 ms |
26612 KB |
Output is correct |
52 |
Correct |
89 ms |
26624 KB |
Output is correct |
53 |
Correct |
34 ms |
9300 KB |
Output is correct |
54 |
Correct |
110 ms |
26516 KB |
Output is correct |
55 |
Correct |
122 ms |
26564 KB |
Output is correct |
56 |
Correct |
182 ms |
26636 KB |
Output is correct |
57 |
Correct |
111 ms |
26868 KB |
Output is correct |
58 |
Correct |
175 ms |
26740 KB |
Output is correct |
59 |
Correct |
136 ms |
26620 KB |
Output is correct |
60 |
Correct |
138 ms |
26552 KB |
Output is correct |
61 |
Correct |
129 ms |
26608 KB |
Output is correct |
62 |
Correct |
80 ms |
26512 KB |
Output is correct |
63 |
Correct |
139 ms |
26664 KB |
Output is correct |
64 |
Correct |
124 ms |
26628 KB |
Output is correct |
65 |
Correct |
142 ms |
26612 KB |
Output is correct |
66 |
Correct |
1 ms |
212 KB |
Output is correct |
67 |
Correct |
104 ms |
26616 KB |
Output is correct |
68 |
Correct |
148 ms |
26512 KB |
Output is correct |
69 |
Correct |
181 ms |
26564 KB |
Output is correct |
70 |
Correct |
147 ms |
26512 KB |
Output is correct |
71 |
Correct |
146 ms |
26516 KB |
Output is correct |
72 |
Correct |
130 ms |
26592 KB |
Output is correct |
73 |
Correct |
113 ms |
26860 KB |
Output is correct |
74 |
Correct |
108 ms |
26880 KB |
Output is correct |
75 |
Correct |
156 ms |
26704 KB |
Output is correct |
76 |
Correct |
145 ms |
26732 KB |
Output is correct |
77 |
Correct |
71 ms |
18980 KB |
Output is correct |
78 |
Correct |
0 ms |
212 KB |
Output is correct |
79 |
Correct |
1 ms |
468 KB |
Output is correct |
80 |
Correct |
1 ms |
468 KB |
Output is correct |
81 |
Correct |
1 ms |
468 KB |
Output is correct |
82 |
Correct |
1 ms |
468 KB |
Output is correct |
83 |
Correct |
1 ms |
468 KB |
Output is correct |
84 |
Correct |
2 ms |
468 KB |
Output is correct |
85 |
Correct |
1 ms |
348 KB |
Output is correct |
86 |
Correct |
33 ms |
2072 KB |
Output is correct |
87 |
Correct |
33 ms |
2004 KB |
Output is correct |
88 |
Correct |
36 ms |
2380 KB |
Output is correct |
89 |
Correct |
29 ms |
1876 KB |
Output is correct |
90 |
Correct |
29 ms |
2168 KB |
Output is correct |
91 |
Correct |
25 ms |
2248 KB |
Output is correct |
92 |
Correct |
27 ms |
1876 KB |
Output is correct |
93 |
Correct |
76 ms |
26628 KB |
Output is correct |
94 |
Correct |
73 ms |
26512 KB |
Output is correct |
95 |
Correct |
68 ms |
17968 KB |
Output is correct |
96 |
Correct |
78 ms |
26620 KB |
Output is correct |
97 |
Correct |
65 ms |
26508 KB |
Output is correct |
98 |
Correct |
80 ms |
26560 KB |
Output is correct |
99 |
Correct |
27 ms |
9300 KB |
Output is correct |
100 |
Correct |
167 ms |
26500 KB |
Output is correct |
101 |
Correct |
127 ms |
26532 KB |
Output is correct |
102 |
Correct |
113 ms |
26620 KB |
Output is correct |
103 |
Correct |
151 ms |
26516 KB |
Output is correct |
104 |
Correct |
117 ms |
26500 KB |
Output is correct |
105 |
Correct |
126 ms |
26504 KB |
Output is correct |
106 |
Correct |
143 ms |
22488 KB |
Output is correct |
107 |
Correct |
164 ms |
26500 KB |
Output is correct |
108 |
Correct |
102 ms |
18700 KB |
Output is correct |
109 |
Correct |
122 ms |
18448 KB |
Output is correct |
110 |
Correct |
119 ms |
26620 KB |
Output is correct |
111 |
Correct |
115 ms |
26516 KB |
Output is correct |