#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 1e5 + 5;
const int INF = 1e9 + 7;
int S[MAX_N], E[MAX_N];
int pre[MAX_N][20];
pair <int, int> tree[8 * MAX_N];
void build(int idx, int l, int r) {
tree[idx] = make_pair(INF, INF);
if(l == r) {
return;
}
int mid = (l + r) / 2;
build(idx * 2, l, mid);
build(idx * 2 + 1, mid + 1, r);
}
void update(int idx, int l, int r, int q, pair <int, int> v) {
if(l == r) {
tree[idx] = min(tree[idx], v);
return;
}
int mid = (l + r) / 2;
if(q <= mid) {
update(idx * 2, l, mid, q, v);
}
else {
update(idx * 2 + 1, mid + 1, r, q, v);
}
tree[idx] = min(tree[idx * 2], tree[idx * 2 + 1]);
}
pair <int, int> query(int idx, int l, int r, int ql, int qr) {
if(r < ql or qr < l) {
return make_pair(INF, INF);
}
if(ql <= l and r <= qr) {
return tree[idx];
}
int mid = (l + r) / 2;
return min(query(idx * 2, l, mid, ql, qr), query(idx * 2 + 1, mid + 1, r, ql, qr));
}
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int N, M, Q;
cin >> N >> Q;
vector <int> vec;
for(int i = 1; i <= N; i++) {
cin >> S[i] >> E[i];
vec.push_back(S[i]);
vec.push_back(E[i]);
}
sort(vec.begin(), vec.end());
vec.resize(unique(vec.begin(), vec.end()) - vec.begin());
M = vec.size();
for(int i = 1; i <= N; i++) {
S[i] = lower_bound(vec.begin(), vec.end(), S[i]) - vec.begin() + 1;
E[i] = lower_bound(vec.begin(), vec.end(), E[i]) - vec.begin() + 1;
}
build(1, 1, M);
for(int i = 1; i <= N; i++) {
update(1, 1, M, E[i], make_pair(S[i], i));
}
for(int i = 1; i <= N; i++) {
auto [lm, nxt] = query(1, 1, M, S[i], E[i]);
if(nxt != i and lm != S[i]) {
pre[i][0] = nxt;
}
}
for(int j = 1; j < 20; j++) {
for(int i = 1; i <= N; i++) {
pre[i][j] = pre[pre[i][j - 1]][j - 1];
}
}
while(Q--) {
int s, e;
cin >> s >> e;
if(s == e) {
cout << "0\n";
}
else if(E[s] > E[e]) {
cout << "impossible\n";
}
else if(S[e] <= E[s] and E[s] <= E[e]) {
cout << "1\n";
}
else {
int ans = 2;
for(int i = 19; i >= 0; i--) {
if(pre[e][i] != 0 and E[s] < S[pre[e][i]]) {
e = pre[e][i];
ans += (1<<i);
}
}
if(pre[e][0] != 0 and S[pre[e][0]] <= E[s]) {
cout << ans << '\n';
}
else {
cout << "impossible\n";
}
}
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
134 ms |
15584 KB |
Output is correct |
3 |
Correct |
136 ms |
15584 KB |
Output is correct |
4 |
Correct |
191 ms |
15472 KB |
Output is correct |
5 |
Correct |
165 ms |
15576 KB |
Output is correct |
6 |
Correct |
139 ms |
15688 KB |
Output is correct |
7 |
Correct |
142 ms |
15708 KB |
Output is correct |
8 |
Correct |
134 ms |
18140 KB |
Output is correct |
9 |
Correct |
124 ms |
18148 KB |
Output is correct |
10 |
Correct |
151 ms |
15964 KB |
Output is correct |
11 |
Correct |
145 ms |
18052 KB |
Output is correct |
12 |
Correct |
91 ms |
15320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
324 KB |
Output is correct |
3 |
Correct |
1 ms |
464 KB |
Output is correct |
4 |
Correct |
1 ms |
372 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
352 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
324 KB |
Output is correct |
3 |
Correct |
1 ms |
464 KB |
Output is correct |
4 |
Correct |
1 ms |
372 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
352 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
328 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
464 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
2 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
35 ms |
2364 KB |
Output is correct |
20 |
Correct |
29 ms |
2504 KB |
Output is correct |
21 |
Correct |
34 ms |
2764 KB |
Output is correct |
22 |
Correct |
26 ms |
2772 KB |
Output is correct |
23 |
Correct |
32 ms |
2744 KB |
Output is correct |
24 |
Correct |
25 ms |
2820 KB |
Output is correct |
25 |
Correct |
28 ms |
2380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
324 KB |
Output is correct |
3 |
Correct |
1 ms |
464 KB |
Output is correct |
4 |
Correct |
1 ms |
372 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
352 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
376 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
372 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 |
464 KB |
Output is correct |
19 |
Correct |
105 ms |
13764 KB |
Output is correct |
20 |
Correct |
99 ms |
12964 KB |
Output is correct |
21 |
Correct |
115 ms |
13728 KB |
Output is correct |
22 |
Correct |
103 ms |
15884 KB |
Output is correct |
23 |
Correct |
127 ms |
15880 KB |
Output is correct |
24 |
Correct |
130 ms |
15864 KB |
Output is correct |
25 |
Correct |
34 ms |
11088 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
128 ms |
15716 KB |
Output is correct |
2 |
Correct |
134 ms |
15536 KB |
Output is correct |
3 |
Correct |
159 ms |
15324 KB |
Output is correct |
4 |
Correct |
140 ms |
17756 KB |
Output is correct |
5 |
Correct |
172 ms |
15768 KB |
Output is correct |
6 |
Correct |
171 ms |
17696 KB |
Output is correct |
7 |
Correct |
178 ms |
17552 KB |
Output is correct |
8 |
Correct |
170 ms |
17856 KB |
Output is correct |
9 |
Correct |
143 ms |
15796 KB |
Output is correct |
10 |
Correct |
156 ms |
17328 KB |
Output is correct |
11 |
Correct |
154 ms |
17208 KB |
Output is correct |
12 |
Correct |
161 ms |
17256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
134 ms |
15584 KB |
Output is correct |
3 |
Correct |
136 ms |
15584 KB |
Output is correct |
4 |
Correct |
191 ms |
15472 KB |
Output is correct |
5 |
Correct |
165 ms |
15576 KB |
Output is correct |
6 |
Correct |
139 ms |
15688 KB |
Output is correct |
7 |
Correct |
142 ms |
15708 KB |
Output is correct |
8 |
Correct |
134 ms |
18140 KB |
Output is correct |
9 |
Correct |
124 ms |
18148 KB |
Output is correct |
10 |
Correct |
151 ms |
15964 KB |
Output is correct |
11 |
Correct |
145 ms |
18052 KB |
Output is correct |
12 |
Correct |
91 ms |
15320 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
324 KB |
Output is correct |
15 |
Correct |
1 ms |
464 KB |
Output is correct |
16 |
Correct |
1 ms |
372 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
2 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
352 KB |
Output is correct |
22 |
Correct |
0 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
328 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
1 ms |
464 KB |
Output is correct |
26 |
Correct |
1 ms |
468 KB |
Output is correct |
27 |
Correct |
1 ms |
364 KB |
Output is correct |
28 |
Correct |
1 ms |
468 KB |
Output is correct |
29 |
Correct |
2 ms |
468 KB |
Output is correct |
30 |
Correct |
1 ms |
468 KB |
Output is correct |
31 |
Correct |
35 ms |
2364 KB |
Output is correct |
32 |
Correct |
29 ms |
2504 KB |
Output is correct |
33 |
Correct |
34 ms |
2764 KB |
Output is correct |
34 |
Correct |
26 ms |
2772 KB |
Output is correct |
35 |
Correct |
32 ms |
2744 KB |
Output is correct |
36 |
Correct |
25 ms |
2820 KB |
Output is correct |
37 |
Correct |
28 ms |
2380 KB |
Output is correct |
38 |
Correct |
0 ms |
340 KB |
Output is correct |
39 |
Correct |
0 ms |
340 KB |
Output is correct |
40 |
Correct |
1 ms |
468 KB |
Output is correct |
41 |
Correct |
1 ms |
376 KB |
Output is correct |
42 |
Correct |
1 ms |
468 KB |
Output is correct |
43 |
Correct |
1 ms |
372 KB |
Output is correct |
44 |
Correct |
1 ms |
468 KB |
Output is correct |
45 |
Correct |
1 ms |
468 KB |
Output is correct |
46 |
Correct |
1 ms |
464 KB |
Output is correct |
47 |
Correct |
105 ms |
13764 KB |
Output is correct |
48 |
Correct |
99 ms |
12964 KB |
Output is correct |
49 |
Correct |
115 ms |
13728 KB |
Output is correct |
50 |
Correct |
103 ms |
15884 KB |
Output is correct |
51 |
Correct |
127 ms |
15880 KB |
Output is correct |
52 |
Correct |
130 ms |
15864 KB |
Output is correct |
53 |
Correct |
34 ms |
11088 KB |
Output is correct |
54 |
Correct |
128 ms |
15716 KB |
Output is correct |
55 |
Correct |
134 ms |
15536 KB |
Output is correct |
56 |
Correct |
159 ms |
15324 KB |
Output is correct |
57 |
Correct |
140 ms |
17756 KB |
Output is correct |
58 |
Correct |
172 ms |
15768 KB |
Output is correct |
59 |
Correct |
171 ms |
17696 KB |
Output is correct |
60 |
Correct |
178 ms |
17552 KB |
Output is correct |
61 |
Correct |
170 ms |
17856 KB |
Output is correct |
62 |
Correct |
143 ms |
15796 KB |
Output is correct |
63 |
Correct |
156 ms |
17328 KB |
Output is correct |
64 |
Correct |
154 ms |
17208 KB |
Output is correct |
65 |
Correct |
161 ms |
17256 KB |
Output is correct |
66 |
Correct |
1 ms |
340 KB |
Output is correct |
67 |
Correct |
132 ms |
15644 KB |
Output is correct |
68 |
Correct |
134 ms |
15572 KB |
Output is correct |
69 |
Correct |
161 ms |
15640 KB |
Output is correct |
70 |
Correct |
142 ms |
15592 KB |
Output is correct |
71 |
Correct |
133 ms |
15664 KB |
Output is correct |
72 |
Correct |
143 ms |
15884 KB |
Output is correct |
73 |
Correct |
124 ms |
18084 KB |
Output is correct |
74 |
Correct |
128 ms |
18120 KB |
Output is correct |
75 |
Correct |
151 ms |
15956 KB |
Output is correct |
76 |
Correct |
151 ms |
18104 KB |
Output is correct |
77 |
Correct |
85 ms |
15320 KB |
Output is correct |
78 |
Correct |
0 ms |
340 KB |
Output is correct |
79 |
Correct |
1 ms |
468 KB |
Output is correct |
80 |
Correct |
1 ms |
468 KB |
Output is correct |
81 |
Correct |
2 ms |
468 KB |
Output is correct |
82 |
Correct |
2 ms |
468 KB |
Output is correct |
83 |
Correct |
2 ms |
472 KB |
Output is correct |
84 |
Correct |
2 ms |
468 KB |
Output is correct |
85 |
Correct |
1 ms |
464 KB |
Output is correct |
86 |
Correct |
39 ms |
2388 KB |
Output is correct |
87 |
Correct |
32 ms |
2528 KB |
Output is correct |
88 |
Correct |
32 ms |
2768 KB |
Output is correct |
89 |
Correct |
25 ms |
2812 KB |
Output is correct |
90 |
Correct |
24 ms |
2812 KB |
Output is correct |
91 |
Correct |
22 ms |
2760 KB |
Output is correct |
92 |
Correct |
28 ms |
2396 KB |
Output is correct |
93 |
Correct |
104 ms |
13796 KB |
Output is correct |
94 |
Correct |
101 ms |
12932 KB |
Output is correct |
95 |
Correct |
115 ms |
13664 KB |
Output is correct |
96 |
Correct |
104 ms |
15952 KB |
Output is correct |
97 |
Correct |
134 ms |
15836 KB |
Output is correct |
98 |
Correct |
148 ms |
16036 KB |
Output is correct |
99 |
Correct |
34 ms |
11080 KB |
Output is correct |
100 |
Correct |
169 ms |
17800 KB |
Output is correct |
101 |
Correct |
157 ms |
17748 KB |
Output is correct |
102 |
Correct |
163 ms |
17880 KB |
Output is correct |
103 |
Correct |
155 ms |
17424 KB |
Output is correct |
104 |
Correct |
155 ms |
17184 KB |
Output is correct |
105 |
Correct |
171 ms |
17360 KB |
Output is correct |
106 |
Correct |
166 ms |
15044 KB |
Output is correct |
107 |
Correct |
164 ms |
15052 KB |
Output is correct |
108 |
Correct |
161 ms |
15688 KB |
Output is correct |
109 |
Correct |
139 ms |
15704 KB |
Output is correct |
110 |
Correct |
176 ms |
17676 KB |
Output is correct |
111 |
Correct |
175 ms |
17628 KB |
Output is correct |