#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define all(x) (x).begin(), (x).end()
#define MP make_pair
const int N = 100500;
template <typename T>
struct SegmentTree{
int n = 0;
vector<T> tree;
T neutral_element = make_pair(1e9, -1); // e.g. 0 for sum, INF for min ...
SegmentTree(){};
SegmentTree(int _n){
n = _n;
tree.assign(n * 4 + 5, neutral_element);
}
inline T combine(T lf, T rg){
return min(lf, rg); // change it
}
inline void build(int v, int tl, int tr, const vector<T> &a){
if (tl == tr){
tree[v] = a[tl];
return;
}
int tm = (tl + tr) >> 1;
build(v << 1, tl, tm, a);
build(v << 1 | 1, tm + 1, tr, a);
tree[v] = combine(tree[v << 1], tree[v << 1 | 1]);
}
inline void init(const vector<T> &a){
n = (int)a.size();
tree.assign(n * 4 + 5, neutral_element);
build(1, 0, n - 1, a);
}
inline void update(int v, int tl, int tr, int pos, T val){
if (tl == tr){
tree[v] = min(tree[v], val); // pay attention!
return;
}
int tm = (tl + tr) >> 1;
if (pos <= tm){
update(v << 1, tl, tm, pos, val);
} else {
update(v << 1 | 1, tm + 1, tr, pos, val);
}
tree[v] = combine(tree[v << 1], tree[v << 1 | 1]);
}
inline void update(int pos, T val){
update(1, 0, n - 1, pos, val);
}
inline T get(int v, int tl, int tr, int l, int r){
if (l <= tl && tr <= r) return tree[v];
if (tl > r || tr < l) return neutral_element;
int tm = (tl + tr) >> 1;
return combine(get(v << 1, tl, tm, l, r), get(v << 1 | 1, tm + 1, tr, l, r));
}
inline T get(int l, int r){
return get(1, 0, n - 1, l, r);
}
};
int f[N][18];
int l[N], r[N];
bool can_switch(int i, int j){
return l[j] <= r[i] && r[i] <= r[j];
}
void solve(){
int n, q;
cin >> n >> q;
map<int, int> mp;
for (int i = 0; i < n; i++){
cin >> l[i] >> r[i];
mp[l[i]], mp[r[i]];
}
int z = 0;
for (auto &now : mp) now.second = z++;
SegmentTree<pair<int, int>> st(n * 2);
for (int i = 0; i < n; i++) {
st.update(mp[r[i]], make_pair(mp[l[i]], i));
}
for (int i = 0; i < n; i++){
int j = st.get(mp[l[i]], mp[r[i]]).second;
f[i][0] = (j != -1 && l[j] < l[i] ? j : -1);
}
for (int j = 1; j < 18; j++){
for (int i = 0; i < n; i++){
int mid = f[i][j - 1];
f[i][j] = (mid != -1 ? f[mid][j - 1] : -1);
}
}
while (q--){
int x, y;
cin >> x >> y;
--x, --y;
if (x == y){
cout << 0 << endl;
continue;
}
if (can_switch(x, y)){
cout << 1 << endl;
continue;
}
int cnt = 2;
for (int i = 17; i >= 0; i--){
int to = f[y][i];
if (to != -1 && l[to] > r[x]){
cnt += 1 << i;
y = to;
}
}
y = f[y][0];
if (y != -1 && can_switch(x, y)){
cout << cnt << endl;
} else {
cout << "impossible" << endl;
}
}
}
int main() {
ios_base::sync_with_stdio(false);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
solve();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
342 ms |
19768 KB |
Output is correct |
3 |
Correct |
406 ms |
19904 KB |
Output is correct |
4 |
Correct |
434 ms |
19844 KB |
Output is correct |
5 |
Correct |
401 ms |
20312 KB |
Output is correct |
6 |
Correct |
457 ms |
20844 KB |
Output is correct |
7 |
Correct |
393 ms |
21060 KB |
Output is correct |
8 |
Correct |
429 ms |
25068 KB |
Output is correct |
9 |
Correct |
423 ms |
25096 KB |
Output is correct |
10 |
Correct |
483 ms |
20216 KB |
Output is correct |
11 |
Correct |
452 ms |
24812 KB |
Output is correct |
12 |
Correct |
289 ms |
22472 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
328 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
4 |
Correct |
2 ms |
464 KB |
Output is correct |
5 |
Correct |
2 ms |
456 KB |
Output is correct |
6 |
Correct |
2 ms |
468 KB |
Output is correct |
7 |
Correct |
3 ms |
564 KB |
Output is correct |
8 |
Correct |
2 ms |
596 KB |
Output is correct |
9 |
Correct |
1 ms |
480 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
328 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
4 |
Correct |
2 ms |
464 KB |
Output is correct |
5 |
Correct |
2 ms |
456 KB |
Output is correct |
6 |
Correct |
2 ms |
468 KB |
Output is correct |
7 |
Correct |
3 ms |
564 KB |
Output is correct |
8 |
Correct |
2 ms |
596 KB |
Output is correct |
9 |
Correct |
1 ms |
480 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
2 ms |
548 KB |
Output is correct |
13 |
Correct |
2 ms |
468 KB |
Output is correct |
14 |
Correct |
2 ms |
468 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
2 ms |
468 KB |
Output is correct |
17 |
Correct |
2 ms |
516 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
148 ms |
1864 KB |
Output is correct |
20 |
Correct |
164 ms |
2168 KB |
Output is correct |
21 |
Correct |
151 ms |
3020 KB |
Output is correct |
22 |
Correct |
151 ms |
3064 KB |
Output is correct |
23 |
Correct |
160 ms |
3092 KB |
Output is correct |
24 |
Correct |
142 ms |
3128 KB |
Output is correct |
25 |
Correct |
149 ms |
2724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
328 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
4 |
Correct |
2 ms |
464 KB |
Output is correct |
5 |
Correct |
2 ms |
456 KB |
Output is correct |
6 |
Correct |
2 ms |
468 KB |
Output is correct |
7 |
Correct |
3 ms |
564 KB |
Output is correct |
8 |
Correct |
2 ms |
596 KB |
Output is correct |
9 |
Correct |
1 ms |
480 KB |
Output is correct |
10 |
Correct |
1 ms |
328 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
3 ms |
468 KB |
Output is correct |
13 |
Correct |
3 ms |
468 KB |
Output is correct |
14 |
Correct |
2 ms |
468 KB |
Output is correct |
15 |
Correct |
2 ms |
452 KB |
Output is correct |
16 |
Correct |
3 ms |
596 KB |
Output is correct |
17 |
Correct |
2 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
225 ms |
19220 KB |
Output is correct |
20 |
Correct |
256 ms |
19160 KB |
Output is correct |
21 |
Correct |
242 ms |
19104 KB |
Output is correct |
22 |
Correct |
239 ms |
20916 KB |
Output is correct |
23 |
Correct |
315 ms |
23964 KB |
Output is correct |
24 |
Correct |
426 ms |
23876 KB |
Output is correct |
25 |
Correct |
61 ms |
14488 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
364 ms |
19836 KB |
Output is correct |
2 |
Correct |
360 ms |
19896 KB |
Output is correct |
3 |
Correct |
453 ms |
19852 KB |
Output is correct |
4 |
Correct |
412 ms |
24928 KB |
Output is correct |
5 |
Correct |
404 ms |
20264 KB |
Output is correct |
6 |
Correct |
591 ms |
27628 KB |
Output is correct |
7 |
Correct |
531 ms |
27508 KB |
Output is correct |
8 |
Correct |
547 ms |
27752 KB |
Output is correct |
9 |
Correct |
308 ms |
25684 KB |
Output is correct |
10 |
Correct |
515 ms |
27220 KB |
Output is correct |
11 |
Correct |
595 ms |
27092 KB |
Output is correct |
12 |
Correct |
522 ms |
27276 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
342 ms |
19768 KB |
Output is correct |
3 |
Correct |
406 ms |
19904 KB |
Output is correct |
4 |
Correct |
434 ms |
19844 KB |
Output is correct |
5 |
Correct |
401 ms |
20312 KB |
Output is correct |
6 |
Correct |
457 ms |
20844 KB |
Output is correct |
7 |
Correct |
393 ms |
21060 KB |
Output is correct |
8 |
Correct |
429 ms |
25068 KB |
Output is correct |
9 |
Correct |
423 ms |
25096 KB |
Output is correct |
10 |
Correct |
483 ms |
20216 KB |
Output is correct |
11 |
Correct |
452 ms |
24812 KB |
Output is correct |
12 |
Correct |
289 ms |
22472 KB |
Output is correct |
13 |
Correct |
1 ms |
328 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
2 ms |
464 KB |
Output is correct |
17 |
Correct |
2 ms |
456 KB |
Output is correct |
18 |
Correct |
2 ms |
468 KB |
Output is correct |
19 |
Correct |
3 ms |
564 KB |
Output is correct |
20 |
Correct |
2 ms |
596 KB |
Output is correct |
21 |
Correct |
1 ms |
480 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
2 ms |
548 KB |
Output is correct |
25 |
Correct |
2 ms |
468 KB |
Output is correct |
26 |
Correct |
2 ms |
468 KB |
Output is correct |
27 |
Correct |
2 ms |
468 KB |
Output is correct |
28 |
Correct |
2 ms |
468 KB |
Output is correct |
29 |
Correct |
2 ms |
516 KB |
Output is correct |
30 |
Correct |
1 ms |
468 KB |
Output is correct |
31 |
Correct |
148 ms |
1864 KB |
Output is correct |
32 |
Correct |
164 ms |
2168 KB |
Output is correct |
33 |
Correct |
151 ms |
3020 KB |
Output is correct |
34 |
Correct |
151 ms |
3064 KB |
Output is correct |
35 |
Correct |
160 ms |
3092 KB |
Output is correct |
36 |
Correct |
142 ms |
3128 KB |
Output is correct |
37 |
Correct |
149 ms |
2724 KB |
Output is correct |
38 |
Correct |
1 ms |
328 KB |
Output is correct |
39 |
Correct |
1 ms |
212 KB |
Output is correct |
40 |
Correct |
3 ms |
468 KB |
Output is correct |
41 |
Correct |
3 ms |
468 KB |
Output is correct |
42 |
Correct |
2 ms |
468 KB |
Output is correct |
43 |
Correct |
2 ms |
452 KB |
Output is correct |
44 |
Correct |
3 ms |
596 KB |
Output is correct |
45 |
Correct |
2 ms |
468 KB |
Output is correct |
46 |
Correct |
1 ms |
468 KB |
Output is correct |
47 |
Correct |
225 ms |
19220 KB |
Output is correct |
48 |
Correct |
256 ms |
19160 KB |
Output is correct |
49 |
Correct |
242 ms |
19104 KB |
Output is correct |
50 |
Correct |
239 ms |
20916 KB |
Output is correct |
51 |
Correct |
315 ms |
23964 KB |
Output is correct |
52 |
Correct |
426 ms |
23876 KB |
Output is correct |
53 |
Correct |
61 ms |
14488 KB |
Output is correct |
54 |
Correct |
364 ms |
19836 KB |
Output is correct |
55 |
Correct |
360 ms |
19896 KB |
Output is correct |
56 |
Correct |
453 ms |
19852 KB |
Output is correct |
57 |
Correct |
412 ms |
24928 KB |
Output is correct |
58 |
Correct |
404 ms |
20264 KB |
Output is correct |
59 |
Correct |
591 ms |
27628 KB |
Output is correct |
60 |
Correct |
531 ms |
27508 KB |
Output is correct |
61 |
Correct |
547 ms |
27752 KB |
Output is correct |
62 |
Correct |
308 ms |
25684 KB |
Output is correct |
63 |
Correct |
515 ms |
27220 KB |
Output is correct |
64 |
Correct |
595 ms |
27092 KB |
Output is correct |
65 |
Correct |
522 ms |
27276 KB |
Output is correct |
66 |
Correct |
1 ms |
212 KB |
Output is correct |
67 |
Correct |
363 ms |
22772 KB |
Output is correct |
68 |
Correct |
388 ms |
22808 KB |
Output is correct |
69 |
Correct |
375 ms |
22768 KB |
Output is correct |
70 |
Correct |
410 ms |
23256 KB |
Output is correct |
71 |
Correct |
418 ms |
23752 KB |
Output is correct |
72 |
Correct |
382 ms |
23848 KB |
Output is correct |
73 |
Correct |
422 ms |
27984 KB |
Output is correct |
74 |
Correct |
388 ms |
27948 KB |
Output is correct |
75 |
Correct |
358 ms |
23212 KB |
Output is correct |
76 |
Correct |
411 ms |
25084 KB |
Output is correct |
77 |
Correct |
244 ms |
22504 KB |
Output is correct |
78 |
Correct |
1 ms |
340 KB |
Output is correct |
79 |
Correct |
2 ms |
468 KB |
Output is correct |
80 |
Correct |
2 ms |
516 KB |
Output is correct |
81 |
Correct |
2 ms |
468 KB |
Output is correct |
82 |
Correct |
2 ms |
460 KB |
Output is correct |
83 |
Correct |
2 ms |
600 KB |
Output is correct |
84 |
Correct |
2 ms |
596 KB |
Output is correct |
85 |
Correct |
1 ms |
468 KB |
Output is correct |
86 |
Correct |
154 ms |
2824 KB |
Output is correct |
87 |
Correct |
154 ms |
2804 KB |
Output is correct |
88 |
Correct |
151 ms |
2916 KB |
Output is correct |
89 |
Correct |
158 ms |
3016 KB |
Output is correct |
90 |
Correct |
170 ms |
3180 KB |
Output is correct |
91 |
Correct |
163 ms |
3176 KB |
Output is correct |
92 |
Correct |
145 ms |
2828 KB |
Output is correct |
93 |
Correct |
208 ms |
21196 KB |
Output is correct |
94 |
Correct |
220 ms |
20236 KB |
Output is correct |
95 |
Correct |
248 ms |
20940 KB |
Output is correct |
96 |
Correct |
235 ms |
22732 KB |
Output is correct |
97 |
Correct |
291 ms |
25688 KB |
Output is correct |
98 |
Correct |
414 ms |
25660 KB |
Output is correct |
99 |
Correct |
64 ms |
15568 KB |
Output is correct |
100 |
Correct |
602 ms |
27604 KB |
Output is correct |
101 |
Correct |
485 ms |
27596 KB |
Output is correct |
102 |
Correct |
470 ms |
27768 KB |
Output is correct |
103 |
Correct |
497 ms |
27212 KB |
Output is correct |
104 |
Correct |
469 ms |
26964 KB |
Output is correct |
105 |
Correct |
479 ms |
27160 KB |
Output is correct |
106 |
Correct |
331 ms |
20984 KB |
Output is correct |
107 |
Correct |
388 ms |
22196 KB |
Output is correct |
108 |
Correct |
364 ms |
22860 KB |
Output is correct |
109 |
Correct |
369 ms |
22860 KB |
Output is correct |
110 |
Correct |
612 ms |
27456 KB |
Output is correct |
111 |
Correct |
485 ms |
27500 KB |
Output is correct |