#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 2;
const int OFF = 1 << 19;
const int LOG = 17;
int n, q, s[MAXN], e[MAXN], bl[MAXN][LOG];
set<pair<int, int> > st;
vector<pair<int, bool> > v[MAXN];
pair<int, int> tour[OFF];
void upd(int x, int l, int r, int i, pair<int, int> val, bool keep = 0){
if(i < l || i > r) return ;
if(l == r){
if(keep) tour[x] = min(tour[x], val);
else tour[x] = val;
return ;
}
int mid = (l + r) >> 1;
upd(x * 2 + 1, l, mid, i, val, keep);
upd(x * 2 + 2, mid + 1, r, i, val, keep);
tour[x] = min(tour[x * 2 + 1], tour[x * 2 + 2]);
}
pair<int, int> f(int x, int l, int r, int ql, int qr){
if(ql > r || l > qr) return {1e9, 0};
if(ql <= l && r <= qr) return tour[x];
int mid = (l + r) >> 1;
return min(f(x * 2 + 1, l, mid, ql, qr), f(x * 2 + 2, mid + 1, r, ql, qr));
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(0);
cin >> n >> q;
unordered_map<int, int> um;
vector<int> cmp;
for(int i = 0; i < n; ++i){
cin >> s[i] >> e[i];
cmp.push_back(s[i]);
cmp.push_back(e[i]);
}
sort(cmp.begin(), cmp.end());
cmp.erase(unique(cmp.begin(), cmp.end()), cmp.end());
for(int i = 0; i < (int)cmp.size(); ++i){
um[cmp[i]] = i;
}
for(int i = 0; i < n; ++i){
s[i] = um[s[i]]; e[i] = um[e[i]];
v[s[i]].push_back({i, 0});
v[e[i]].push_back({i, 1});
}
for(int i = 0; i < 2 * n; ++i) upd(0, 0, 2 * n - 1, i, {1e9, 0});
for(int i = 0; i < n; ++i) upd(0, 0, 2 * n - 1, e[i], {s[i], i}, 1);
for(int i = 0; i < n; ++i){
bl[i][0] = f(0, 0, 2 * n - 1, s[i], e[i]).second;
}
for(int j = 1; j < LOG; ++j){
for(int i = 0; i < n; ++i){
bl[i][j] = bl[bl[i][j - 1]][j - 1];
}
}
while(q--){
int qs, qe; cin >> qs >> qe, --qs, --qe;
if(qs == qe){
cout << "0\n"; continue;
} else if(s[qe] <= e[qs] && e[qs] <= e[qe]){
cout << "1\n"; continue;
} else if(e[qs] > e[qe]){
cout << "impossible\n"; continue;
}
int ret = 0;
for(int i = LOG - 1; i >= 0; --i){
int idx = bl[qe][i];
if(e[qs] < s[idx]){
qe = idx;
ret += 1 << i;
}
}
if(e[qs] < s[bl[qe][0]]) cout << "impossible\n";
else cout << ret + 2 << "\n";
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
177 ms |
25584 KB |
Output is correct |
3 |
Correct |
151 ms |
25508 KB |
Output is correct |
4 |
Correct |
223 ms |
25536 KB |
Output is correct |
5 |
Correct |
179 ms |
26168 KB |
Output is correct |
6 |
Correct |
163 ms |
26840 KB |
Output is correct |
7 |
Correct |
167 ms |
27064 KB |
Output is correct |
8 |
Correct |
197 ms |
33720 KB |
Output is correct |
9 |
Correct |
194 ms |
33696 KB |
Output is correct |
10 |
Correct |
176 ms |
25960 KB |
Output is correct |
11 |
Correct |
170 ms |
28364 KB |
Output is correct |
12 |
Correct |
121 ms |
26076 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
5040 KB |
Output is correct |
3 |
Correct |
3 ms |
5204 KB |
Output is correct |
4 |
Correct |
3 ms |
5204 KB |
Output is correct |
5 |
Correct |
3 ms |
5204 KB |
Output is correct |
6 |
Correct |
3 ms |
5204 KB |
Output is correct |
7 |
Correct |
3 ms |
5204 KB |
Output is correct |
8 |
Correct |
3 ms |
5204 KB |
Output is correct |
9 |
Correct |
3 ms |
5076 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
5040 KB |
Output is correct |
3 |
Correct |
3 ms |
5204 KB |
Output is correct |
4 |
Correct |
3 ms |
5204 KB |
Output is correct |
5 |
Correct |
3 ms |
5204 KB |
Output is correct |
6 |
Correct |
3 ms |
5204 KB |
Output is correct |
7 |
Correct |
3 ms |
5204 KB |
Output is correct |
8 |
Correct |
3 ms |
5204 KB |
Output is correct |
9 |
Correct |
3 ms |
5076 KB |
Output is correct |
10 |
Correct |
2 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
5204 KB |
Output is correct |
13 |
Correct |
3 ms |
5204 KB |
Output is correct |
14 |
Correct |
3 ms |
5204 KB |
Output is correct |
15 |
Correct |
3 ms |
5204 KB |
Output is correct |
16 |
Correct |
5 ms |
5204 KB |
Output is correct |
17 |
Correct |
5 ms |
5204 KB |
Output is correct |
18 |
Correct |
4 ms |
5076 KB |
Output is correct |
19 |
Correct |
30 ms |
6664 KB |
Output is correct |
20 |
Correct |
27 ms |
6492 KB |
Output is correct |
21 |
Correct |
29 ms |
6848 KB |
Output is correct |
22 |
Correct |
24 ms |
6824 KB |
Output is correct |
23 |
Correct |
25 ms |
7000 KB |
Output is correct |
24 |
Correct |
22 ms |
6996 KB |
Output is correct |
25 |
Correct |
27 ms |
6724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
5040 KB |
Output is correct |
3 |
Correct |
3 ms |
5204 KB |
Output is correct |
4 |
Correct |
3 ms |
5204 KB |
Output is correct |
5 |
Correct |
3 ms |
5204 KB |
Output is correct |
6 |
Correct |
3 ms |
5204 KB |
Output is correct |
7 |
Correct |
3 ms |
5204 KB |
Output is correct |
8 |
Correct |
3 ms |
5204 KB |
Output is correct |
9 |
Correct |
3 ms |
5076 KB |
Output is correct |
10 |
Correct |
2 ms |
4948 KB |
Output is correct |
11 |
Correct |
2 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
5204 KB |
Output is correct |
13 |
Correct |
3 ms |
5204 KB |
Output is correct |
14 |
Correct |
3 ms |
5204 KB |
Output is correct |
15 |
Correct |
3 ms |
5132 KB |
Output is correct |
16 |
Correct |
4 ms |
5204 KB |
Output is correct |
17 |
Correct |
3 ms |
5204 KB |
Output is correct |
18 |
Correct |
3 ms |
5076 KB |
Output is correct |
19 |
Correct |
128 ms |
24888 KB |
Output is correct |
20 |
Correct |
126 ms |
24852 KB |
Output is correct |
21 |
Correct |
155 ms |
25456 KB |
Output is correct |
22 |
Correct |
156 ms |
27160 KB |
Output is correct |
23 |
Correct |
184 ms |
32516 KB |
Output is correct |
24 |
Correct |
183 ms |
32600 KB |
Output is correct |
25 |
Correct |
86 ms |
18936 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
159 ms |
25560 KB |
Output is correct |
2 |
Correct |
179 ms |
25552 KB |
Output is correct |
3 |
Correct |
199 ms |
25516 KB |
Output is correct |
4 |
Correct |
212 ms |
33760 KB |
Output is correct |
5 |
Correct |
161 ms |
25948 KB |
Output is correct |
6 |
Correct |
270 ms |
33288 KB |
Output is correct |
7 |
Correct |
227 ms |
33512 KB |
Output is correct |
8 |
Correct |
233 ms |
33412 KB |
Output is correct |
9 |
Correct |
242 ms |
32624 KB |
Output is correct |
10 |
Correct |
222 ms |
32924 KB |
Output is correct |
11 |
Correct |
207 ms |
32760 KB |
Output is correct |
12 |
Correct |
209 ms |
33036 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
177 ms |
25584 KB |
Output is correct |
3 |
Correct |
151 ms |
25508 KB |
Output is correct |
4 |
Correct |
223 ms |
25536 KB |
Output is correct |
5 |
Correct |
179 ms |
26168 KB |
Output is correct |
6 |
Correct |
163 ms |
26840 KB |
Output is correct |
7 |
Correct |
167 ms |
27064 KB |
Output is correct |
8 |
Correct |
197 ms |
33720 KB |
Output is correct |
9 |
Correct |
194 ms |
33696 KB |
Output is correct |
10 |
Correct |
176 ms |
25960 KB |
Output is correct |
11 |
Correct |
170 ms |
28364 KB |
Output is correct |
12 |
Correct |
121 ms |
26076 KB |
Output is correct |
13 |
Correct |
2 ms |
4948 KB |
Output is correct |
14 |
Correct |
2 ms |
5040 KB |
Output is correct |
15 |
Correct |
3 ms |
5204 KB |
Output is correct |
16 |
Correct |
3 ms |
5204 KB |
Output is correct |
17 |
Correct |
3 ms |
5204 KB |
Output is correct |
18 |
Correct |
3 ms |
5204 KB |
Output is correct |
19 |
Correct |
3 ms |
5204 KB |
Output is correct |
20 |
Correct |
3 ms |
5204 KB |
Output is correct |
21 |
Correct |
3 ms |
5076 KB |
Output is correct |
22 |
Correct |
2 ms |
4948 KB |
Output is correct |
23 |
Correct |
3 ms |
4948 KB |
Output is correct |
24 |
Correct |
3 ms |
5204 KB |
Output is correct |
25 |
Correct |
3 ms |
5204 KB |
Output is correct |
26 |
Correct |
3 ms |
5204 KB |
Output is correct |
27 |
Correct |
3 ms |
5204 KB |
Output is correct |
28 |
Correct |
5 ms |
5204 KB |
Output is correct |
29 |
Correct |
5 ms |
5204 KB |
Output is correct |
30 |
Correct |
4 ms |
5076 KB |
Output is correct |
31 |
Correct |
30 ms |
6664 KB |
Output is correct |
32 |
Correct |
27 ms |
6492 KB |
Output is correct |
33 |
Correct |
29 ms |
6848 KB |
Output is correct |
34 |
Correct |
24 ms |
6824 KB |
Output is correct |
35 |
Correct |
25 ms |
7000 KB |
Output is correct |
36 |
Correct |
22 ms |
6996 KB |
Output is correct |
37 |
Correct |
27 ms |
6724 KB |
Output is correct |
38 |
Correct |
2 ms |
4948 KB |
Output is correct |
39 |
Correct |
2 ms |
4948 KB |
Output is correct |
40 |
Correct |
3 ms |
5204 KB |
Output is correct |
41 |
Correct |
3 ms |
5204 KB |
Output is correct |
42 |
Correct |
3 ms |
5204 KB |
Output is correct |
43 |
Correct |
3 ms |
5132 KB |
Output is correct |
44 |
Correct |
4 ms |
5204 KB |
Output is correct |
45 |
Correct |
3 ms |
5204 KB |
Output is correct |
46 |
Correct |
3 ms |
5076 KB |
Output is correct |
47 |
Correct |
128 ms |
24888 KB |
Output is correct |
48 |
Correct |
126 ms |
24852 KB |
Output is correct |
49 |
Correct |
155 ms |
25456 KB |
Output is correct |
50 |
Correct |
156 ms |
27160 KB |
Output is correct |
51 |
Correct |
184 ms |
32516 KB |
Output is correct |
52 |
Correct |
183 ms |
32600 KB |
Output is correct |
53 |
Correct |
86 ms |
18936 KB |
Output is correct |
54 |
Correct |
159 ms |
25560 KB |
Output is correct |
55 |
Correct |
179 ms |
25552 KB |
Output is correct |
56 |
Correct |
199 ms |
25516 KB |
Output is correct |
57 |
Correct |
212 ms |
33760 KB |
Output is correct |
58 |
Correct |
161 ms |
25948 KB |
Output is correct |
59 |
Correct |
270 ms |
33288 KB |
Output is correct |
60 |
Correct |
227 ms |
33512 KB |
Output is correct |
61 |
Correct |
233 ms |
33412 KB |
Output is correct |
62 |
Correct |
242 ms |
32624 KB |
Output is correct |
63 |
Correct |
222 ms |
32924 KB |
Output is correct |
64 |
Correct |
207 ms |
32760 KB |
Output is correct |
65 |
Correct |
209 ms |
33036 KB |
Output is correct |
66 |
Correct |
2 ms |
4948 KB |
Output is correct |
67 |
Correct |
183 ms |
25480 KB |
Output is correct |
68 |
Correct |
154 ms |
25532 KB |
Output is correct |
69 |
Correct |
161 ms |
25532 KB |
Output is correct |
70 |
Correct |
163 ms |
26168 KB |
Output is correct |
71 |
Correct |
176 ms |
26828 KB |
Output is correct |
72 |
Correct |
165 ms |
27052 KB |
Output is correct |
73 |
Correct |
202 ms |
33672 KB |
Output is correct |
74 |
Correct |
199 ms |
33668 KB |
Output is correct |
75 |
Correct |
153 ms |
25900 KB |
Output is correct |
76 |
Correct |
195 ms |
28420 KB |
Output is correct |
77 |
Correct |
113 ms |
25976 KB |
Output is correct |
78 |
Correct |
2 ms |
4948 KB |
Output is correct |
79 |
Correct |
3 ms |
5204 KB |
Output is correct |
80 |
Correct |
3 ms |
5204 KB |
Output is correct |
81 |
Correct |
4 ms |
5240 KB |
Output is correct |
82 |
Correct |
3 ms |
5204 KB |
Output is correct |
83 |
Correct |
5 ms |
5204 KB |
Output is correct |
84 |
Correct |
3 ms |
5204 KB |
Output is correct |
85 |
Correct |
3 ms |
5204 KB |
Output is correct |
86 |
Correct |
29 ms |
6724 KB |
Output is correct |
87 |
Correct |
28 ms |
6560 KB |
Output is correct |
88 |
Correct |
31 ms |
6904 KB |
Output is correct |
89 |
Correct |
24 ms |
6824 KB |
Output is correct |
90 |
Correct |
26 ms |
7004 KB |
Output is correct |
91 |
Correct |
27 ms |
7064 KB |
Output is correct |
92 |
Correct |
25 ms |
6676 KB |
Output is correct |
93 |
Correct |
140 ms |
24932 KB |
Output is correct |
94 |
Correct |
150 ms |
24908 KB |
Output is correct |
95 |
Correct |
191 ms |
25528 KB |
Output is correct |
96 |
Correct |
149 ms |
27180 KB |
Output is correct |
97 |
Correct |
213 ms |
32528 KB |
Output is correct |
98 |
Correct |
208 ms |
32528 KB |
Output is correct |
99 |
Correct |
93 ms |
19188 KB |
Output is correct |
100 |
Correct |
261 ms |
35128 KB |
Output is correct |
101 |
Correct |
237 ms |
35872 KB |
Output is correct |
102 |
Correct |
222 ms |
35984 KB |
Output is correct |
103 |
Correct |
251 ms |
35548 KB |
Output is correct |
104 |
Correct |
262 ms |
35380 KB |
Output is correct |
105 |
Correct |
217 ms |
35512 KB |
Output is correct |
106 |
Correct |
154 ms |
26696 KB |
Output is correct |
107 |
Correct |
166 ms |
28076 KB |
Output is correct |
108 |
Correct |
180 ms |
28916 KB |
Output is correct |
109 |
Correct |
181 ms |
28908 KB |
Output is correct |
110 |
Correct |
229 ms |
35988 KB |
Output is correct |
111 |
Correct |
221 ms |
35776 KB |
Output is correct |