#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5, K = 20, INF = 1e9 + 5;
int n, q, binlift[N][K], s, e, ans;
struct Event{
int l, r;
bool operator < (const Event &sc) const{
if(r == sc.r) return l < sc.l;
else return r < sc.r;
}
};
Event events[N];
vector<pair<Event, int>> vec;
struct SegTree{
pair<int, int> tree[N * 8];
SegTree(){
fill(tree, tree + N * 8, pair{INF, 0});
}
void update(int pos, pair<int, int> val, int v = 1, int l = 1, int r = 2 * N - 1){
if(l == r) tree[v] = min(tree[v], val);
else{
int m = l + (r - l) / 2;
if(pos <= m) update(pos, val, v * 2, l, m);
else update(pos, val, v * 2 + 1, m + 1, r);
tree[v] = min(tree[v * 2], tree[v * 2 + 1]);
}
}
pair<int, int> query(int l, int r, int v = 1, int tl = 1, int tr = 2 * N - 1){
if(l > r) return {INF, 0};
else if(l == tl && r == tr) return tree[v];
else{
int tm = tl + (tr - tl) / 2;
return min(query(l, min(tm, r), v * 2, tl, tm), query(max(l, tm + 1), r, v * 2 + 1, tm + 1, tr));
}
}
};
SegTree sgt;
map<int, int> cmprs;
int main(){
ios_base::sync_with_stdio(false);cin.tie(NULL);
cin >> n >> q;
for(int i = 1; i <= n; i++){
cin >> events[i].l >> events[i].r;
cmprs[events[i].l] = cmprs[events[i].r] = 1;
}
int tmp = 0;
for(auto &p : cmprs) p.second = ++tmp;
for(int i = 1; i <= n; i++){
events[i].l = cmprs[events[i].l];
events[i].r = cmprs[events[i].r];
vec.push_back({events[i], i});
}
sort(vec.begin(), vec.end());
for(auto p : vec){
Event cur = p.first;
binlift[p.second][0] = sgt.query(cur.l, cur.r).second;
sgt.update(cur.r, {cur.l, p.second});
}
for(int k = 1; k < K; k++){
for(int i = 1; i <= n; i++){
binlift[i][k] = binlift[binlift[i][k - 1]][k - 1];
}
}
while(q--){
cin >> s >> e;
ans = 0;
for(int k = K - 1; k >= 0; k--){
if(binlift[e][k] != 0 && events[binlift[e][k]].l > events[s].r){
e = binlift[e][k];
ans += 1 << k;
}
}
if(s == e) cout << ans << "\n";
else if(events[s].r >= events[e].l && events[s].r <= events[e].r) cout << ans + 1 << "\n";
else{
e = binlift[e][0], ans++;
if(e == 0) cout << "impossible\n";
else{
if(s == e) cout << ans << "\n";
else if(events[s].r >= events[e].l && events[s].r <= events[e].r) cout << ans + 1 << "\n";
else cout << "impossible\n";
}
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
6484 KB |
Output is correct |
2 |
Correct |
221 ms |
21692 KB |
Output is correct |
3 |
Correct |
198 ms |
21700 KB |
Output is correct |
4 |
Correct |
228 ms |
21760 KB |
Output is correct |
5 |
Correct |
242 ms |
22252 KB |
Output is correct |
6 |
Correct |
209 ms |
22728 KB |
Output is correct |
7 |
Correct |
246 ms |
22852 KB |
Output is correct |
8 |
Correct |
240 ms |
26968 KB |
Output is correct |
9 |
Correct |
225 ms |
26944 KB |
Output is correct |
10 |
Correct |
244 ms |
22052 KB |
Output is correct |
11 |
Correct |
226 ms |
24164 KB |
Output is correct |
12 |
Correct |
122 ms |
22236 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
6484 KB |
Output is correct |
2 |
Correct |
4 ms |
6484 KB |
Output is correct |
3 |
Correct |
5 ms |
6740 KB |
Output is correct |
4 |
Correct |
4 ms |
6640 KB |
Output is correct |
5 |
Correct |
5 ms |
6740 KB |
Output is correct |
6 |
Correct |
5 ms |
6740 KB |
Output is correct |
7 |
Correct |
5 ms |
6724 KB |
Output is correct |
8 |
Correct |
5 ms |
6724 KB |
Output is correct |
9 |
Correct |
4 ms |
6600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
6484 KB |
Output is correct |
2 |
Correct |
4 ms |
6484 KB |
Output is correct |
3 |
Correct |
5 ms |
6740 KB |
Output is correct |
4 |
Correct |
4 ms |
6640 KB |
Output is correct |
5 |
Correct |
5 ms |
6740 KB |
Output is correct |
6 |
Correct |
5 ms |
6740 KB |
Output is correct |
7 |
Correct |
5 ms |
6724 KB |
Output is correct |
8 |
Correct |
5 ms |
6724 KB |
Output is correct |
9 |
Correct |
4 ms |
6600 KB |
Output is correct |
10 |
Correct |
4 ms |
6584 KB |
Output is correct |
11 |
Correct |
4 ms |
6484 KB |
Output is correct |
12 |
Correct |
5 ms |
6688 KB |
Output is correct |
13 |
Correct |
6 ms |
6728 KB |
Output is correct |
14 |
Correct |
5 ms |
6740 KB |
Output is correct |
15 |
Correct |
5 ms |
6728 KB |
Output is correct |
16 |
Correct |
5 ms |
6724 KB |
Output is correct |
17 |
Correct |
6 ms |
6740 KB |
Output is correct |
18 |
Correct |
4 ms |
6720 KB |
Output is correct |
19 |
Correct |
44 ms |
8844 KB |
Output is correct |
20 |
Correct |
37 ms |
8908 KB |
Output is correct |
21 |
Correct |
39 ms |
9148 KB |
Output is correct |
22 |
Correct |
32 ms |
9220 KB |
Output is correct |
23 |
Correct |
34 ms |
9288 KB |
Output is correct |
24 |
Correct |
35 ms |
9216 KB |
Output is correct |
25 |
Correct |
35 ms |
8908 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
6484 KB |
Output is correct |
2 |
Correct |
4 ms |
6484 KB |
Output is correct |
3 |
Correct |
5 ms |
6740 KB |
Output is correct |
4 |
Correct |
4 ms |
6640 KB |
Output is correct |
5 |
Correct |
5 ms |
6740 KB |
Output is correct |
6 |
Correct |
5 ms |
6740 KB |
Output is correct |
7 |
Correct |
5 ms |
6724 KB |
Output is correct |
8 |
Correct |
5 ms |
6724 KB |
Output is correct |
9 |
Correct |
4 ms |
6600 KB |
Output is correct |
10 |
Correct |
4 ms |
6484 KB |
Output is correct |
11 |
Correct |
4 ms |
6484 KB |
Output is correct |
12 |
Correct |
5 ms |
6728 KB |
Output is correct |
13 |
Correct |
5 ms |
6740 KB |
Output is correct |
14 |
Correct |
5 ms |
6740 KB |
Output is correct |
15 |
Correct |
6 ms |
6740 KB |
Output is correct |
16 |
Correct |
6 ms |
6812 KB |
Output is correct |
17 |
Correct |
5 ms |
6740 KB |
Output is correct |
18 |
Correct |
5 ms |
6612 KB |
Output is correct |
19 |
Correct |
176 ms |
23120 KB |
Output is correct |
20 |
Correct |
188 ms |
22272 KB |
Output is correct |
21 |
Correct |
169 ms |
22912 KB |
Output is correct |
22 |
Correct |
199 ms |
24768 KB |
Output is correct |
23 |
Correct |
238 ms |
27760 KB |
Output is correct |
24 |
Correct |
245 ms |
27740 KB |
Output is correct |
25 |
Correct |
61 ms |
17684 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
185 ms |
21792 KB |
Output is correct |
2 |
Correct |
189 ms |
21728 KB |
Output is correct |
3 |
Correct |
214 ms |
21752 KB |
Output is correct |
4 |
Correct |
230 ms |
27056 KB |
Output is correct |
5 |
Correct |
222 ms |
22080 KB |
Output is correct |
6 |
Correct |
356 ms |
26600 KB |
Output is correct |
7 |
Correct |
282 ms |
26520 KB |
Output is correct |
8 |
Correct |
306 ms |
26764 KB |
Output is correct |
9 |
Correct |
228 ms |
25868 KB |
Output is correct |
10 |
Correct |
284 ms |
26224 KB |
Output is correct |
11 |
Correct |
274 ms |
26048 KB |
Output is correct |
12 |
Correct |
338 ms |
26176 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
6484 KB |
Output is correct |
2 |
Correct |
221 ms |
21692 KB |
Output is correct |
3 |
Correct |
198 ms |
21700 KB |
Output is correct |
4 |
Correct |
228 ms |
21760 KB |
Output is correct |
5 |
Correct |
242 ms |
22252 KB |
Output is correct |
6 |
Correct |
209 ms |
22728 KB |
Output is correct |
7 |
Correct |
246 ms |
22852 KB |
Output is correct |
8 |
Correct |
240 ms |
26968 KB |
Output is correct |
9 |
Correct |
225 ms |
26944 KB |
Output is correct |
10 |
Correct |
244 ms |
22052 KB |
Output is correct |
11 |
Correct |
226 ms |
24164 KB |
Output is correct |
12 |
Correct |
122 ms |
22236 KB |
Output is correct |
13 |
Correct |
4 ms |
6484 KB |
Output is correct |
14 |
Correct |
4 ms |
6484 KB |
Output is correct |
15 |
Correct |
5 ms |
6740 KB |
Output is correct |
16 |
Correct |
4 ms |
6640 KB |
Output is correct |
17 |
Correct |
5 ms |
6740 KB |
Output is correct |
18 |
Correct |
5 ms |
6740 KB |
Output is correct |
19 |
Correct |
5 ms |
6724 KB |
Output is correct |
20 |
Correct |
5 ms |
6724 KB |
Output is correct |
21 |
Correct |
4 ms |
6600 KB |
Output is correct |
22 |
Correct |
4 ms |
6584 KB |
Output is correct |
23 |
Correct |
4 ms |
6484 KB |
Output is correct |
24 |
Correct |
5 ms |
6688 KB |
Output is correct |
25 |
Correct |
6 ms |
6728 KB |
Output is correct |
26 |
Correct |
5 ms |
6740 KB |
Output is correct |
27 |
Correct |
5 ms |
6728 KB |
Output is correct |
28 |
Correct |
5 ms |
6724 KB |
Output is correct |
29 |
Correct |
6 ms |
6740 KB |
Output is correct |
30 |
Correct |
4 ms |
6720 KB |
Output is correct |
31 |
Correct |
44 ms |
8844 KB |
Output is correct |
32 |
Correct |
37 ms |
8908 KB |
Output is correct |
33 |
Correct |
39 ms |
9148 KB |
Output is correct |
34 |
Correct |
32 ms |
9220 KB |
Output is correct |
35 |
Correct |
34 ms |
9288 KB |
Output is correct |
36 |
Correct |
35 ms |
9216 KB |
Output is correct |
37 |
Correct |
35 ms |
8908 KB |
Output is correct |
38 |
Correct |
4 ms |
6484 KB |
Output is correct |
39 |
Correct |
4 ms |
6484 KB |
Output is correct |
40 |
Correct |
5 ms |
6728 KB |
Output is correct |
41 |
Correct |
5 ms |
6740 KB |
Output is correct |
42 |
Correct |
5 ms |
6740 KB |
Output is correct |
43 |
Correct |
6 ms |
6740 KB |
Output is correct |
44 |
Correct |
6 ms |
6812 KB |
Output is correct |
45 |
Correct |
5 ms |
6740 KB |
Output is correct |
46 |
Correct |
5 ms |
6612 KB |
Output is correct |
47 |
Correct |
176 ms |
23120 KB |
Output is correct |
48 |
Correct |
188 ms |
22272 KB |
Output is correct |
49 |
Correct |
169 ms |
22912 KB |
Output is correct |
50 |
Correct |
199 ms |
24768 KB |
Output is correct |
51 |
Correct |
238 ms |
27760 KB |
Output is correct |
52 |
Correct |
245 ms |
27740 KB |
Output is correct |
53 |
Correct |
61 ms |
17684 KB |
Output is correct |
54 |
Correct |
185 ms |
21792 KB |
Output is correct |
55 |
Correct |
189 ms |
21728 KB |
Output is correct |
56 |
Correct |
214 ms |
21752 KB |
Output is correct |
57 |
Correct |
230 ms |
27056 KB |
Output is correct |
58 |
Correct |
222 ms |
22080 KB |
Output is correct |
59 |
Correct |
356 ms |
26600 KB |
Output is correct |
60 |
Correct |
282 ms |
26520 KB |
Output is correct |
61 |
Correct |
306 ms |
26764 KB |
Output is correct |
62 |
Correct |
228 ms |
25868 KB |
Output is correct |
63 |
Correct |
284 ms |
26224 KB |
Output is correct |
64 |
Correct |
274 ms |
26048 KB |
Output is correct |
65 |
Correct |
338 ms |
26176 KB |
Output is correct |
66 |
Correct |
4 ms |
6484 KB |
Output is correct |
67 |
Correct |
204 ms |
24856 KB |
Output is correct |
68 |
Correct |
243 ms |
24892 KB |
Output is correct |
69 |
Correct |
216 ms |
24836 KB |
Output is correct |
70 |
Correct |
190 ms |
25392 KB |
Output is correct |
71 |
Correct |
204 ms |
25812 KB |
Output is correct |
72 |
Correct |
224 ms |
25944 KB |
Output is correct |
73 |
Correct |
217 ms |
30020 KB |
Output is correct |
74 |
Correct |
248 ms |
29996 KB |
Output is correct |
75 |
Correct |
209 ms |
25156 KB |
Output is correct |
76 |
Correct |
235 ms |
27176 KB |
Output is correct |
77 |
Correct |
152 ms |
24628 KB |
Output is correct |
78 |
Correct |
4 ms |
6484 KB |
Output is correct |
79 |
Correct |
5 ms |
6740 KB |
Output is correct |
80 |
Correct |
5 ms |
6740 KB |
Output is correct |
81 |
Correct |
4 ms |
6740 KB |
Output is correct |
82 |
Correct |
4 ms |
6740 KB |
Output is correct |
83 |
Correct |
6 ms |
6740 KB |
Output is correct |
84 |
Correct |
5 ms |
6724 KB |
Output is correct |
85 |
Correct |
5 ms |
6612 KB |
Output is correct |
86 |
Correct |
36 ms |
8924 KB |
Output is correct |
87 |
Correct |
35 ms |
8840 KB |
Output is correct |
88 |
Correct |
41 ms |
9248 KB |
Output is correct |
89 |
Correct |
35 ms |
9132 KB |
Output is correct |
90 |
Correct |
34 ms |
9312 KB |
Output is correct |
91 |
Correct |
35 ms |
9240 KB |
Output is correct |
92 |
Correct |
43 ms |
8944 KB |
Output is correct |
93 |
Correct |
190 ms |
23172 KB |
Output is correct |
94 |
Correct |
177 ms |
22280 KB |
Output is correct |
95 |
Correct |
178 ms |
22988 KB |
Output is correct |
96 |
Correct |
189 ms |
24820 KB |
Output is correct |
97 |
Correct |
257 ms |
27708 KB |
Output is correct |
98 |
Correct |
221 ms |
27716 KB |
Output is correct |
99 |
Correct |
76 ms |
17756 KB |
Output is correct |
100 |
Correct |
337 ms |
29732 KB |
Output is correct |
101 |
Correct |
320 ms |
29688 KB |
Output is correct |
102 |
Correct |
314 ms |
29868 KB |
Output is correct |
103 |
Correct |
302 ms |
29324 KB |
Output is correct |
104 |
Correct |
325 ms |
29224 KB |
Output is correct |
105 |
Correct |
290 ms |
29340 KB |
Output is correct |
106 |
Correct |
241 ms |
23044 KB |
Output is correct |
107 |
Correct |
237 ms |
24256 KB |
Output is correct |
108 |
Correct |
229 ms |
24896 KB |
Output is correct |
109 |
Correct |
224 ms |
24928 KB |
Output is correct |
110 |
Correct |
284 ms |
29624 KB |
Output is correct |
111 |
Correct |
328 ms |
29556 KB |
Output is correct |